我尝试访问时仍然会收到FileNotFoundException
吗?
请问我做错了什么?
File cacheDir;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
{
//if SDCARD is mounted (SDCARD is present on device and mounted)
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
}
else
{
// if checking on simulator the create cache dir in your application context
cacheDir=MainActivity.this.getCacheDir();
}
if(!cacheDir.exists()){ // create cache dir in your application context
cacheDir.mkdirs();
}
String filename=String.valueOf(rss.getChannel().getItems().get(i).getEnclosure().getUrl().hashCode());
File f = new File(cacheDir, filename);
答案 0 :(得分:1)
下面
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
// ...
cacheDir.mkdirs();
根据getExternalStorageDirectory
写入此路径需要
WRITE_EXTERNAL_STORAGE
权限
另外,我建议始终检查mkdirs
的返回值,该值指示目录创建是否成功,因为此方法的文档说明
请注意,此方法在失败时不会抛出
IOException
。