【问题标题】:Android Why this error ENOENT (No such file or directory) comesAndroid 为什么会出现这个错误 ENOENT (No such file or directory)
【发布时间】:2015-12-28 05:13:39
【问题描述】:

我想知道为什么尝试使用 Marshmallow 使用真实设备上传文件时会出现此错误。你能解释一下这个问题的解决方案吗?

12-28 10:39:32.606: E/3(17552): Excption : java.io.FileNotFoundException: /storage/emulated/0/WoodenstreetD.doc: 打开失败:ENOENT(没有这样的文件或目录)

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

【问题讨论】:

  • 请添加您的代码..!!
  • stackoverflow.com/questions/11620641/… .查看这个链接是因为sd卡写保护
  • 我在这里问这个问题是链接 - stackoverflow.com/questions/34469351/…
  • 如果异常消息声称指定文件是一个目录,那么您必须更改文件名或删除现有目录(如果该目录未被应用程序使用) .
  • 我该怎么做,请建议并帮助我。我是安卓新手。

标签: android filenotfoundexception


【解决方案1】:

此错误是由于路径错误。您从 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 为您提供图像/文件的完整路径。

它对我有用。

【讨论】:

  • MediaStore.Files.FileColumns.DATA 已弃用
【解决方案2】:

我写这个,它会为我工作。

 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;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2012-06-20
    • 2016-11-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多