写入外部存储文件未找到异常

发布于 2024-12-27 22:10:19 字数 4471 浏览 1 评论 0原文

我正在尝试将文件写入 android 中的外部存储。我不断收到错误“java.io.FileNotFoundException:/mnt/sdcard/Survey.txt(是一个目录)”。包含此代码的方法如下:

public String sendFile(System system) throws FileNotFoundException {

    File writeable = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/Survey.txt");
    try {
        writeable.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {

        Log.w("Before FO", "***");
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(context, "External SD card not mounted",
                    Toast.LENGTH_LONG).show();
        } else {
            FileOutputStream f = new FileOutputStream(writeable);
            Log.w("before write", "0");

            f.write("HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO".getBytes());
            Log.w("before write", "1");
            f.close();
        }
    } catch (Exception e) {
        Log.w("Write sd", e.toString());
    }

    return writeable.getAbsolutePath();
}

我相信程序在 FileOutputStream 减速时崩溃,因为 Log.w("Before FO", "*");执行。我几天来一直在寻找这个问题的答案,所以任何帮助将不胜感激。谢谢!

01-19 14:07:33.839: W/Before FO(641): ***
01-19 14:07:33.857: W/Write sd(641): java.io.FileNotFoundException: /mnt/sdcard/Survey.txt (Is a directory)
01-19 14:07:33.857: W/Write sd(641):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-19 14:07:33.857: W/Write sd(641):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
01-19 14:07:33.857: W/Write sd(641):    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
01-19 14:07:33.857: W/Write sd(641):    at java.io.FileOutputStream.<init>(FileOutputStream.java:77)
01-19 14:07:33.857: W/Write sd(641):    at premise.survey.IOInterface.sendFile(IOInterface.java:315)
01-19 14:07:33.857: W/Write sd(641):    at premise.survey.PremiseSurveyActivity.onClick(PremiseSurveyActivity.java:1119)
01-19 14:07:33.857: W/Write sd(641):    at android.view.View.performClick(View.java:3110)
01-19 14:07:33.857: W/Write sd(641):    at android.view.View$PerformClick.run(View.java:11934)
01-19 14:07:33.857: W/Write sd(641):    at android.os.Handler.handleCallback(Handler.java:587)
01-19 14:07:33.857: W/Write sd(641):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 14:07:33.857: W/Write sd(641):    at android.os.Looper.loop(Looper.java:132)
01-19 14:07:33.857: W/Write sd(641):    at android.app.ActivityThread.main(ActivityThread.java:4123)
01-19 14:07:33.857: W/Write sd(641):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 14:07:33.857: W/Write sd(641):    at java.lang.reflect.Method.invoke(Method.java:491)
01-19 14:07:33.857: W/Write sd(641):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-19 14:07:33.857: W/Write sd(641):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-19 14:07:33.857: W/Write sd(641):    at dalvik.system.NativeStart.main(Native Method)
01-19 14:07:33.857: I/ActivityManager(73): Starting: Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras) } from pid 641
01-19 14:07:33.857: W/WindowManager(73): Failure taking screenshot for (230x341) to layer 21020
01-19 14:07:34.027: I/ActivityManager(73): Starting: Intent { act=android.intent.action.SEND typ=plain/text flg=0x3000000 cmp=com.android.email/.activity.MessageCompose (has extras) } from pid 641
01-19 14:07:35.269: I/ActivityManager(73): Displayed com.android.email/.activity.MessageCompose: +1s197ms (total +1s391ms)
01-19 14:07:40.647: D/dalvikvm(252): GC_EXPLICIT freed 164K, 5% free 7462K/7815K, paused 4ms+3ms
01-19 14:07:45.727: D/dalvikvm(269): GC_EXPLICIT freed 8K, 8% free 6305K/6791K, paused 13ms+5ms
01-19 14:12:04.720: D/SntpClient(73): request time failed: java.net.SocketException: Address family not supported by protocol
01-19 14:12:15.947: I/Email(293): ReconcilePopImapAccountsSync: start
01-19 14:12:15.977: I/Email(293): ReconcilePopImapAccountsSync: done
01-19 14:12:16.137: D/dalvikvm(293): GC_CONCURRENT freed 236K, 8% free 7787K/8391K, paused 4ms+7ms
01-19 14:12:16.137: W/CursorWrapperInner(293): Cursor finalized without prior close()


Line 315 in IOInterface.java is FileOutputStream f = new FileOutputStream(writeable);

I am attempting to write a file to external storage in android. I keep getting the error, "java.io.FileNotFoundException: /mnt/sdcard/Survey.txt (Is a directory)." The method that contains the code for this is as follows:

public String sendFile(System system) throws FileNotFoundException {

    File writeable = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/Survey.txt");
    try {
        writeable.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {

        Log.w("Before FO", "***");
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(context, "External SD card not mounted",
                    Toast.LENGTH_LONG).show();
        } else {
            FileOutputStream f = new FileOutputStream(writeable);
            Log.w("before write", "0");

            f.write("HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO".getBytes());
            Log.w("before write", "1");
            f.close();
        }
    } catch (Exception e) {
        Log.w("Write sd", e.toString());
    }

    return writeable.getAbsolutePath();
}

I believe the program is crashing on the FileOutputStream deceleration, because Log.w("Before FO", "*"); executes. I have been searching for an answer to this problem for several day, so any help will be greatly appreciated. Thanks!

01-19 14:07:33.839: W/Before FO(641): ***
01-19 14:07:33.857: W/Write sd(641): java.io.FileNotFoundException: /mnt/sdcard/Survey.txt (Is a directory)
01-19 14:07:33.857: W/Write sd(641):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-19 14:07:33.857: W/Write sd(641):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
01-19 14:07:33.857: W/Write sd(641):    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
01-19 14:07:33.857: W/Write sd(641):    at java.io.FileOutputStream.<init>(FileOutputStream.java:77)
01-19 14:07:33.857: W/Write sd(641):    at premise.survey.IOInterface.sendFile(IOInterface.java:315)
01-19 14:07:33.857: W/Write sd(641):    at premise.survey.PremiseSurveyActivity.onClick(PremiseSurveyActivity.java:1119)
01-19 14:07:33.857: W/Write sd(641):    at android.view.View.performClick(View.java:3110)
01-19 14:07:33.857: W/Write sd(641):    at android.view.View$PerformClick.run(View.java:11934)
01-19 14:07:33.857: W/Write sd(641):    at android.os.Handler.handleCallback(Handler.java:587)
01-19 14:07:33.857: W/Write sd(641):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 14:07:33.857: W/Write sd(641):    at android.os.Looper.loop(Looper.java:132)
01-19 14:07:33.857: W/Write sd(641):    at android.app.ActivityThread.main(ActivityThread.java:4123)
01-19 14:07:33.857: W/Write sd(641):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 14:07:33.857: W/Write sd(641):    at java.lang.reflect.Method.invoke(Method.java:491)
01-19 14:07:33.857: W/Write sd(641):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-19 14:07:33.857: W/Write sd(641):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-19 14:07:33.857: W/Write sd(641):    at dalvik.system.NativeStart.main(Native Method)
01-19 14:07:33.857: I/ActivityManager(73): Starting: Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras) } from pid 641
01-19 14:07:33.857: W/WindowManager(73): Failure taking screenshot for (230x341) to layer 21020
01-19 14:07:34.027: I/ActivityManager(73): Starting: Intent { act=android.intent.action.SEND typ=plain/text flg=0x3000000 cmp=com.android.email/.activity.MessageCompose (has extras) } from pid 641
01-19 14:07:35.269: I/ActivityManager(73): Displayed com.android.email/.activity.MessageCompose: +1s197ms (total +1s391ms)
01-19 14:07:40.647: D/dalvikvm(252): GC_EXPLICIT freed 164K, 5% free 7462K/7815K, paused 4ms+3ms
01-19 14:07:45.727: D/dalvikvm(269): GC_EXPLICIT freed 8K, 8% free 6305K/6791K, paused 13ms+5ms
01-19 14:12:04.720: D/SntpClient(73): request time failed: java.net.SocketException: Address family not supported by protocol
01-19 14:12:15.947: I/Email(293): ReconcilePopImapAccountsSync: start
01-19 14:12:15.977: I/Email(293): ReconcilePopImapAccountsSync: done
01-19 14:12:16.137: D/dalvikvm(293): GC_CONCURRENT freed 236K, 8% free 7787K/8391K, paused 4ms+7ms
01-19 14:12:16.137: W/CursorWrapperInner(293): Cursor finalized without prior close()


Line 315 in IOInterface.java is FileOutputStream f = new FileOutputStream(writeable);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2025-01-03 22:10:19

在对这个主题进行了几个小时的研究后,我发现另一个人发布了一种不同的方法,如下所示:

String fileName = surveyName + ".csv";
String headings = "Hello, world!";
File path = Environment.getExternalStorageDirectory();
File file = new File(path, fileName);
path.mkdirs();
OutputStream os = new FileOutputStream(file);
os.write(headings.getBytes());

After doing a few more hours of research on this topic, I found a place were another person had posted a different way of doing this, and it is as follows:

String fileName = surveyName + ".csv";
String headings = "Hello, world!";
File path = Environment.getExternalStorageDirectory();
File file = new File(path, fileName);
path.mkdirs();
OutputStream os = new FileOutputStream(file);
os.write(headings.getBytes());
靑春怀旧 2025-01-03 22:10:19

您可能会发现您尝试写入的文件实际上是一个目录。找到该文件/目录并将其删除。

当我第一次处理文件时,我遇到了这个问题。我调用 File.mkdirs() 期望它只创建目录,但它创建了一个具有我指定的文件名的目录。

当我创建文件时,我使用两个 File 对象。第一个仅引用路径,我调用 mkdirs()。然后第二个引用该文件。

//create path
File directoryFile = new File(Environment.getExternalStorageDirectory(), "");
directoryFile.mkdirs();

//create file
File outFile = new File(Environment.getExternalStorageDirectory(), filename);

You may find that the file you are trying to write to is actually a directory. Find the file/directory and delete it.

I ran into this when I was first working with files. I called File.mkdirs() expecting it to just create the directories, but it created a directory with the filename that I specified.

When I create files I use two File objects. The first references just the path, and I call mkdirs(). The second then references the file.

//create path
File directoryFile = new File(Environment.getExternalStorageDirectory(), "");
directoryFile.mkdirs();

//create file
File outFile = new File(Environment.getExternalStorageDirectory(), filename);
皇甫轩 2025-01-03 22:10:19

另一个原因可能是 mkdirs 在 Android Q 中返回 false,请检查

Another reason could be mkdirs returns false anyway in Android Q please check this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文