问题描述
我看到一堆其他人问同样的问题,但没有张贴的解决方案帮助了我。
I see a bunch of other people asking this same question, but none of the solutions posted helped me.
我试图写一个(二进制)从我的Android应用程序文件到外部存储器。
我把<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
进入我的清单,但我仍然不能设法创建任何文件。在code我使用创建文件是
I'm trying to write a (binary) file to external storage from my Android app.I put <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
into my manifest, but I still can't manage to create any files. The code I'm using to create files is
File folder = new File(Environment.getExternalStorageDirectory(), SAVE_DIRECTORY);
File toWrite = new File(folder, "save.bin");
if(!toWrite.exists()){
try {
if(!folder.mkdirs())
Log.e("Save", "Failed to create directories for save file!");
toWrite.createNewFile();
} catch (IOException e) {
Log.e("Save", "Failed to create save file! " + e.getMessage());
}
}
到 mkdirs()
失败,并且调用 createNewFile()
投 IOException异常
(ENOENT,因为目录不存在)
The call to mkdirs()
fails, and the createNewFile()
throws the IOException
(ENOENT, because the directory doesn't exist)
有人知道这是怎么回事?我甚至试图重新启动我的设备。我在API级别8上的Nexus 7,如果这有什么差别。
Anybody know what's up? I've even tried rebooting my device. I'm on API level 8 on a Nexus 7, if it makes any difference.
推荐答案
你首先应该检查ExternalStorageState
public static boolean isSDCARDAvailable(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
如果这个方法 isSDCARDAvailable
收益真正
,然后用你的code
if this method isSDCARDAvailable
return true
, then use your code
添加权限:
&LT;使用许可权的android:NAME =android.permission.MOUNT_UNMOUNT_FILESYSTEMS/&GT;
这篇关于无法写入Android上的外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!