【问题标题】:FileNotFoundException while uploading image using Retrofit使用 Retrofit 上传图像时出现 FileNotFoundException
【发布时间】:2018-02-12 16:39:33
【问题描述】:

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

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() 函数被调用:

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 部分,我做了以下操作:

@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");
        }
    });
}

【问题讨论】:

  • getPath() 调试代码时出现问题。检查This discussion或者你也可以搜索类似的。
  • FilePathUri.getPath(),我明白了:/document/image:23165
  • java.io.FileNotFoundException: /document/image:30231:。实际上,这不是文件系统上的有效路径。那是内容方案的一部分。完整的方案由 FilePathUri.toString() 给出。
  • 有什么方法可以让我找到正确的路径吗?
  • 你应该问:有什么办法可以改造让我直接使用获得的uri?还是内容方案?你可以让改造从流上传文件吗?来自 InputStream?

标签: android retrofit2 image-uploading multipart


【解决方案1】:

获取文件路径有问题

试试这个:

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();




            }

        }

并使用字符串路径制作多部分

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);

            }

【讨论】:

  • FilePathUri = c.getString(columnIndex) 这里 FilePathUri 是 Uri 类型,所以会报错。
  • 检查更新的ans,请使用字符串而不是URI
  • data.getData() 需要 Uri 类型而不是字符串
  • 好的@sangeeta,现在请检查我删除URI代码它会起作用
  • 欢迎你,现在你可以用这个答案做正确的勾选
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 2022-11-11
  • 2018-07-04
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
相关资源
最近更新 更多