开发者

How to handle FileNotFoundException?

In my app i am storing images in the cache memory. But i got the following error using the following code. How to handle it, can anybody help me?

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)

Code

{

 ......

 final File file = getCacheFile(imageUrl);

 file.getParentFile().mkdirs(); 

 file.createNewFile();

 ......

}

      public File getCacheFile(String url) 
         {
             // First compute the cach开发者_StackOverflowe 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();
         }


Since its a cache , there is always a chance that it is deleted before you fetch.
Therefore you check for file existence and if file exists, use it otherwise fetch it from the original source.

e.g.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜