加载大图片内存溢出 解决方案

加载本地图片使用BitmapFactory.decodeFile

加载网络图片使用BitmapFactory.decodeStream

 

    Options opts = new Options();
        opts.inJustDecodeBounds = true;//
        BitmapFactory.decodeStream(is, null, opts);

Options opts = new Options();
opts.inJustDecodeBounds = true; //设置为true, 加载器不会返回图片, 而是设置Options对象中以out开头的字段.即仅仅解码边缘区域
BitmapFactory.decodeFile(filePath, opts);

// 得到图片的宽和高
int imageWidth = opts.outWidth;
int imageHeight = opts.outHeight;

// 获取屏幕的宽和高
Display display = this.getWindowManager().getDefaultDisplay(); // 获取默认窗体显示的对象
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

// 计算缩放比例
int widthScale = imageWidth / screenWidth;
int heightScale = imageHeight / screenHeight;

int scale = widthScale > heightScale ? widthScale:heightScale;
 // 指定加载可以加载出图片.
opts.inJustDecodeBounds = false;
// 使用计算出来的比例进行缩放
opts.inSampleSize = scale;
Bitmap bm = BitmapFactory.decodeFile(path, opts);

 

 

 

  
/**
   * 计算图片的缩放比例
   * @return
   */
  public int getScare() {
    try {
      HttpClient client = new DefaultHttpClient();
      HttpGet httpGet = new HttpGet(imageUrl);
      HttpResponse response = client.execute(httpGet);
      int code = response.getStatusLine().getStatusCode();

      if (200 == code) {
        InputStream is = response.getEntity().getContent();
        Options opts = new Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, opts);

        int imageWidth = opts.outWidth;
        int imageHeight = opts.outHeight;

        Display display = ImageActivity.this.getWindowManager()
            .getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        int widthscale = imageWidth / screenWidth;
        int heightscale = imageHeight / screenHeight;
        int scale = widthscale > heightscale ? widthscale : heightscale;

        return scale;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 1;//网络连接失败时默认返回1
  }
  /**
     * 获取网络图片
     */
    protected void getNetImage() {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(imageUrl);
            HttpResponse response = client.execute(httpGet);
            int code = response.getStatusLine().getStatusCode();

            if (200 == code) {
  InputStream is = response.getEntity().getContent();

  Options opts = new Options();
  
  //根据计算出的比例进行缩放
  int scale = getScare();
  opts.inSampleSize = scale;
   
  Bitmap bm = BitmapFactory.decodeStream(is, null, opts);

  //将bm发生给主线程用于显示图片,更新UI
  Message msg = Message.obtain();
  msg.obj = bm;
  handler.sendMessage(msg);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
//显示图片
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            Bitmap bm = (Bitmap) msg.obj;
            iv.setImageBitmap(bm);
        };
    };

转载于:https://my.oschina.net/u/1377520/blog/726605

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值