2

I have a problem when creating a bitmap from a MapView (osmdroid). This map takes almost the entire screen of a phone (always in portrait mode). I want to create a bitmap from a square of the center of the displayed map (side size : the screen width) and save it in a file to use it after in my app.

A picture says more than a thousand words so I drew this :

enter image description here

This is the method I use to get the bitmap :

public static Bitmap loadBitmapFromMapView(final MapView mapview, final int width, final int height) {
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    mapview.setDrawingCacheEnabled(true);
    b = Bitmap.createBitmap(mapview.getDrawingCache(), 0, ((height - width) / 2), width, width);
    mapview.setDrawingCacheEnabled(false);

    return b;
}

And then :

public static File saveBitmapAsFile(final Bitmap bmp) {
    if (mSaveDirectory == null || !mSaveDirectory.exists()) {
        final ContextWrapper cWrapper = new ContextWrapper(getApplicationContext());
        mSaveDirectory = cWrapper.getFilesDir();
        if (!mSaveDirectory.exists()) {
            mSaveDirectory.mkdirs();
        }
    }
    try {
        final String imageName = "Image_" + System.currentTimeMillis() + ".png";
        final File file = new File(mSaveDirectory, imageName);
        final FileOutputStream out = getApplicationContext().openFileOutput(imageName, Context.MODE_WORLD_READABLE);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
        return file;
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return null;
}

The problem is in my first method, two times out of three, I have a OutOfMemoryException at this line :

b = Bitmap.createBitmap(mapview.getDrawingCache(), 0, ((height - width) / 2), width, width);

When the exception isn't thrown, it works well and my map is saved in the good format.

I searched a lot but I didn't find a solution that work and resolve the OutOfMemory error on a createBitmap from a view.

If you know a way to solve it, it would be great, ask me if you need more informations and sorry for my English.

Thanks !

Aenur56

Pidikan
  • 303
  • 1
  • 11
  • This question is asked several times a day. All of the possible answers have already been given. Search for out of memory error here and you will find hundreds of questions and answers. – Simon Oct 30 '13 at 22:15
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Simon Oct 30 '13 at 22:15
  • All topics I found were about bitmap get from files. I didn't found anything relevant about getting bitmap from a view :/ – Pidikan Oct 30 '13 at 22:34
  • possible duplicate of [Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap](http://stackoverflow.com/questions/11820266/android-bitmapfactory-decodestream-out-of-memory-with-a-400kb-file-with-2mb-f) – Devrim Oct 30 '13 at 22:38
  • I already did those improvements for bitmap loading and displaying, but my problem is with bitmap generation from a view with method "Bitmap.createBitmap" and I don't find anything concrete on that subject... It's not a duplicate the two subjects you gave. – Pidikan Nov 06 '13 at 13:47
  • have you tried to dispose the created save-as-bitmap as soon as it is not necessary any more as decribed in http://stackoverflow.com/a/20920832/519334 – k3b Mar 23 '15 at 15:47

0 Answers0