0

I'm trying to scale down an image with a code like this:

        bmp = BitmapFactory.decodeFileDescriptor(getContentResolver().openAssetFileDescriptor(imageUri, "r").getFileDescriptor());
        Pair<Integer, Integer> dims = getScaledDimensions(longerSide, bmp.getWidth(), bmp.getHeight());
        Bitmap smallBmp = Bitmap.createScaledBitmap(bmp, dims.first, dims.second, true);
        ByteArrayOutputStream bastream = new ByteArrayOutputStream();
        smallBmp.compress(Bitmap.CompressFormat.JPEG, 85, bastream)
        barray = bastream.toByteArray();

However with bigger image (8px camera photo scaled to 1920x1440) output is "corrupted" as you can see below (few pixels followed by white area). Small images are sometimes processed correctly. This is issue on the Nexus 5, 5.0.1 phone and emulator.

I've elaborated with image size, disabled some relatively expensive memory operations of my app etc, but nothing helped.

Does anyone have an idea what's the cause? Thank you.

Image wrongly scaled

Dežo
  • 51
  • 4

2 Answers2

0

read a big image into memory is a terrible thing.

you should decode Bounds first, use BitmapFactory.Options inJustDecodeBounds;

then calculate the proper proportion,

finally you load the prescaled image ( use BitmapFactory.Options inSampleSize) to memory to do your scale work.

the samples are easy found .

0

Your image is of large resolution. And for your I would like to tell you that in android 1px=4byte of memory.

With this you can imagine how much it is taking.

Check if this link for loading large bitmap efficiently

http://developer.android.com/training/displaying-bitmaps/index.html

Fahim
  • 12,198
  • 5
  • 39
  • 57
  • Fahim, @davidleen29 guys I've of course read given documentation. What you are indirectly saying is, that after one takes image from the camera, he can't process it otherwise, than scaling it down during the load. Yes 8Mpx image may take 40MB of memory, but at least N5 has 2G of ram, and app memory limits somewhere about 512MB. [source] (http://stackoverflow.com/a/9940415/326731) – Dežo Mar 12 '15 at 15:08