android – 缓存位图代码令人困惑

android – 缓存位图代码令人困惑,第1张

概述任何人都可以注释掉这段代码,让我更好地了解这段代码中发生的事情吗? 谢谢. private void putBitmapInDiskCache(Uri url, Bitmap avatar) { File cacheDir = new File(context.getCacheDir(), "thumbnails"); File cacheFile = new File(cac 任何人都可以注释掉这段代码,让我更好地了解这段代码中发生的事情吗?

谢谢.

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);
解决方法
/** * 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,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,e);         }  }

第二部分:

// Open input stream to the cache filefis = 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);
总结

以上是内存溢出为你收集整理的android – 缓存位图代码令人困惑全部内容,希望文章能够帮你解决android – 缓存位图代码令人困惑所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存