无法在SD卡上写入文件Android模拟器

发布于 2024-12-23 06:42:40 字数 1223 浏览 4 评论 0原文

我正在尝试在 SD 卡上写入 MIDI 文件。它可以在我的设备上运行,但不能在模拟器中运行。这是 logCat 上的错误:

12-25 09:18:27.733: W/ExternalStorage(1951): Error create /sdcard/download/test.mid
12-25 09:18:27.733: W/ExternalStorage(1951): java.io.FileNotFoundException: /sdcard/download/test.mid
12-25 09:18:27.733: W/ExternalStorage(1951):    at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:147)

我已经在清单文件中设置了权限,并将路径从“/sdcard/download/test.mid”更改为Environment.getExternalStorageDirectory()+“/download/test.mid”,但仍然没有不工作。

仅供参考,我使用 Kevin Boone 的 MidiFile 类,这是我的代码:

String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mf.writeToFile(Environment.getExternalStorageDirectory()+"/download/test.mid");
    }

I am trying to write a MIDI file on SDcard. It works on my device but not in the emulator. Here's the error on the logCat :

12-25 09:18:27.733: W/ExternalStorage(1951): Error create /sdcard/download/test.mid
12-25 09:18:27.733: W/ExternalStorage(1951): java.io.FileNotFoundException: /sdcard/download/test.mid
12-25 09:18:27.733: W/ExternalStorage(1951):    at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:147)

I have set the permission in the manifest file, and change the path from "/sdcard/download/test.mid" to Environment.getExternalStorageDirectory()+"/download/test.mid" but still doesn't work.

FYI, I use Kevin Boone's MidiFile class, and here's my code :

String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mf.writeToFile(Environment.getExternalStorageDirectory()+"/download/test.mid");
    }

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

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

发布评论

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

评论(2

请爱~陌生人 2024-12-30 06:42:41

首先确保您拥有适当的权限,如果您没有将其添加到 AndroidManifest.xml

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

其次,您还应该检查下载文件夹是否存在,如果不存在则必须创建它。检查 File 类中是否存在该方法。

如果您看一下 Kevin 的 MidiFile 方法,使用它仅在特定路径上创建文件,它不能确保您的工作路径存在

mg.writeToFile ("somefile.mid");

Well first make sure you have the proper permission, if you don't add this to you're AndroidManifest.xml

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

Secondly you should also check if download folder exist if not you have to create it. Check the method exist from the File class.

If you take a look at Kevin's MidiFile method use it only create a file on a specific path it does not ensure the path exist that is your job

mg.writeToFile ("somefile.mid");
宣告ˉ结束 2024-12-30 06:42:41

我建议检查您是否创建了模拟器的 SD 卡并具有写入权限。

请参阅:Android 模拟器 sdcard 推送错误:读取-only file system

一旦你完成了这一步,我认为它也应该以编程方式为你工作。

I would suggest checking if you have emaulator's sdcard is created with permissions to write on it.

Refer this: Android Emulator sdcard push error: Read-only file system

Once you are through with this step, I think it shall work for you programatically as well.

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