Sinh Phan
Sinh Phan

Reputation: 1266

Android: Load large image from storage into ImageView

I'm doing a gallery image. I get path of image in device and parse to URI. Then I use Picasso Android Lib to load image into Imageview in Gridview. It's work fine until have a large image. Picasso can not load large image. I got error Out Of Memory. Is there any suggestion to load large image into ImageView? And have any lib to load image into ImageView can instead Picasso?

Upvotes: 0

Views: 1956

Answers (4)

Sinh Phan
Sinh Phan

Reputation: 1266

I found ImageLoader lib for my problem. It works fine. I tested on my project, and then I saw that ImageLoader looks better than Picasso.

Upvotes: 1

Amirhossein Naghshzan
Amirhossein Naghshzan

Reputation: 1180

You can custom recent-images library for your purpose. It's very simple and easy to use library. It creates a thumbnail of image for showing in gridview and then in click opens the original.

Upvotes: 0

Geert Berkers
Geert Berkers

Reputation: 731

Here is an example I once used, but it is not perfect! (Sorry)

You can reduce the bitmap size:

public Bitmap resizeBitmap(Bitmap bitmap) {
    if (bitmap.getHeight() > 4096 || bitmap.getWidth() > 4096) {
        int width = (int) (bitmap.getWidth() * 0.9);
        int height = (int) (bitmap.getHeight() * 0.9);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        resizeBitmap(resizedBitmap);
        returnresizedBitmap;
    } else {
        return bitmap;
    }
}

If I should do it again: (not tested)

public Bitmap resizeBitmap(Bitmap bitmap) {
    int originalWidth = bitmap.getWidth();
    int originalHeight = bitmap.getHeight();

    if (originalWidth > 4096 || originalHeight  > 4096) {

        int height;
        int width;

        if(originalHeight > originalWidth) {
            height = 4096;
            width  = originalWidth  / (originalHeight / 4096);
        } else {
            width  = 4096;
            height = originalHeight / (originalWidth / 4096);
        }

        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        return resizedBitmap;
    } else {
        return bitmap;
    }
}

Upvotes: 0

alkber
alkber

Reputation: 1436

Learn how to use common techniques to process and load Bitmap objects in a way that keeps your user interface (UI) components responsive and avoids exceeding your application memory limit. If you're not careful, bitmaps can quickly consume your available memory budget leading to an application crash due to the dreaded exception: java.lang.OutofMemoryError: bitmap size exceeds VM budget.

There are a number of reasons why loading bitmaps in your Android application is tricky:

Mobile devices typically have constrained system resources. Android devices can have as little as 16MB of memory available to a single application. The Android Compatibility Definition Document (CDD), Section 3.7. Virtual Machine Compatibility gives the required minimum application memory for various screen sizes and densities. Applications should be optimized to perform under this minimum memory limit. However, keep in mind many devices are configured with higher limits.

Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices. Android app UI’s frequently require several bitmaps to be loaded at once. Components such as ListView, GridView and ViewPager commonly include multiple bitmaps on-screen at once with many more potentially off-screen ready to show at the flick of a finger.

Read More Regarding this issue

Upvotes: 0

Related Questions