0

I am developing custom camera app and I am trying to crop the bitmap before saving it to file.

int subPhoto= new int[reqWidth * reqHeight]; 
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
bitmap.getPixels(subPhoto, 0, reqWidth, x, y, reqWidth, reqHeight);
bitmap = Bitmap.createBitmap(subPhoto, 0, reqWidth, reqWidth, reqHeight,  Bitmap.Config.ARGB_8888);
Utils.saveBitmapToFile(bitmap);

But here I have a problem. This line int subPhoto= new int[reqWidth * reqHeight]; throwing outofmemory error. Please let me know what I am doing wrong here

Praveena
  • 6,340
  • 2
  • 40
  • 53
  • you are assigning a very large int[] is what you are doing. more detail on the why and the how to prevent it would require more details from your part to begin with. – njzk2 Jan 12 '15 at 19:03
  • it depends on camera capabilities of the device. For ex it can be 2592*1944 – Praveena Jan 12 '15 at 19:03
  • why do you have the type int and create an array? – SorryForMyEnglish Jan 12 '15 at 19:03
  • also, what is it you are doing? you are decoding an image, then copying it, then recreating the same image to save it? what is the point? – njzk2 Jan 12 '15 at 19:04
  • 3
    @Praveen: 2592x1944 is a 20Mb array. no wonder it OOMEs. – njzk2 Jan 12 '15 at 19:05
  • @njzk2 `bitmap.getPixels` does the cropping from particular x,y coordinates. – Praveena Jan 12 '15 at 19:06
  • @njzk2 as I said in question I want to crop before saving – Praveena Jan 12 '15 at 19:07
  • 2
    then use http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap%28android.graphics.Bitmap,%20int,%20int,%20int,%20int%29 – njzk2 Jan 12 '15 at 19:08
  • @njzk2 Yes. it is very large array so I am finding an alternative to do the job – Praveena Jan 12 '15 at 19:08
  • possible duplicate of [Crop a Bitmap image](http://stackoverflow.com/questions/15789049/crop-a-bitmap-image) – njzk2 Jan 12 '15 at 19:10
  • @njzk2 thanks a lot. If you write that in answer I can accept it. Meanwhile what is the difference between those two methods and the file size of the saved bitmap is very less compared to my earlier version. Does that mean it is reducing the image quality? – Praveena Jan 12 '15 at 19:31
  • the file size depends on how you save it. images are usually compressed, this compression can be lossy or lossless, depending on the algorithm. – njzk2 Jan 12 '15 at 19:39

0 Answers0