Addev
Addev

Reputation: 32243

Decode a part of Bitmap from file in Android

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

Upvotes: 6

Views: 7046

Answers (6)

android developer
android developer

Reputation: 116412

is it possible that there is a solution for this?

for example , BitmapRegionDecoder .

It should work for API10 and above...

Usage:

BitmapRegionDecoder.newInstance(...).decodeRegion(...)

Upvotes: 17

Roshan kumar
Roshan kumar

Reputation: 21

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);
    }

Upvotes: 1

suckgamony
suckgamony

Reputation: 804

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.

Upvotes: 4

user1395885
user1395885

Reputation: 56

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

Upvotes: 0

Simone Casagranda
Simone Casagranda

Reputation: 1215

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)

Upvotes: 2

Alexandru Cristescu
Alexandru Cristescu

Reputation: 3948

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.

Upvotes: 0

Related Questions