Maria
Maria

Reputation: 31

Android load big images from external storage

I need to load lots of big images (500 .png files) from the SD card to my app. Do I always have to convert images to Bitmap and make Bitmap files? I don't want to resize the Heap. Is there another way to read the images from SD card?

Upvotes: 0

Views: 1240

Answers (3)

James
James

Reputation: 5159

If your image's dimension is very big, you must to resize them before loading in to ImageView. Otherwise, even one picture can easily cause out of memory problem. I don't know how many images you want to display concurrently and how big they are. But I suggest you to resize them before displaying them.

To resize image and show it, you can use this code:

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);

    //reduce the image size
    int imageWidth = bitmapOptions.outWidth;
    int imageHeight = bitmapOptions.outHeight;
    int scale = 1;
    while (imageWidth/scale >= screenWidth && imageHeight/scale >= screenHeight) {
        imageWidth = imageWidth / 2;
        imageHeight = imageHeight / 2;
        scale = scale * 2;
    }

    //decode the image with necessary size
    fileInputStream = new FileInputStream(cacheFile);
    bitmapOptions.inSampleSize = scale;
    bitmapOptions.inJustDecodeBounds = false;
    imageBitmap = BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);
    ImageView imageView = (ImageView)this.findViewById(R.id.preview);
    imageView.setImageBitmap(imageBitmap);

In my android project, I am using this piece of code to resize my HD wallpaper to review it.

Android Save And Load Downloading File Locally

Upvotes: 0

mxk
mxk

Reputation: 43524

If you're displaying them in a view, then you have to load them into memory in their entirety.

You didn't mention how large your images will get, but what we do in our photo gallery is to keep a list of SoftReferences to these bitmaps, so that the garbage collector can throw them away when they're not visible (i.e. when the view displaying them gets discarded--make sure that this actually happens, e.g. by using AdapterView). Combine this with lazy loading of these bitmaps and you should be good.

Upvotes: 1

DKIT
DKIT

Reputation: 3481

The internal representation of the image in your app is a collection of bits and bytes - not an image of any specific format (png, bmp, etc).

The image is converted to this internal representation when the image is loaded by the BitmapFactory.

It is usually not a good idea to load all the bitmaps at once, you will quickly run out of memory...

Upvotes: 0

Related Questions