Sharing Bitmap via Android Intent

后端 未结 8 1535
名媛妹妹
名媛妹妹 2020-12-02 17:35

In my android app, I have a bitmap (say b) and a button. Now when I click on the button, I want to share the bitmap. I am making use of the below code inside my onClic

相关标签:
8条回答
  • 2020-12-02 18:02

    As CommonsWare stated you need to get the URI to the bitmap and pass that as your Extra.

    String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap,"title", null);
    Uri bitmapUri = Uri.parse(bitmapPath);
    ...
    intent.putExtra(Intent.EXTRA_STREAM, bitmapUri );
    
    0 讨论(0)
  • 2020-12-02 18:02

    A solution in Kotlin with a timestamp in the image's filename:

    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    val imagePath = MediaStore.Images.Media.insertImage(
        context?.contentResolver,
        bitmap,
        "img_$timeStamp",
        null
    )
    val shareIntent = Intent(Intent.ACTION_SEND).apply {
        type = "image/*"
        putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath))
    }
    context?.startActivity(Intent.createChooser(shareIntent, null))
    
    0 讨论(0)
提交回复
热议问题