【问题标题】:ENOENT no such file or directory using image picker library and transforming the path every which way, Android image upload issueENOENT 没有使用图像选择器库并以各种方式转换路径的此类文件或目录,Android 图像上传问题
【发布时间】:2026-01-21 03:50:01
【问题描述】:

我已经看到了这个问题的变化,并尝试了很多不同的东西,包括将整个东西编码为字符串等等。基本上,我从 Android 图像选择器库 (Fishbun) 中获取了一组图像路径,然后成功将该数组传递给另一个片段,这是应该上传的片段。

从那里我使用 Koush 的 Ion 框架来完成所有网络(应用程序中 99.99% 的网络都在使用它,所以它是可取的,尽管如果这成为问题,我愿意接受其他建议) .

上传片段中的相关方法:

private void uploadImage(String adId, String fileUri, Context context){


    final ProgressDialog progress=new ProgressDialog(context);
    progress.setMessage("Uploading Images");
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setIndeterminate(false);
    progress.setProgress(0);
    progress.show();


    String url = <URL HERE>;

    Log.d(TAG,"Uri: "+ fileUri);


    Uri uri = Uri.parse(fileUri);

    // creating a file body consisting of the file that we want to
    // send to the server
    File bin = new File(getRealPathFromURI(uri));

    if(bin != null){
        Ion.with(getContext())
                .load("POST",url)
                      .uploadProgressDialog(progress).setMultipartFile("image", bin).asJsonObject().setCallback(new FutureCallback<JsonObject>() {
            @Override
            public void onCompleted(Exception e, JsonObject result) {

                if(e != null){
                    Log.e("error", e.getLocalizedMessage());
                }else{
                    Log.e("result", result.getAsString());
                }

            }
        });
    }else {
        Log.d(TAG, "couldn't create file");
    }

}

private String getRealPathFromURI(Uri contentURI) {
    String result;
    Cursor cursor = getContext().getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        result = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

相关堆栈跟踪:

E/error: /api/v2/adverts/102847259/images: open failed: ENOENT (No such file or directory)

这是文件路径到达片段时的样子,然后我使用上面的方法将其转换为真实的文件路径:

/storage/emulated/0/Pictures/Screenshots/Screenshot_20160516-130248.png

【问题讨论】:

    标签: android upload image-uploading multipartform-data ion


    【解决方案1】:

    看起来您的 URL 是文件路径而不是 http url。 ENOENT 表示它正在尝试打开本地文件,但失败了。检查调试器中的值。

    【讨论】:

    • 男人,神话,传奇!感谢您回复我的帖子,您是绝对正确的,我的网址缺少前半部分,虽然我传递了一个完整的网址,但我没有。一旦我消除了这一点,由于我使用的 API 和有问题的图像的具体情况,最终构建的其余部分比上面的帖子更复杂一些。感谢很棒的图书馆。在同事介绍 Ion 后,我将在所有项目中使用它。
    • FWIW,您应该将片段值传递给 Ion.with(fragment),因此如果片段被破坏或删除,请求将被取消。通过传入上下文,请求将保持活动状态,直到上下文(我猜是调用活动)死亡。
    最近更新 更多