无法创建文件

时间:2013-09-06 14:04:43

标签: android file ioexception

我正在尝试在Android上创建一个简单的图像文件,并使用以下两种方法: 创建目录:

    private void createThumbnailDir() {
    File file = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    + File.separator + "scouthouse",
            "scouthouse_thumbnails");
    this.getActivity().sendBroadcast(
            new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                    + Environment.getExternalStorageDirectory())));
    if (!file.mkdirs()) {
        Log.d("file", "file not created");
    }
}

创建文件:

    private File createNewThumbnailFile() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss",
            Locale.ENGLISH);
    Date date = Calendar.getInstance().getTime();
    File file = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "scouthouse" + File.separator + "schouthouse_thumbnails" + File.separator
                    + sdf.format(date) + ".jpg");
    try {
        if (file.createNewFile()) {
            return file;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

当我创建文件时,会引发以下IOException:        java.io.IOException:ENOENT(没有这样的文件或目录)

但是当我检查手机上的文件管理器时,该目录确实存在。

编辑:

有关错误的更多信息: stacktrace = null,所以我只有cause和detailmessage,它们都是相同的:libcore.io.ErrnoException:open failed:ENOENT(没有这样的文件或目录)

2 个答案:

答案 0 :(得分:3)

你的清单上有这个吗?

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

? 编辑:

      private File createThumbnailDir() {
        File file = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                        + File.separator + "scouthouse",
                "scouthouse_thumbnails");
        this.getActivity().sendBroadcast(
                new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                        + Environment.getExternalStorageDirectory())));
        if (!file.mkdirs()) {
            Log.d("file", "file not created");
            return file;
        }else {return null}
    }  

现在:

  private File createNewThumbnailFile() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss",
            Locale.ENGLISH);
    Date date = Calendar.getInstance().getTime();
    String filename= sdf.format(date) + ".jpg";
    File file = new File(createThumbnailDir(), filename); 

 try {
    outStream = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPG, 100, outStream);
    outStream.flush();
    outStream.close();

    Toast.makeText(AndroidWebImage.this, "Saved", Toast.LENGTH_LONG).show();

   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
   }

    return null;
}

reference

答案 1 :(得分:0)

您是否尝试过将文件保存在其他位置?您的应用可能没有写入权限。