简体   繁体   中英

Drawable from URL limited memory in android

I want go get image from URL and convert it into drawable. I have this method, that works fine:

public static Drawable getDrawableFromUrl(String url) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    x = BitmapFactory.decodeStream(input);
    return new BitmapDrawable(x);
}

Except one thing. When image is too big app crashes.

E/dalvikvm-heap(12750): 13142360-byte external allocation too large for this process.
E/dalvikvm(12750): Out of memory: Heap Size=6023KB, Allocated=3177KB, Bitmap Size=2563KB, Limit=20480KB
E/dalvikvm(12750): Trim info: Footprint=6023KB, Allowed Footprint=6023KB, Trimmed=952KB
E/GraphicsJNI(12750): VM won't let us allocate 13142360 bytes

Can I somehow increase the memory that I need for the image? Or maybe there is a way around this problem?

Loading Large Bitmaps Efficiently

the above link returns the re-sized bitmap from url, then you can convert to bitmap drawable.

Download and use the Bitmap fun sample application from that link.

You need to scale down the image or resize it as this link suggested

Loading Large Image

Here are some code snippet for this:

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
    if (width > height) {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    } else {
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }
}
return inSampleSize;
}

Just call this part when you set your drawable

MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mydrawable, 420, 620));

Ok. Here is the code that finaly works. As suggested I reduced the size of image using given function: calculateInSampleSize - thx Androyds.

public static Drawable getDrawableFromUrl(String url, Context context) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream inputStream = connection.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

    BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bufferedInputStream.mark(connection.getContentLength());
BitmapFactory.decodeStream(bufferedInputStream, null, options);
bufferedInputStream.reset();

options.inJustDecodeBounds = false;
options.inSampleSize = calculateInSampleSize(options, 400, 400);

x = BitmapFactory.decodeStream(bufferedInputStream, null, options);

bufferedInputStream.close();
connection.disconnect();

    BitmapDrawable y = new BitmapDrawable(x);

    return y;
}

I suggest you to save the file temporary on phone and read it and use it as a drawable. But if the phone cannot load the image, then you need to scale down the image from the sender or before opening it.

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