在 Android 中保存文件:java.io.FileNotFoundException。打开失败:ENOENT(没有那个文件或目录)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39554201/
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-11-03 04:27:37  来源:igfitidea点击:

Save file in Android: java.io.FileNotFoundException. open failed: ENOENT (No such file or directory)

javaandroid

提问by Dzmitry

I've a next problem... I searched answer on the source, but didn't find a good answer...

我有一个下一个问题......我在来源上搜索了答案,但没有找到一个好的答案......

java.io.FileNotFoundException: /mounted/EmailClient/side-corner.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:453)
at java.io.FileOutputStream.<init>(FileOutputStream.
at java.io.FileOutputStream.<init>(FileOutputStream.java:73) etc

Code:

代码:

private static String saveFile(String filename, InputStream input) {
    String path = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(path)) {
        try {
            byte[] attachment = new byte[input.available()];
            input.read(attachment);
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/EmailClient/", filename);
            if (!file.mkdirs())
                Log.d("EmailClient", "saveFile: Dir not created");
            FileOutputStream out = new FileOutputStream(file); //PROBLEM!
            out.write(attachment);
            input.close();
            out.close();
            return path;
        } catch (IOException e) {
            Log.e("EmailClient", "saveFile: File not saved", e);;
        }
    }
    return path;
}

回答by LearnPainLess

use this code

使用此代码

File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/EmailClient/");

folder.mkdirs();
File file = new File(folder,filename);
file.createNewFile();

回答by Kona Suresh

I think you are trying to save the file in the "DIRECTORY_DOWNLOADS" directory,. So you should follow the below code. The below line gives the directory.

我认为您正在尝试将文件保存在“DIRECTORY_DOWNLOADS”目录中。所以你应该遵循下面的代码。下面一行给出了目录。

   path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOWNLOADS);

If You want to create specific folder "Email_Client" inside that path, write this line.

如果您想在该路径中创建特定文件夹“Email_Client”,请写下这一行。

   File folder = new File(Environment.getExternalStoragePublicDirectory(
   Environment.DIRECTORY_DOWNLOADS)+"/Email_Client/");

And you should declare the permission "WRITE_EXTERNAL_STORAGE"

你应该声明权限“WRITE_EXTERNAL_STORAGE”

String fileName="myfile.txt";
            String input="Hello World";
            String path = Environment.getExternalStorageState();
            File file=null;
            if (Environment.MEDIA_MOUNTED.equals(path)) {
                try {
                    byte[] attachment = input.getBytes();

      File folder = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS)+"/Email_Client/");
       folder.mkdirs();


                         file=new File(folder,fileName);
          //Automatically creates the new empty file specified by the name,   if it is not exist.
                         file.createNewFile();
                        Log.i("EmailClient", "saveFile: Dir created");
                    FileOutputStream out = new FileOutputStream(file); 
                    out.write(attachment);

                    out.close();


                } catch (IOException e) {
                    Log.e("EmailClient", "saveFile: File not saved", e);;
                }
            }

Please refer this URL: https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

请参考这个网址:https: //developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)