2011-09-16 27 views
0

私のアプリでは、キャッシュメモリに画像を保存しています。しかし、次のコードを使用して次のエラーが発生しました。どのようにそれを処理する、誰も私を助けることができますか?そのキャッシュ以来FileNotFoundExceptionを処理する方法は?

例外

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(); 
     } 

答えて

0

、あなたがフェッチする前に、それが削除される可能性が常にあります。
したがって、ファイルの有無をチェックし、ファイルが存在する場合はそれを使用し、そうでない場合は元のソースからファイルをフェッチします。

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

しかし、私のアプリは正常に動作しています。それに何か問題がありますか? – naresh

関連する問題