0

here I am making a bitmap that its background is transparent but it dose not works and make background black:

    Bitmap baseBitmap = null;
    Canvas canvas;
    Bitmap mergedBitmap = null;
    baseBitmap = imageViewToBitmap(imgForground);
    mergedBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), Bitmap.Config.RGB_565);
    canvas = new Canvas(mergedBitmap);
    canvas.drawBitmap((baseBitmap), matrix, null);

and here is the function I use to extract bitmap out of imageview:

public Bitmap imageViewToBitmap(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getMinimumWidth(), drawable.getMinimumHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

send it to other activity:

Intent intent = new Intent(EditImageActivity.this, FilterImageActivity.class);
intent.putExtra("uri", getImageUri(getApplicationContext(), bitmap).toString());
startActivity(intent);

and the function getImageUri is :

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    imgForground.setImageBitmap(inImage);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

in the other activity using getintent the image background it black:

 Intent intent = getIntent();

   Uri uri = Uri.parse(intent.getStringExtra("uri"));
                imageView.setImageBitmap(bmp);
baran
  • 17
  • 8
  • https://stackoverflow.com/questions/1492554/set-transparent-background-of-an-imageview-on-android – Farzad Jan 19 '21 at 09:54

2 Answers2

2

You created Bitmap with RGB_565 option.

mergedBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), Bitmap.Config.RGB_565); // your code

It's not support transparent, so you need to use ARGB_8888 for transparent.

Canvas canvas;
Bitmap mergedBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), Bitmap.Config.ARGB_8888); // changed code
Bitmap baseBitmap = imageViewToBitmap(imgForground);
canvas = new Canvas(mergedBitmap);
canvas.drawColor(0);
canvas.drawBitmap(baseBitmap, matrix, null);
Daniel.Wang
  • 2,242
  • 2
  • 9
  • 27
2

add the following to make the background transparent,

canvas.drawColor(Color.TRANSPARENT)
Sekiro
  • 1,537
  • 1
  • 7
  • 21
  • I edit the question, the problem is in sending png via intent and uri to anther activity. – baran Jan 19 '21 at 11:38