7
votes

I have a file with a very large image: for example 9000x9000.

I can't load the Bitmap in memory because the heap size. But I only need to display a small part of this bitmap for example the rect width=100-200 and height =200-400 (resulting size of the sub-bitmap =100x200)

How can I retrieve this bitmap from the file?

Note: I dont want to lose quality in the 100x200 image

Thanks

6

6 Answers

17
votes

is it possible that there is a solution for this?

for example , BitmapRegionDecoder .

It should work for API10 and above...

Usage:

BitmapRegionDecoder.newInstance(...).decodeRegion(...)
4
votes

It can easily be done using RapidDecoder.

I actually generated a 9000x9000 png which its file size is about 80MB and the 200x400 sized region was successfully loaded.

import rapid.decoder.BitmapDecoder;

Bitmap bitmap = BitmapDecoder.from("big-image.png")
                             .region(145, 192, 145 + 200, 192 + 400)
                             .decode();
imageView.setImageBitmap(bitmap);

It works for Android 2.2 and above.

2
votes

I think that you can use the BitmapFactory method that allows you to specify the Rect that you want to decode.

public static Bitmap decodeStream (InputStream is, Rect outPadding, BitmapFactory.Options opts)
1
votes

Try this code:

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;
        if (height > reqHeight) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        }
        int expectedWidth = width / inSampleSize;
        if (expectedWidth > reqWidth) {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }
0
votes

I don't think you can. Not even on a PC, I fail to see how you could do that without loading the entire image: most image formats, for example PNGs, have the pixel data zipped, so you need to at least unzip the IDAT chunk before you can start doing anything else and that will basically decode the whole image.

In your shoes I would try to have a server do it for me. Where do you get the image anyway? Not from a server? Then try to make a WS request that will give you the proper part of the image. If the image does NOT come from the server you can nevertheless send it to your server to get back only the part of the image that you want.

0
votes

try this code:

private Bitmap decodeFile(File f) {
    Bitmap b = null;
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream fis = new FileInputStream(f);
        b=Bitmap.createBitmap(BitmapFactory.decodeStream(fis, null, o), 100, 200, 200, 400, null, null);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}

Am not sure, but this may give some idea to you