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