Android 为什么会出现 ENOENT (No such file or directory) 错误

Android Why this error ENOENT (No such file or directory) comes

我想知道为什么当尝试使用 Marshmallow 通过真实设备上传文件时会出现此错误。能否请您解释一下解决方案。

12-28 10:39:32.606: E/3(17552): Excption : java.io.FileNotFoundException: /storage/emulated/0/WoodenstreetD.doc: open failed: ENOENT (No such file or directory)

上周我一直被这个问题困扰,但仍然遇到同样的问题。 提前致谢。

此错误是由于路径错误造成的。您从 onActivityResult 获取的路径不正确。使用它来获取所选文件的路径。

public static String getRealPathFromUri(Context ctx, Uri uri) {
    String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };

    Cursor cursor = ctx.getContentResolver().query(uri, filePathColumn,
            null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        Log.e("", "picturePath : " + picturePath);
        cursor.close();
    }
    return picturePath;
}

picturePath 为您提供 image/file.

的完整路径

对我有用。

这是我写的,对我有用。

 public static java.io.File convertUriTFile(Activity activity, Uri uri) {
        InputStream inputStream = null;
        java.io.File file = null;
        try {
            inputStream = activity.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            file = new java.io.File(activity.getCacheDir(), "temp");
            try (OutputStream output = new FileOutputStream(file)) {
                byte[] buffer = new byte[4 * 1024];
                int read;

                while ((read = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }

                output.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return file;

    }