Android的文件创建失败 [英] Android file-creation fails
本文介绍了Android的文件创建失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用下面的code上创建一个机器人单元的SD卡的文件夹mymir和文件.nomedia(在mymir文件夹)。然而,不知何故,失败,所不同的是文件夹中的.nomedia-file是被放置在不到风度并存。这里的code:
私人字符串EnsureRootDir()抛出IOException异常
{
文件SD卡= Environment.getExternalStorageDirectory();
文件mymirFolder =新的文件(sdcard.getAbsolutePath()+/ mymir /);
如果(!mymirFolder.exists())
{
文件noMedia =新的文件(mymirFolder.getAbsolutePath()+/.nomedia);
noMedia.mkdirs();
noMedia.createNewFile();
}
返回mymirFolder.getAbsolutePath();
}
解决方案
I SD真的存在?
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())==真
如果定位1.6+,你有没有声明
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
?
确切的异常可能会有帮助。
I use the following code to create a folder "mymir" and a file ".nomedia" (in the mymir-folder) on the sdcard of an android unit. However, somehow it fails with the exception that the folder the ".nomedia"-file is to be placed in dosn't exist. Here's the code:
private String EnsureRootDir() throws IOException
{
File sdcard = Environment.getExternalStorageDirectory();
File mymirFolder = new File(sdcard.getAbsolutePath() + "/mymir/");
if(!mymirFolder.exists())
{
File noMedia = new File(mymirFolder.getAbsolutePath() + "/.nomedia");
noMedia.mkdirs();
noMedia.createNewFile();
}
return mymirFolder.getAbsolutePath();
}
解决方案
I SD really there?
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) == true
If targeting 1.6+, have you declared
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
?
The exact Exception could help.
这篇关于Android的文件创建失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文