【问题标题】:Android ENOENT error while trying to write a file尝试写入文件时出现 Android ENOENT 错误
【发布时间】:2016-05-23 20:59:25
【问题描述】:

我看到很多问题都说明了同样的问题,但我似乎无法弄清楚我的代码有什么问题。
我是使用 Uri 的新手,不太了解发生了什么。
首先是负责启动相机拍照活动的代码:

void cameraAction() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    startActivityForResult(intent, 1);
    }

现在出现问题的代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);

                viewImage.setImageBitmap(bitmap);

                String path = Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                OutputStream outFile;
                String fileName = "" + System.currentTimeMillis() + ".jpg";
                File file = new File(path, fileName);
                try {//This is where the ENOENT error is shown
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == 2) {

        }
    }
}

【问题讨论】:

  • 您的外部存储上是否已存在 Phoenix/default/ 文件夹?
  • @adelphus 我认为不,我认为该方法将自行创建目录。感谢您的快速回复!你能指导我解决这个问题吗?
  • 否 - 您必须先创建任何目录。 File.mkdirs() 应该有帮助。
  • @adelphus 你在说'java.io.File.mkdirs()'吗?
  • @adelphus 所以我可以使用File file = new File(path, fileName);,然后使用file.mkdirs();,它会毫无问题地工作吗?好像不会。

标签: java android file-handling


【解决方案1】:

在尝试创建文件之前调用mkdirs()确保路径存在:

String path = Environment
        .getExternalStorageDirectory()
        + File.separator
        + "Phoenix" + File.separator + "default";

if (new File(path).mkdirs()) {
    OutputStream outFile;
    String fileName = "" + System.currentTimeMillis() + ".jpg";
    File file = new File(path, fileName);        
    ...
}

不要忘记清单中的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

【讨论】:

  • 完美,谢谢!不应该浪费我一整天的时间来解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2011-02-25
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
相关资源
最近更新 更多