谢谢.
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 – 缓存位图代码令人困惑所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)