图片查看器

来源:互联网 发布:网络语老铁是什么意思 编辑:程序博客网 时间:2024/05/21 15:02

只要做耗时的操作,就开一个子线程.

1:把流信息转换成bitmap对象

2:使用工厂类bitmapfactory.decodestream(inputstream is)

3:访问权限 


缓存逻辑:

1:创建文件输出流fileoutputstream,在第一次读取的时候写入到cache目录下,使用getcacahedir()方法,(存储不太重要的信息getfiledir()存储重要的信息,清空缓存的时候不会删除),在这里出现了一个问题,

因为第一次的时候要完成两个方法:一个是缓存,一个是把数据传给UI,这时Bitmap bf = BitmapFactory.decodeStream(is);不可行.因为在while循环中指针位置已经指向末尾,而decodestream底层也是while循环实现,而此时指针已经指向末尾,导致没有输入数据,应使用final Bitmap bf = BitmapFactory.decodeFile(file.getAbsolutePath());方法.

2:在动作开始时判断是否有缓存

if(file.exists() && file.length() > 0) {final Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());//如果只是更新ui,可以用runonuithread()runOnUiThread(new Runnable() {public void run() {iv.setImageBitmap(cacheBitmap);Toast.makeText(getApplicationContext(), "缓存", 0).show();}});


原创粉丝点击