Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - AsyncTask working with Bitmap - OutOfMemoryError
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/6815899/outofmemory-exception-when-handling-images">OutOfMemory Exception when handling images</a> </p> </blockquote> <p>I have a Bitmap creation within a AsyncTask, which goes like this:</p> <pre><code>private WeakReference&lt;Bitmap&gt; myBitmap; private WeakReference&lt;Bitmap&gt; endResultBitmap; private ImageView imv; </code></pre> <p>...</p> <pre><code>private class SendBitmap extends AsyncTask&lt;Integer, Void, Bitmap&gt; { public SendBitmap(Bitmap bitmap) { myBitmap = new WeakReference&lt;Bitmap&gt;(bitmap); } @Override protected Bitmap doInBackground(Integer... params) { Bitmap bm = null; bm = getBitmapFromNet(params[0]); return bm; } </code></pre> <p>And then I want to create Bitmap on which the received Bitmap would appear twice (one next to another)</p> <pre><code>protected void onPostExecute(Bitmap result) { endResultBitmap = new WeakReference&lt;Bitmap&gt;(Bitmap.createBitmap(result.getWidth() * 2, result.getHeight(), result.getConf())); Canvas canvas = new Canvas(endResultBitmap.get()); canvas.drawBitmap(result, 0, 0, null); canvas.drawBitmap(result, result.getWidth(), 0, null); imv.setImageBitmap(endResultBitmap); } </code></pre> <p>then I have my onCancelled() method:</p> <pre><code>@Override protected void onCancelled(Bitmap result) { if(endResultBitmap!=null) { endResultBitmap.recycle(); endResultBitmap = null; } } </code></pre> <p>The thing is that if I execute this AsyncTask couple of times, the heap grows as mad. I execute the AsyncTask when a button is pressed, but at first I do:</p> <pre><code>public void onClicked(View v) { if(asyncTaskInstance != null) asyncTaskInstance.cancel(); asynctaskInstance.execute(2); } </code></pre> <p>But again, the Heap grows as mad and at some point it will crash with <strong>OutOfMemoryError</strong>.</p> <p>Any idea? Do I have something wrong in my Design of the task?</p>
    singulars
    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.
 

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