1

I am making an app in which I am drawing a bitmap on a canvas as a overlay after erasing some part of overlay bitmap I want to save it into sd-card but when is save it contain black UI like attach screenenter image description here

And My code is bellow:-

public EraserView(Context context) {
    super(context);
    setFocusable(true);
    setBackgroundResource(R.drawable.back);
    // setting paint
    mPaint = new Paint();
    mPaint.setAlpha(0);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    mPaint.setColor(Color.TRANSPARENT);
    mPaint.setAntiAlias(true);
    Resources r = this.getContext().getResources();
    Bitmap bm = BitmapFactory.decodeResource(getResources(),
            R.drawable.image2);
    bitmap = bm.createBitmap(295, 260, Config.ARGB_8888);
    pcanvas = new Canvas();
    pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
    pcanvas.drawBitmap(bm, 0, 0, null);
}

@Override
protected void onDraw(Canvas canvas) {
    pcanvas.drawCircle(x, y, r, mPaint);
    canvas.drawBitmap(bitmap, 0, 0, null);
    setBitmap(bitmap);
    super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction())
    {
    case MotionEvent.ACTION_DOWN:
        x = (int) event.getX();
        y = (int) event.getY();
        r = 2;
       invalidate();
        break;
    case MotionEvent.ACTION_UP:
        x = (int) event.getX();
        y = (int) event.getY();
        r = 20;
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        x = (int) event.getX();
        y = (int) event.getY();
        r =2;
        invalidate();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        x = (int) event.getX();
        y = (int) event.getY();
        r = 2;
        // Atlast invalidate canvas
        invalidate();
        break;
    }
    return true;
}

public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}

public Bitmap getBitmap() {
    return bitmap;
}

But My requirement is only save overlay thanks in advance.

SKT
  • 253
  • 4
  • 15

1 Answers1

2

When CompressFormat is JPEG its shows you black background because JPEG format does not support alpha transparency, just change CompressFormat to PNG and even save your image in png format instead jpeg. check below code:

ByteArrayOutputStream objbytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, objbytes);

While delcare path of image, use .png extension.

directory + "/pics+"+System.currentTimeMillis()+".png";

Community
  • 1
  • 1
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • Perhaps you didn't get question, My need is how to save only overlay bitmap ,currently bitmap is saving properly but with black background so kindly help regarding that.. – SKT Jul 01 '13 at 05:50
  • Do you want to save the black part. Or the dog? Could you clarify what the foreground and what is the background is in your picture? – RvdK Jul 01 '13 at 10:22
  • @Rvdk I want to save only dog part – SKT Jul 01 '13 at 10:44