anz
anz

Reputation: 1327

not able to extract the drawing cache with overlayed bitmap images

I am trying to save a canvas on an sdcard. Basically I am drawing two bitmaps (one on top of another) in the onDraw(Canvas canvas) method. But when I save the file, only the bottom layered Bitmap is stored. I am posting the code for the onDraw method here:

    Paint paint = new Paint();
    paint.setColor(Color.BLACK);

    //rectangle for the first image
    rct = new Rect(10, 10, canvas.getWidth(), canvas.getHeight());

    // rectangle for the second image, the secong image is drawn where the user touches the screen
    new_image = new RectF(touchX, touchY, touchX + secondBitmap.getWidth(),
                touchY + secondBitmap.getHeight());

   //this is the bitmap that is drawn first
    canvas.drawBitmap(firstBitmap, null, rct, paint);

    //this is the bitmap drawn on top of the first bitmap on user touch
    canvas.drawBitmap(secondBitmap, null, new_image, paint);

    canvas.save();

the code for saving the canvas on the SDcard written on the MainActivity is:

   Bitmap bm = canvas.getDrawingCache() // canvas in an object of the class I extended from View
   String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
   boolean exists = (new File(path)).exists();

   OutputStream outStream = null;
   File file = new File(path, "drawn_image" + ".jpg");

   try {
      outStream = new FileOutputStream(file);
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
      outStream.flush();
      outStream.close();
      } 
   catch (FileNotFoundException e) {
    e.printStackTrace();
    } 
   catch (IOException e) {
        e.printStackTrace();
    }

The problem is that only the base image(firstBitmap of the onDraw() method) is saved on the SDCard instead of the entire canvas(with both the images). I am new to canvas...So any help would be very appreciated

Upvotes: 0

Views: 806

Answers (1)

Kirti Patel
Kirti Patel

Reputation: 54

when getDrawingcache() is called it invalidates the view to get the full cache. so debug your code and check if it is going through each and every line of your onDraw() method in view.

Upvotes: 2

Related Questions