java.io.FileNotFoundException при загрузке видеофайла на сервер в Android с помощью Retrofit - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь сжать видео перед загрузкой на сервер с помощью Retrofit. Я перепробовал все возможные решения, опубликованные в stackoverflow, но все еще получаю то же исключение. Тем не менее, при попадании в API это вызывает исключение, как указано ниже:

java.io.FileNotFoundException: /data/user/0/com.flexyn.debug/files/mydir (является каталогом)

Ниже приведен метод, который я использую для сохранения файла во внутреннем хранилище:

private File convertVideoPathToFile() {
        FileOutputStream outputStream = null;
        file = new File(getFilesDir(),"flexyn_video");
        if (!file.exists()) {
            file.mkdirs();
        }else if(!file.isDirectory() && file.canWrite()){
            file.delete();
            file.mkdirs();
        }

        try {
            outputStream = openFileOutput(file.getName(), Context.MODE_PRIVATE);
            outputStream.write(videoPath.getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

Ниже приведен метод вызова API для загрузки видео на сервер:

@ Override

 public void saveVideoPost(File file) {
        if (mView == null) {
            return;
        }
        mView.showLoadingLayout();
        ApiInterface apiInterface= ApiClient.getClient().create(ApiInterface.class);
        final RequestBody requestBody = RequestBody.create(MediaType.parse("video/*"), file);
        RequestBody caption = RequestBody.create(MediaType.parse("text/plain"), mView.getCaption());
        RequestBody privacy = RequestBody.create(MediaType.parse("text/plain"), mView.getPrivacyStatus());
        MultipartBody.Part imageFileBody = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
        apiInterface.postActivity(tags_ids.toString(),FlexynApplication.getInstance().getUserToken(),imageFileBody, caption, privacy)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<DefaultResponse>() {
                    @Override
                    public void call(DefaultResponse responseBody)
                    {
                        if(responseBody.getError_code()==200 && responseBody.isSuccess()){
                            mView.hideLoadingLayout();
                            Log.d(TAG, "uploaded successfully " +responseBody.getMessage());
                            success(responseBody);
                        }else{
                            mView.hideLoadingLayout();
                            error(responseBody.getMessage());
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
    // throwing error here 
                        mView.hideLoadingLayout();
                        error(AppConstant.ERROR_MESSAGE1);
                    }
                });
    }
...