Intent.createChooser not working for sharing image in REDMI xiomi devices

373 Views Asked by At

Intent.createChooser not working for sharing image in REDMI devices

I am saving image locally in my external storage of device and then passing the URI of that image in Intent.putExtra(Intent.EXTRA_STREAM, uri) and MIME type as image/jpg and then using Intent.createChooser . It works perfectly in all android devices but in REDMI devices the image get stored but I dont see the options menu for social media apps....

First I am calling function shareImage and from which I am calling saveMediaToStorage function to get URI... I have attached the screenshot for what I get in REDMI devices....

        var finalUri: Uri?=null
        var fos: OutputStream? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            requireContext().contentResolver?.also { resolver ->
                val contentValues = ContentValues().apply {
                    put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
                    put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
                    put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
                }
                val imageUri: Uri? = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
                finalUri=imageUri
                fos = imageUri?.let { resolver.openOutputStream(it) }
            }
        } else {
            val imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            val image = File(imagesDir, filename)
            finalUri= FileProvider.getUriForFile(requireContext(),"${requireContext().packageName}.provider",image)
                //image.toUri()
            fos = FileOutputStream(image)
        }
        fos?.use {
            bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, it)
            it.close()
        }

        return finalUri
    }

    private fun shareImage() {

        Glide.with(requireContext())
            .asBitmap()
            .load(badge?.image)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {

                   val uri= saveMediaToStorage(resource,"${badge?.name}.jpg")
                    val shareIntent = Intent()
                    shareIntent.putExtra(
                        Intent.EXTRA_TEXT,
                        "${badge?.name} \n\nLorem ipsum dolor sit amet. Quo alias fuga vel rerum quod ad voluptatem debitis?"
                    )
                    shareIntent.action = Intent.ACTION_SEND
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
                    shareIntent.type = "image/jpg"
                    startActivity(Intent.createChooser(shareIntent,  "Share Badge via..."))

                }

                override fun onLoadCleared(placeholder: Drawable?) {
                }
            })

    }

enter image description here

Please help me share the image locally stored in redmi device having a menu with options of all social media apps.....

1

There are 1 best solutions below

0
Kishan Mevada On

You need to set one more flag while sharing intent after setting shareIntent.putExtra(Intent.EXTRA_STREAM, uri) :

shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

and in your AndroidManifest.xml file, set in FileProvider Tag:

android:grantUriPermissions="true"