无法在SD卡上写入文件Android模拟器
我正在尝试在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先确保您拥有适当的权限,如果您没有将其添加到 AndroidManifest.xml
其次,您还应该检查下载文件夹是否存在,如果不存在则必须创建它。检查 File 类中是否存在该方法。
如果您看一下 Kevin 的 MidiFile 方法,使用它仅在特定路径上创建文件,它不能确保您的工作路径存在
Well first make sure you have the proper permission, if you don't add this to you're AndroidManifest.xml
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
我建议检查您是否创建了模拟器的 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.