如何处理FileNotFoundException?

时间:2011-09-16 11:43:25

标签: android file exception

在我的应用程序中,我将图像存储在缓存中。但我使用以下代码得到以下错误。如何处理它,任何人都可以帮助我吗?

异常

09-16 16:56:06.001: DEBUG/WifiService(98): enable and start wifi due to updateWifiState
09-16 17:07:36.581: WARN/System.err(21480): java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.ibkr.elgifto/cache/bitmap_dc9a5b371e3c3915d12d0f32a56075022a505119.tmp (No such file or directory)
09-16 17:07:36.611: WARN/System.err(21480):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
09-16 17:07:36.611: WARN/System.err(21480):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
09-16 17:07:36.621: WARN/System.err(21480):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
09-16 17:07:36.621: WARN/System.err(21480):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
09-16 17:07:36.631: WARN/System.err(21480):     at com.ibkr.elgifto.GiftSuggestions$itemlistadapter$4$1.run(GiftSuggestions.java:606)

代码

{

 ......

 final File file = getCacheFile(imageUrl);

 file.getParentFile().mkdirs(); 

 file.createNewFile();

 ......

}

      public File getCacheFile(String url) 
         {
             // First compute the cache key and cache file path for this URL
             File cacheFile = null;
             try
             {
                 MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
                 mDigest.update(url.getBytes());
                 final String cacheKey = bytesToHexString(mDigest.digest());
                 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
                 {
                     cacheFile = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "Android"
                            + File.separator + "data"
                            + File.separator + GiftSuggestions.this.getPackageName()
                            + File.separator + "cache"
                            + File.separator + "bitmap_" + cacheKey + ".tmp");              
                 }
             }
             catch (NoSuchAlgorithmException e) 
             {
                 // Oh well, SHA-1 not available (weird), don't cache bitmaps.
             }
             return cacheFile;
         }

         private String bytesToHexString(byte[] bytes) 
         {
             // http://stackoverflow.com/questions/332079
             StringBuffer sb = new StringBuffer();
             for (int i = 0; i < bytes.length; i++)
             {
                 String hex = Integer.toHexString(0xFF & bytes[i]);
                 if (hex.length() == 1) {
                     sb.append('0');
                 }
                 sb.append(hex);
             }
             return sb.toString();
         }

1 个答案:

答案 0 :(得分:0)

由于它是一个缓存,因此在获取之前总是有可能删除它 因此,检查文件是否存在以及文件是否存在,请使用它,否则从原始源获取它。

e.g。

File f = new File(path);  
if(f.exists()) {  
    //Use the file  
} else {  
    //Fetch from the original source  
}