0

I want to load a bitmap image with universal loader, but the Bitmap constructor is not accept, as I transform into a valid bitmap to the constructor of Universal Image Loader URI.

ImageLoader.getInstance().displayImage(mImagesBitmap[position]), imageView, options);

which is the correct way?

an essential piece of my code, this is called multiple times.

int windowDrawable = R.drawable.window_arch;
int resultBitmap = R.drawable.result;

Bitmap windowBitmap= BitmapFactory.decodeResource(getResources(), windowDrawable);

Bitmap editedBitMap= BitmapFactory.decodeResource(getResources(), resultBitmap);

Bitmap finalBitmap =  bitmap_overlay(windowBitmap, editedBitMap) ;
editedBitMap.recycle();
windowBitmap.recycle();

finalBitmap  . 
public static bitmap_overlay(Bitmap bmp1, Bitmap bmp2)
    { 
     try 
     { 
       int maxWidth = (bmp1.getWidth() > bmp2.getWidth() ? bmp1.getWidth() : bmp2.getWidth());
       int maxHeight = (bmp1.getHeight() > bmp2.getHeight() ? bmp1.getHeight() : bmp2.getHeight());
       Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight,  bmp1.getConfig());
       Canvas canvas = new Canvas(bmOverlay);
       canvas.drawBitmap(bmp1, 0, 0, null);
       canvas.drawBitmap(bmp2, 0, 0, null);
       return bmOverlay;

     } catch (Exception e)
     { 

      e.printStackTrace();
      return null; 
     } 
    } 


setWindowBackgroud(BitmapFactory.decodeResource(mContext.getResources(), windowDrawable));
Gilberto Ibarra
  • 2,849
  • 2
  • 27
  • 38

1 Answers1

0

You can't display a bitmap using the image loader. You can only supply a valid URI (file location, drawable, etc.). So if you really want to display it using the image loader, you need to save it down first.

Since you have the bitmap, why are you not simply calling this?

imageView.setImageBitmap(mImagesBitmap[position]);

Update:

The question changed to why you're getting OOM exception and how to resolve it without losing quality.

You can refer to this answer I previously posted dealing with many full screen png to avoid OOM but option 2 will not apply in your case.

The issue here is that you want to edit these images and don't want to lose quality. Because of this, loading them with a lower resolution may not be an option (although you should still consider loading them in a smaller resolution if possible). Also try to load your bitmaps with the RGB_565 flag which should save you 50% of the memory unless your images have an alpha channel (transparency).

Your only option here may be to set the largeHeap to true (see link posted) and even if that doesn't work, then you're out of options.

Generally speaking, you should remember:

  1. By default, Android will use 4 bytes per pixel to load an image to bitmap. This is irrespective of the file size and compression technologies used.

  2. You should try to load the images with as low a resolution as you can realistically get away with. This can depend on the device size, the quality you want the images to be in and various other heuristics.

  3. You should not load more than you need in memory. This means, if you're displaying only one image at the time to edit, you should only load this image into memory. Loading them in memory and saving them in an array is a big no.

I understand for an image editing app, #2 is not always an option but you may have to consider it if you run into memory problems even if you use largeHeap. You should also make sure you do not store them in an array in any shape or form. As soon as you're done with editing them, discard the old version and only have one loaded into memory at any given time.

Memory is the most precious asset when it comes to Android development. You're given a very limited amount and are forced to work with what's given to you. There's a way to request larger memory but even that's limited so you still must ensure you only load what you need and discard what you don't.

Community
  • 1
  • 1
kha
  • 19,123
  • 9
  • 34
  • 67
  • I dont use setImagebitmap because i have OutOfMemoryException , i have at least 1000 large bitmaps that changes every click. – Gilberto Ibarra Mar 24 '15 at 18:57
  • 1
    Hmm. So you can load 1000 large bitmaps into an array but can't display them using setImageBitmap? That doesn't sound correct. If the source of your images are files or some web url, you should use the universal image loader to load them using the URI instead of loading them into memory first (in your bitmap array) and then calling it. – kha Mar 24 '15 at 18:58
  • I have aprox 1000 .png files with at least 750 kbs.. these are converted to bitmaps to be edited multiple times with one click at runtime and then be displayed again. – Gilberto Ibarra Mar 24 '15 at 19:02
  • That's fine. What I don't understand is that why would you think showing them in the image view would cause an OOM exception and that showing them with Universal Image Loader wouldn't? You may need to revisit your architecture and think about how you are working with these images. You should probably only operate on one bitmap at a time, load it, edit it and then display it after your edits. Since you have the bitmap object loader, you should just call setImageBitmap() on the image view. UIL won't allow you to load a bitmap since its API doesn't support that function. Moreover, even if it... – kha Mar 24 '15 at 19:06
  • [cont] did, it wouldn't solve any memory issues for you since at the end of the day, it would eventually call the exact method on the image view. – kha Mar 24 '15 at 19:06
  • ok, but still I have the problem with OOM, i merge multiple bitmaps ant runtime, what i could do without losing quality of these images. – Gilberto Ibarra Mar 24 '15 at 19:14
  • i lost transparency with RGB_565 ... all the images have transparency – Gilberto Ibarra Mar 24 '15 at 19:55
  • Yep. in that case you have to use the default unfortunately. Can you paste some of your code which loads and edits the images so that I can have a look how you do it and if it can be improved? With the amount of detail you've given in your question, it's very hard to help. – kha Mar 24 '15 at 19:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73696/discussion-between-gilberto-ibarra-and-kha). – Gilberto Ibarra Mar 24 '15 at 20:08