2

I have the same problem found here and I do not know if now there is a a solution. I have a Bitmap image of 2592x1944 pixels and, when I run Bitmap.decodeFile(...) the application crashes cause OutOfmemory Exception.

Does anyone of you know how to solve it?

Thank you very much

Here below my code:

for (int iFile = 0; iFile < files.length; iFile++) {
     if (files[iFile].exists()) {
         bitmap = BitmapFactory.decodeFile(files[iFile].getAbsolutePath());
         int[][][] rgb = Utils.getImageRgb(bitmap);

         indexLastFile = iFile + 1;
         images.put("M" + (iFile + 1), rgb);
      }
}
Community
  • 1
  • 1
Paride Letizia
  • 330
  • 1
  • 4
  • 23

2 Answers2

1

If you really want the bitmap alone, try to load image efficiently, You need to scale the image before even loading the image into ram memory.

https://www.youtube.com/watch?v=12cB7gnL6po follow this video series to load bitmap efficiently.

Sujith Niraikulathan
  • 2,111
  • 18
  • 21
  • No man. I do not want to scale image because scaling is a pixels average and I need all pixels – Paride Letizia Apr 05 '17 at 09:56
  • Can you explain why you need bitmap with no loss in pixels, so that we can provide you a better solution?? – Sujith Niraikulathan Apr 05 '17 at 10:20
  • Because I have to extract RGB color for each pixel in the image, so if I scale the image, the pixels will be moved and I won't able to get the same output, so I have to maintain the same pixels – Paride Letizia Apr 05 '17 at 10:24
  • Great! But loading a bitmap fully is very bit and getting the rgb out of it is like doubling the size of bitmap in RAM, So can you explain why you need the pixel colors?? – Sujith Niraikulathan Apr 05 '17 at 10:26
  • In order to extract some info putting masks and get some parameters as area, volume, depth etc. I really need pixels color – Paride Letizia Apr 05 '17 at 12:33
  • You can get the info part by part, but not entirely. So you can get part of the bitmap without scaling by calling Bitmap bitmap = BitmapRegionDecoder.decodeRegion(rect, null);, rect defines the bounds of the image you want to load. From example if you have 400x400 image, just load 100x100 part of the image and extract info and store it in disk or db. Now clear the variable and load next 100x100 part of the image to extract info. You cannot load the entire bitmap or you cannot have the entire bitmap pixel RGB in your app memory(RAM). – Sujith Niraikulathan Apr 05 '17 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139980/discussion-between-paride-letizia-and-sujith-niraikulathan). – Paride Letizia Apr 05 '17 at 13:43
  • Not see the conversation – Sujith Niraikulathan Apr 05 '17 at 17:59
0

You should avoid playing with bitmaps otherwise you have to face OutOfMemory exceptions.

Try image libraries Picasso or Glide to avoid out of memory issues & better performance.

Replace path with local folder path or server url

Glide.with (context).load (path).asBitmap().into(imageView);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • 1
    How does this help? Be specific (Bitmap.Config, target size, etc.) – Eugen Pechanec Apr 05 '17 at 09:20
  • Can you post your code so that I can update it in the way you need it – PEHLAJ Apr 05 '17 at 09:23
  • @Pehlaj please post your code how to decode a file without to put it in a imageView – Paride Letizia Apr 05 '17 at 09:25
  • ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filesPath), THUMBSIZE, THUMBSIZE))Why do you need to decode file to bitmap? – PEHLAJ Apr 05 '17 at 09:27
  • @Pehlaj I have a java class which take a 5MPX image from an external cam and transforms it, or it should do, in a map[width][height][3] of RGB. Cause this algorithm has to run in an Android device, a Bitmap is necessary but when I run Bitmap.decodeFile() the app crashes – Paride Letizia Apr 05 '17 at 09:33
  • You should avoid playing with bitmaps otherwise you have to face OutOfMemory exceptions. – PEHLAJ Apr 05 '17 at 09:37
  • @Pehlaj sure. You can find it above. Could you give me your email so I can write down my full issue? – Paride Letizia Apr 05 '17 at 09:39