0

I am currently working on an application that shows a Bitmap on an ImageView. Now the problem is, whenever trying to show a Bitmap larger than 4096x4096, it simply won't show up, stating that the image is too large to be shown. For example: I want to load up an image that's 4128x2322 pixels

I to resize it to be smaller than 4096x4096. I thought about something like this:

Bitmap bitmap;

    if(b.getHeight() >= 4096) {
        double f = b.getHeight() / 4096;
        b = Bitmap.createScaledBitmap(b, (int)(b.getWidth() / f), (int)(b.getHeight() / f), false);
    }else if(b.getWidth() >= 4096) {
        double f = b.getWidth() / 4096;
        b = Bitmap.createScaledBitmap(b, (int)(b.getWidth() / f), (int)(b.getHeight() / f), false);
    }
imageview.setImageBitmap(b);

Somehow it won't work... Any advices on how to scale properly?

Thanks in advance!

user2410644
  • 3,861
  • 6
  • 39
  • 59
  • A 4096x4096 image will consume 64MB of heap space as an `ARGB_8888` image. It is rather unlikely that you will have a free block big enough for that. There are also approximately zero devices at present with a screen that measures 4096 pixels on any axis. What are you planning on doing with this image, even if you are capable of loading it? – CommonsWare Dec 15 '14 at 18:44
  • `getHeight` is an int, which makes `b.getHeight() / 4096;` an int division. The result is typically 1 if your image is 4096 to 8192. – njzk2 Dec 15 '14 at 18:44
  • @CommonsWare I got an intent which takes a picture and returns the image - most likely it's that large. But to show the taken picture I need to scale it down first – user2410644 Dec 15 '14 at 18:47
  • 1
    Not exactly a duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object), but the answers will give you what you need. – Simon Dec 15 '14 at 18:49

2 Answers2

0

I got an intent which takes a picture and returns the image

That image will be stored somewhere else, which you will reference via a File or perhaps a content: Uri.

But to show the taken picture I need to scale it down first

Use BitmapFactory to load the bitmap. Use BitmapFactory.Options and its inSampleSize field to indicate how you want the image to be downsampled. This is covered in the documentation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

There is no device with resolution 4096X4096, your image going to be scaled any way. So instead trying to scale it up just top let the system to scale it down. How about taking the screen size in to calculation and provide the final scale by your self. Not only that you will save space but also improve performance.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216