Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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/

Solution 2:

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

Solution 3:

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()).

publicstatic Bitmap getBitmapFromURL(final String imageUrl, final Options options, int reqWidth, int reqHeight) {
    try {
        URLurl=newURL(imageUrl);
        HttpURLConnectionconnection= (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        finalInputStreaminput= connection.getInputStream();
        // using byte array to prevent open 2 times a streamfinalBufferedInputStreambis=newBufferedInputStream(input, 4*1024);
        ByteArrayBufferbaf=newByteArrayBuffer(50);
        intcurrent=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 dimensionsfinal BitmapFactory.OptionsoptionsSize=newBitmapFactory.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;

        }
        BitmapmyBitmap=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) {
        returnnull;
    }
}

privatestaticintcalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of imagefinalintheight= options.outHeight;
    finalintwidth= options.outWidth;
    intinSampleSize=1;

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

        finalinthalfHeight= height / 2;
        finalinthalfWidth= 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.

Post a Comment for "Load Large Bitmap From Url And Resize It"