Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 缓存位图代码_Android - Fatal编程技术网

Android 缓存位图代码

Android 缓存位图代码,android,Android,有谁能注释掉这段代码,让我更好地了解这段代码中发生了什么 谢谢 private void putBitmapInDiskCache(Uri url, Bitmap avatar) { File cacheDir = new File(context.getCacheDir(), "thumbnails"); File cacheFile = new File(cacheDir, ""+url.hashCode()); try { cacheFile.

有谁能注释掉这段代码,让我更好地了解这段代码中发生了什么

谢谢

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); 
第二部分:

// 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); 

更新为包含第二部分在第二部分中,CachFile部分中包含什么?我如何知道缓存文件的名称?大概该文件的名称仍然是url的哈希,字符串cacheFile=“”+url.hashCode();/“+int是将int转换为string的缩写。您能回答这个问题吗?必须添加以下代码行。如果(!cacheDir.exists())cacheDir.mkdir();
// 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);