使用 Retrofit 上传图片时出现 FileNotFoundException。

4

我试图使用Retrofit上传一张图片,但是遇到了以下错误:

无法将帖子提交到API:java.io.FileNotFoundException:/document/image:30231:打开失败:ENOENT(没有这样的文件或目录)

我的接口如下:

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() 存在一些问题,请调试你的代码。查看这个讨论或者搜索类似的内容。 - ADM
FilePathUri.getPath() 中,我得到了这个:/document/image:23165 - sangeeta
java.io.FileNotFoundException: /document/image:30231:,实际上这不是文件系统上的有效路径。这是内容方案的一部分。完整的方案由FilePathUri.toString()给出。 - greenapps
有没有办法可以获取正确的路径? - sangeeta
你应该问:有没有办法让我直接使用获取到的URI进行Retrofit改造?或者内容方案?你能让Retrofit从流中上传文件吗?从InputStream中呢? - greenapps
1个回答

2

获取文件路径存在问题。

尝试使用以下方法:

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 类型,所以会出现错误。 - sangeeta
请使用字符串代替URI,检查更新后的答案。 - Adil
但是 data.getData() 需要 Uri 类型而不是字符串。 - sangeeta
好的@sangeeta,请确认我已删除URI代码它将起作用 - Adil
欢迎,现在您可以使用此答案进行正确的勾选。 - Adil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接