1

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.....

  • `First I am calling share image and from which I am calling saveMediaToStorage function to get URI..`. That makes no sense. You should first have a stored image in a file and an uri before you can share the file. – blackapps Jul 15 '22 at 04:17
  • is this list swipeable? ```options menu for social media apps``` is just below the "Share files with device nearby" item. – Ayaka_Ago Jul 15 '22 at 04:37
  • @blackapps those are the function name I am referring to....also I have clearly mentioned my image is saving in external memory but I am unable to have social media apps in share pop-up – Shubham Khatri Jul 15 '22 at 04:46
  • @Ayaka_Ago Nope the screen is not swappable and my orientation is set to landscape mode for the app – Shubham Khatri Jul 15 '22 at 04:49
  • Well wrong funtion name. You are talking about createChooser instead of your share intent for ACTION_SEND. Try without that chooser and try ACTION_SENDTO. – blackapps Jul 15 '22 at 06:11
  • @shubhamKhatri This is a dialog bug. You can "fix" it temporarily, try to use AOSP style share dialog without disabling "MIUI optimization" by using ```Intent.ACTION_CHOOSER``` instead of ```Intent.createChooser``` ```java context.startActivity( new Intent(Intent.ACTION_CHOOSER) .putExtra(Intent.EXTRA_INTENT, //your Intent.createChooser intent param ) .putExtra(Intent.EXTRA_TITLE, //your Intent.createChooser title param ) ``` – Ayaka_Ago Jul 15 '22 at 06:26
  • @Ayaka_Ago I tried your solution, still the same issue in landscape mode – Shubham Khatri Jul 15 '22 at 07:35

1 Answers1

0

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"
Kishan Mevada
  • 662
  • 1
  • 6
  • 17