简体   繁体   中英

Load large bitmap from url and resize it

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.

I am using this 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(context).load(url).resize(50, 50).into(imageView)

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

You get the exception because of:

// BitmapFactory.decodeStream(is,null,mBitmapOptions);

There are no mBitmapOptions set when you try to call:

mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth, mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);

Have a look at the answer in this question to further optimize your code: Getting the size of an image inputstream

I developped a solution based on http://developer.android.com/training/displaying-bitmaps/load-bitmap.html tutorial. Here is my code. The tricky part consist of using a byte array to store image data (because unfortunatly markSupported () is not available for connection.getInputStream() ).

    public static Bitmap getBitmapFromURL(final String imageUrl, final Options options, int reqWidth, int reqHeight) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        final InputStream input = connection.getInputStream();
        // using byte array to prevent open 2 times a stream
        final BufferedInputStream bis = new BufferedInputStream(input, 4*1024);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte)current);
        }
        bis.close();
        byte[] imageData = baf.toByteArray();
        if(options != null){
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options optionsSize = new BitmapFactory.Options();
            optionsSize.inJustDecodeBounds = true;


            BitmapFactory.decodeByteArray(imageData, 0, imageData.length, optionsSize);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(optionsSize, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            optionsSize.inJustDecodeBounds = false;

        }
        Bitmap myBitmap = null;
        if(options == null){
            myBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
        }
        else{
            myBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        }
        // close the stream;
        input.close();
        return myBitmap;
    } catch (Exception e) {
        return null;
    }
}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Hope it will helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM