为什么我会得到FileNotFoundException?

时间:2015-01-06 15:19:56

标签: android file

对我来说这看起来很好,

我尝试访问时仍然会收到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);

1 个答案:

答案 0 :(得分:1)

下面

cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
// ...
cacheDir.mkdirs();

根据getExternalStorageDirectory

的文档
  

写入此路径需要WRITE_EXTERNAL_STORAGE权限

另外,我建议始终检查mkdirs的返回值,该值指示目录创建是否成功,因为此方法的文档说明

  

请注意,此方法在失败时不会抛出IOException