zoukankan      html  css  js  c++  java
  • 异步下载图片并对对图片进行压缩处理

         做网络编程这块,特别是做网站这方面的app需要处理大量的图片,即使把图片下载到本地有时候也会出现内存溢出,这时候就要考虑把图片压缩或者使用略缩图,这样能更好的避免因图片过大而造成内存溢出。

        在33guide中,我的图片全部都是从网站上获取的,所以需要通过获取的路径进行异步下载,在页面上可以先弄一张默认图片,在后台更新图片也就是边下载边更新图片。

     1         //异步加载图片(这边我传了两个参数1、是ImageView对象,2、图片的路径)
     2         private void loadImg(ImageView timg, String path) {
     3 
     4             asynImageload as=new asynImageload(timg);
     5             as.execute(path);//执行
     6         }
     7 
     8         private final class asynImageload extends AsyncTask<String, Integer, Uri>{
     9 
    10             private ImageView imgview;
    11             public asynImageload(ImageView imgview) {
    12                 super();
    13                 this.imgview=imgview;
    14             }
    15 
    16             @Override
    17             protected Uri doInBackground(String... params) {
    18                 try {
    19                     return ProductService.getImageload(params[0],cache);//cache是我设置的在sd卡上的一个包,就是将图片下载到该包下面,getImageLoad写得方法
    20                 } catch (Exception e) {
    21                    e.printStackTrace();
    22                 }
    23                 return null;
    24             }
    25 
    26             @Override
    27             protected void onPostExecute(Uri result) {
    28              
    29                 if(result!=null&&imgview!=null){
    30                     //本篇两点就在这里了,这里就是处理图片溢出问题的方法,使用的就是BitmapFactory
    31                     BitmapFactory.Options options = new BitmapFactory.Options(); 
    32                     options.inSampleSize=2;//图片的高和宽都为原来的二分之一
    33             
    34                     ContentResolver cr = getContentResolver();//通过当前Activity获取COntentResolver实例
    35                     Bitmap bitmap=null;
    36                      try {
    37                         bitmap = BitmapFactory.decodeStream(cr.openInputStream(result), null, options);//采用流的形式写入
    38                     } catch (FileNotFoundException e) {
    39                         bitmap=BitmapFactory.decodeFile(result.toString(), options);
    40                     }             
    41                     imgview.setImageBitmap(bitmap);//更新图片//保存数据        
    42                     //这里是图片内存回收,不知怎么,有问题,所以我就注释了它
    43                     //if(!(bitmap.isRecycled())){
    44                     //bitmap.recycle();//回收内存的图片
    45                     //    System.gc();//提醒系统回收数据
    46                      //    }
    47                     
    48                 }else{
    49                     imgview.setImageResource(R.drawable.mouren);//使用默认图片
    50                 }    
    51             }    
    52         }

    下载图片的方法写在了另一个类中:如、ProductService

     1 public class ProductService {
     2      //此处使用了MD5加密、主要是处理图片絮乱问题,也就是说图片对不上号 
     3     //获取网络图片,如果图片处于缓存之中,就返回该图片,否则从网络中加载该图片不能够缓存起来
     4     public static Uri getImageload(String path,File cachedir) throws Exception{
     5         File localfile = new File(cachedir,Md5.getMD5(path)+path.substring(path.lastIndexOf(".")));//根据图片的路径,使用md5对它加密
     6         if(localfile.exists()){
     7             return Uri.fromFile(localfile);//判断图片是否存在,存在则返回该路径,不存在则开始下载图片
     8         }else{
     9             HttpURLConnection conn=(HttpURLConnection)new URL(path).openConnection();
    10             conn.setConnectTimeout(5000);//设置响应时间
    11             conn.setRequestMethod("GET");//设置请求方式
    12             if(conn.getResponseCode()==200){
    13                 FileOutputStream outstream=new FileOutputStream(localfile);
    14                 InputStream inputstream=conn.getInputStream();
    15                 byte[] buffer=new byte[1024];
    16                 int len=0;
    17                 while((len=inputstream.read(buffer))!=-1){
    18                     outstream.write(buffer, 0, len);
    19                 }
    20                 inputstream.close();
    21                 outstream.close();
    22                 return Uri.fromFile(localfile);
    23             }
    24         }
    25         return null;
    26     }    
    27 }
    经验的积累在于平时的点滴、
  • 相关阅读:
    360给腾讯造的盗梦空间
    C 语言 运算符优先级
    CorelDraw, Adobe Illustrator 转换到 Photoshop 形状路径
    用户体验经典解释
    禁用Windows XP的自动播放功能
    ObjectiveC ARC下的内存管理(一)
    ARC下的内存管理(二)对象及成员的引用关系
    天天撞墙
    PS: 操作不实时显示的解决办法
    摩托罗拉 Milestone新手刷机教程
  • 原文地址:https://www.cnblogs.com/yrhua/p/3414567.html
Copyright © 2011-2022 走看看