Android - java.io.FileNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24676525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:34:13  来源:igfitidea点击:

Android - java.io.FileNotFoundException

androidfilenotfoundexception

提问by srinu_stack

when i was inserting the bitmap image to files directory, it showing file not found exception and it is showing Is a Directory.

当我将位图图像插入文件目录时,它显示文件未找到异常,它显示是一个目录。

Here is my code:

这是我的代码:

            File mFolder = new File(getFilesDir() + "/sample");

            if (!mFolder.exists()) {
                mFolder.mkdir();
            }
             FileOutputStream fos = null;
             try {
                 fos = new FileOutputStream(mFolder);
                 bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);

                 fos.flush();
                 fos.close();
              //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
             }catch (FileNotFoundException e) {

                 e.printStackTrace();
             } catch (Exception e) {

                 e.printStackTrace();
             }

Logcat:

日志猫:

07-12 01:08:05.434: W/System.err(8170): java.io.FileNotFoundException: /data/data/com.sample.sam/files/sample(Is a directory)
07-12 01:08:05.434: W/System.err(8170):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
07-12 01:08:05.434: W/System.err(8170):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
07-12 01:08:05.444: W/System.err(8170):     at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
07-12 01:08:05.444: W/System.err(8170):     at java.io.FileOutputStream.<init>(FileOutputStream.java:77)

回答by Autocrab

you have to create file, before writing into stream.

在写入流之前,您必须创建文件。

File mFolder = new File(getFilesDir() + "/sample");
File imgFile = new File(mFolder.getAbsolutePath() + "/someimage.png");
if (!mFolder.exists()) {
    mFolder.mkdir();
}
if (!imgFile.exists()) {
    imgFile.createNewFile();
}
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(imgFile);
    bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);
    fos.flush();
    fos.close();
} catch (IOException e) {
     e.printStackTrace();
}

回答by Gil Moshayof

Use file.createNewFile()if you want to make a file and not a directory. You may also need to use mkDirs()if the path doesn't exist either.

使用file.createNewFile(),如果你想使一个文件,而不是一个目录。mkDirs()如果路径也不存在,您可能还需要使用。

回答by Henry

You are opening the directory itself for writing. This does not work. You need to specify a file within the directory, e.g.:

您正在打开目录本身进行写入。这不起作用。您需要在目录中指定一个文件,例如:

fos = new FileOutputStream(new File(mFolder, "myfile"));

回答by Sujit Jamdade

File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/text/");
dir.mkdir();
File file = new File(dir, "sample.txt");
FileOutputStream os = null;
try {
    os = new FileOutputStream(file);
    os.write(enterText.getText().toString().getBytes());
    os.close();
}
catch (IOException e) {
    e.printStackTrace();
}