title: Saving bitmaps to ArrayList makes Bitmaps whole black
body: <p>I am trying to save bitmaps in an ArrayList. I generate 2 types of bitmaps, they are basically identical - drawn with a finger by the user.</p>
  <ul>
  <li>The first bitmap is fully visible in activity</li>
  <li>The second is stored dynamically in the ArrayList - it is a slice of the last x milliseconds from the visible bitmap - It will be further converting and sending by BT.</li>
  </ul>
  <p>The problem is that the last bitmap in the ArrayList is well saved, while each previous bitmap turns into a black rectangle when you add another one.</p>
  <p>Relevant code snippets:</p>
  <pre><code> private ArrayList&lt;Bitmap&gt; bitmapsListToSend = new ArrayList&lt;&gt;();
          private ArrayList&lt;Canvas&gt; canvasListToSend = new ArrayList&lt;&gt;();
  
  mBitmapToShow = Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565);
              mCanvasToShow = new Canvas(mBitmapToShow);
  
              bitmapsListToSend.add(Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565))
  //mCanvasToSend = new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1));
  
  canvasListToSend.add(new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1)));;
  </code></pre>
  <p>I tried to make separate Canvas to each bitmap without success</p>
  <pre><code>public class PaintView extends View {
  
  private final Paint mPaintToShow = new Paint();
  private Bitmap mBitmapToShow;
  private Canvas mCanvasToShow;
  
  private final Paint mPaintToSend = new Paint();
  //private Canvas mCanvasToSend;
  
  
  private TouchPoint startPoint;
  
  @Nullable
  private ArrayList&lt;Bitmap&gt; bitmapsListToSend = new ArrayList&lt;&gt;();
  private ArrayList&lt;Canvas&gt; canvasListToSend = new ArrayList&lt;&gt;();
  
  private int bitmapCounter = 0;
  private int alpha = 1;
  
  /* debug */
  Stopwatch watch = Stopwatch.createUnstarted();
  
  public PaintView(Context c) {
      super(c);
      setFocusable(true);
      mPaintToShow.setAntiAlias(true);
      mPaintToShow.setColor(BLACK);
      mPaintToShow.setAlpha(alpha);
      mPaintToShow.setStrokeJoin(Paint.Join.ROUND);
      mPaintToShow.setStrokeCap(Paint.Cap.ROUND);
  
      mPaintToSend.setAntiAlias(true);
      mPaintToSend.setColor(BLACK);
      mPaintToSend.setStrokeJoin(Paint.Join.ROUND);
      mPaintToSend.setStrokeCap(Paint.Cap.ROUND);
  
      mBitmapToShow = Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565);
      mCanvasToShow = new Canvas(mBitmapToShow);
  
      bitmapsListToSend.add(Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565));
      //mCanvasToSend = new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1));
      canvasListToSend.add(new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1)));
  
  }
  
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      mCanvasToShow.drawColor(Color.WHITE);
      //mCanvasToSend.drawColor(Color.WHITE);
      canvasListToSend.get(canvasListToSend.size()-1).drawColor(WHITE);
  }
  
  public void clear() {
      if (mCanvasToShow != null) {
          mPaintToShow.setColor(Color.WHITE);
          mCanvasToShow.drawPaint(mPaintToShow);
          //mCanvasToSend.drawPaint(mPaintToShow);
          bitmapCounter=0;
          alpha=1;
          bitmapsListToSend.clear();
          canvasListToSend.clear();
          mPaintToShow.setColor(BLACK);
          watch.reset();
          invalidate();
      }
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      if (mBitmapToShow != null) {
          canvas.drawBitmap(mBitmapToShow, 0, 0, null);
          //mCanvasToSend.drawBitmap(bitmapsListToSend.get(bitmapsListToSend.size()-1), 0, 0, null);
      }
  }
  
  @SuppressLint(&quot;ClickableViewAccessibility&quot;)
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      return onTouchOrHoverEvent(event, true /*isTouch*/);
  }
  
  private boolean onTouchOrHoverEvent(MotionEvent event, boolean isTouch) {
          /*if (SENDING_NOW)
              return true;*/
  
      final int action = event.getActionMasked();
      mPaintToShow.setStrokeWidth((float) ( getBrushSize() ));
  
      if (action == MotionEvent.ACTION_DOWN) {
          startPoint = new TouchPoint(event, 0);
          runnableSaveBitmaps.run();
          runnableChangeAlpha.run();
          watch.start();
      }else if(action == MotionEvent.ACTION_MOVE){
          runnableChangeAlpha.run();
      }else if (action == MotionEvent.ACTION_UP) {
          startPoint = new TouchPoint(event, 0);
          handlerSaveBitmaps.removeCallbacks(runnableSaveBitmaps);
          handlerChangeAlpha.removeCallbacks(runnableChangeAlpha);
          watch.stop();
      }
  
      //make drawings//
  
          switch (event.getAction()) {
              case MotionEvent.ACTION_MOVE:
                  paintLines(mTouchPoints);
                  break;
              case MotionEvent.ACTION_DOWN:
                  mCanvasToShow.drawCircle(startPoint.x, startPoint.y, getBrushSize()/2f, mPaintToShow);
                  //mCanvasToSend.drawCircle(startPoint.x, startPoint.y, getBrushSize()/2f, mPaintToSend);
                  canvasListToSend.get(canvasListToSend.size()-1).drawCircle(startPoint.x, startPoint.y, getBrushSize()/2f, mPaintToSend);
                  break;
          }
          invalidate();
      }
      return true;
  }
  
  //drawing methods//
  
  Handler handlerSaveBitmaps = new Handler();
  Runnable runnableSaveBitmaps = new Runnable() {
      public void run() {
          handlerSaveBitmaps.postDelayed(runnableSaveBitmaps, 10);
  
          Log.v(&quot;B&quot;, &quot;Bitmap added &quot; + bitmapCounter + &quot; &quot; + (watch.toString()));
          bitmapsListToSend.add(bitmapCounter, Bitmap.createBitmap(drawingWidth,drawingHeight,Bitmap.Config.RGB_565));
          canvasListToSend.add(bitmapCounter++, new Canvas(bitmapsListToSend.get(bitmapsListToSend.size()-1)));
      }
  };
  
  Handler handlerChangeAlpha = new Handler();
  Runnable runnableChangeAlpha = new Runnable() {
      @Override
      public void run() {
          handlerChangeAlpha.postDelayed(runnableChangeAlpha, 635);
  
          if(alpha &lt; 255)
              mPaintToShow.setAlpha(alpha++);
          else {
              handlerChangeAlpha.removeCallbacks(runnableChangeAlpha);
              Log.v(&quot;A&quot;, &quot;ALPHA MAX&quot; + (watch.toString()));
          }
  
      }
  };
  </code></pre>
  <p>}</p>
  <p>If Runnable saves 100 bitmaps - 99 will be black, the last one will be ok</p>
  <p>//Bitmap.copy() fixed it</p>