使用改造上传图像时出现FileNotFoundException [英] FileNotFoundException while uploading image using Retrofit

查看:135
本文介绍了使用改造上传图像时出现FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Retrofit上传图像,但出现此错误:

I tried to upload an image using Retrofit but I am getting this error:

Unable to submit post to API: java.io.FileNotFoundException: /document/image:30231: open failed: ENOENT (No such file or directory)

我的界面是这样的:

public interface MyService{
    @Multipart
    @POST("/url")
    Call<ResponseBody> addNewEvent( @Part("case_Id") int caseId,@Part MultipartBody.Part(file);
}

单击按钮,将调用selectImage()函数:

On Button click, selectImage() function is called:

private void selectImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Please select image",1);
}

onActivityResult部分,我做了以下事情:

In the onActivityResult part, I did the following:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode!=RESULT_CANCELED && resultCode == RESULT_OK && data != null && data.getData() != null) {
        FilePathUri = data.getData();
        doAddNewEvent();
    }
}

从上方调用doAddNewEvent()函数:

public void doAddNewEvent() {
    File file = new File(FilePathuri.getPath());
    RequestBody requestFile=RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

 apiService.addNewEvent(inputCaseId, body).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {                    
                ResponseBody addEventResponse = response.body();
                Log.d("as", "response: " + addEventResponse);
                finish();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("as", "Unable to submit post to API");
        }
    });
}

推荐答案

获取文件路径存在问题

尝试一下:

if (requestCode == 1 && resultCode!=RESULT_CANCELED && resultCode == RESULT_OK && data != null && data.getData() != null) {

            FilePathStr = null;

            if (data != null) {



                Uri selectedImage = data.getData();
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath,
                        null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                FilePathStr = c.getString(columnIndex);
                c.close();
                doAddNewEvent();




            }

        }

并使用字符串路径进行分段

and make multipart using string path

MultipartBody.Part body = null;

            if (FilePathStr != null) {
                File file = new File(FilePathStr );
                RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
                body = MultipartBody.Part.createFormData("image", file.getName(), reqFile);

            }

这篇关于使用改造上传图像时出现FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆