Note that there are some explanatory texts on larger screens.

plurals
  1. POOutOfMemory Exception when creating Bitmap from OSM MapView
    primarykey
    data
    text
    <p>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.</p> <p>A picture says more than a thousand words so I drew this :</p> <p><img src="https://i.stack.imgur.com/JiZAk.png" alt="enter image description here"></p> <p>This is the method I use to get the bitmap :</p> <pre><code>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; } </code></pre> <p>And then :</p> <pre><code>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; } </code></pre> <p>The problem is in my first method, two times out of three, I have a OutOfMemoryException at this line :</p> <pre><code>b = Bitmap.createBitmap(mapview.getDrawingCache(), 0, ((height - width) / 2), width, width); </code></pre> <p>When the exception isn't thrown, it works well and my map is saved in the good format.</p> <p>I searched a lot but I didn't find a solution that work and resolve the OutOfMemory error on a createBitmap from a view.</p> <p>If you know a way to solve it, it would be great, ask me if you need more informations and sorry for my English.</p> <p>Thanks !</p> <p>Aenur56</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload