1

I want to add a business logo from a company to jpeg files [photos] in Android. I tried it several times to convert the photos into bitmaps and add then add the logo, it resulted in OutOfMemoryError.

EDIT: it works now, but take realy much time and memory... my code is that:

Bitmap logo = BitmapFactory.decodeResource(
            mainActivity.getResources(), R.drawable.logo);
    Bitmap photo =BitmapFactory.decodeByteArray(jpeg[0], 0, jpeg[0].length);

    Bitmap combination = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.RGB_565);

    Canvas comboImage = new Canvas(combination); 

    comboImage.drawBitmap(photo, 0f, 0f, null); 
    comboImage.drawBitmap(logo, 0f, 200, null); 



    try {
        FileOutputStream out = new FileOutputStream(mediaFile);
        combination.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Output from Logcat:

01-29 15:40:44.154: I/dalvikvm-heap(5065): Grow heap (frag case) to 109.067MB for 25560592-byte allocation


01-29 15:40:44.184: I/Choreographer(5065): Skipped 36 frames!  The application may be doing too much work on its main thread.

What do you suggest?

Thanks a lot. Lukas

lukas
  • 495
  • 4
  • 13
  • 1
    post some code and the error stack – ColdFire Jan 29 '14 at 14:28
  • possible duplicate of [How to combine multiple images into a single image in android?](http://stackoverflow.com/questions/15151937/how-to-combine-multiple-images-into-a-single-image-in-android) – njzk2 Jan 29 '14 at 14:49
  • What's the size of the imagens involved? Probably you're trying to load an image that doesn't fit into yours device's memory. When loading a jpeg image, it creates a bitmap that is bigger (in bytes) than your original jpeg. Try to do the same thing, but with smaller images. – Christian Jan 29 '14 at 15:04
  • @Christian: Yes with smaller images it works better. But these images are just created with the camera in this app, i don't want to scale these down. thats why I'm searching a better/faster/less memory using way. – lukas Jan 29 '14 at 15:12
  • I think you should use some Android NDK, where the 16Mb limit isn't there. – Christian Jan 29 '14 at 16:41

1 Answers1

1

If someone has the same problems, i suggest to read this, with its five Lessons Displaying Bitmaps Efficiently - Android Developers

Thanks all for help!

lukas
  • 495
  • 4
  • 13