从URL加载大位图并调整其大小 [英] Load large bitmap from url and resize it

查看:157
本文介绍了从URL加载大位图并调整其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从URL加载巨大的图像,并显示我的设备上。我读后这样做。

I want load huge image from url and show on my device. I read this post for doing this.

在这个例子中,他们使用本地绘制,但是我想从服务器获取图像。

In that example they use native drawable but I want get image from server.

我用这code

 private void getBitmap(ImageView imageView,final String url){

        mBitmapOptions = new BitmapFactory.Options();
        mBitmapOptions.inJustDecodeBounds = true;
        URL mUrl = null;
        InputStream is= null;
        try {
            mUrl = new URL(url);
            is = mUrl.openStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        BitmapFactory.decodeStream(is,null,mBitmapOptions);

        mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
                mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
        mBitmapOptions.inJustDecodeBounds = false;
        mBitmapOptions.inBitmap = mCurrentBitmap;
        mBitmapOptions.inSampleSize = 1;
        BitmapFactory.decodeStream(is,null,mBitmapOptions);
        imageView.setImageBitmap(mCurrentBitmap);
//        Bitmap croppedBitmap=Bitmap.createBitmap(Bitmap.createBitmap(mCurrentBitmap, 0, mCurrentBitmap.getHeight()/2, mCurrentBitmap.getWidth(), mCurrentBitmap.getHeight()/2));
//        imageView.setImageBitmap(croppedBitmap);
    }

我想从这里得到的网址图像并调整其大小,但我有一个例外。

I want here get image from url and resize it but I have an exception.

java.lang.IllegalArgumentException: width and height must be > 0

我做错了的是什么?或者你可以建议我更好的答案?

What I did wrong here? or could you suggest me better answer?

推荐答案

我会用毕加索,那么你想要达到的目标很简单,只要这样的:

I would use Picasso, then what you want to achieve is as simple as this:

Picasso.with(上下文)
  .load(URL)
  .resize(50,50)
  .into(ImageView的)

Picasso.with(context) .load(url) .resize(50, 50) .into(imageView)

http://square.github.io/picasso/

这篇关于从URL加载大位图并调整其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆