2011-07-28 3 views
4

누구든지이 코드에서 무슨 일이 일어나고 있는지 더 잘 알 수 있도록이 코드를 주석 처리 할 수 ​​있습니까?캐싱 비트 맵 코드가 혼동

감사합니다.

private void putBitmapInDiskCache(Uri url, Bitmap avatar) {  
File cacheDir = new File(context.getCacheDir(), "thumbnails"); 
File cacheFile = new File(cacheDir, ""+url.hashCode());  
try {  
    cacheFile.createNewFile();  
    FileOutputStream fos = new FileOutputStream(cacheFile);  
avatar.compress(CompressFormat.PNG, 100, fos);  
fos.flush();  
fos.close();  
} catch (Exception e) {  
Log.e(LOG_TAG, "Error when saving image to cache. ", e);  
} 
} 

은 비슷한을 읽으려면 :

fis = new FileInputStream(cacheFile); 
Bitmap local = BitmapFactory.decodeStream(fis); 

답변

7
/** 
* Write bitmap associated with a url to disk cache 
*/ 
private void putBitmapInDiskCache(Uri url, Bitmap avatar) {  
    // Create a path pointing to the system-recommended cache dir for the app, with sub-dir named 
    // thumbnails 
    File cacheDir = new File(context.getCacheDir(), "thumbnails"); 
    // Create a path in that dir for a file, named by the default hash of the url 
    File cacheFile = new File(cacheDir, ""+url.hashCode());  
    try {  
     // Create a file at the file path, and open it for writing obtaining the output stream 
     cacheFile.createNewFile();  
     FileOutputStream fos = new FileOutputStream(cacheFile); 
     // Write the bitmap to the output stream (and thus the file) in PNG format (lossless compression)  
     avatar.compress(CompressFormat.PNG, 100, fos); 
     // Flush and close the output stream  
     fos.flush();  
     fos.close(); 
    } catch (Exception e) { 
     // Log anything that might go wrong with IO to file  
     Log.e(LOG_TAG, "Error when saving image to cache. ", e);  
    } 
} 

두 번째 부분 :

// Open input stream to the cache file 
fis = new FileInputStream(cacheFile); 
// Read a bitmap from the file (which presumable contains bitmap in PNG format, since 
// that's how files are created) 
Bitmap local = BitmapFactory.decodeStream(fis); 
+0

감사합니다! 마지막 부분을 놓 쳤어. 파일 읽기에 대해서. –

+0

두 번째 부분을 포함하도록 업데이트 됨 – antlersoft

+0

두 번째 부분에서는 CachFile 부분에 어떤 내용이 들어 있습니까? 어떻게하면 cacheFile의 이름을 알 수 있습니까? –