bitmap is not being compress that can be share on whats app

Viewed 26

I am capturing an image using camerax API image is being captured successfully and then I convert it to bitmap using below method

fun Image.toBitmap(): Bitmap {
    val buffer = planes[0].buffer
    buffer.rewind()
    val bytes = ByteArray(buffer.capacity())
    buffer.get(bytes)
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}

this method works perfectly and I succeed to setBitMapImage to image view the issue is that I want to share this captured bitmap image on what's app view intent without saving in storage The below method is what I using to compress the bitmap

private fun btmToPng(){
    try {
        val baos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos)
        baos.close()
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
}

but getting this error

Caused by: android.os.TransactionTooLargeException: data parcel size 51917416 bytes

1 Answers

After getting URI of captured images you can share using URI instead of Bitmap

public void share(Uri uri) {

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.setPackage("com.whatsapp");
        if (SDK_INT < 30)
            shareIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(activity, activity.getPackageName() + ".provider", uri);
        else
            shareIntent.putExtra(Intent.EXTRA_STREAM,uri);

        activity.startActivity(Intent.createChooser(shareIntent, "Share to"));

    }
Related