Share intent issue in Instagram feed

后端 未结 6 529
我在风中等你
我在风中等你 2021-01-31 06:55

I have an app that shares images from url. Last android update, I got message from instagram \"Unable to load image\" when I want to share an image in instagram feed.

相关标签:
6条回答
  • 2021-01-31 06:56

    Problem solved with latest instagram update

    0 讨论(0)
  • 2021-01-31 06:57

    Update: This issue was fixed in the version of Instagram released earlier this week. Workarounds no longer necessary.


    None of the solutions mentioned above worked for me, as it seems that direct sharing via ContentProvider or its derivative FileProvider was broken by a change made within the Instagram app.

    I did notice that sharing a MediaStore content Uri still works, as other apps such as Google Photos that write to the MediaStore prior to sharing were still able to share images to feed.

    You can insert an image File to the MediaStore as follows:

    @SuppressLint("InlinedApi")
    fun insertImageToMediaStore(file: File, relativePath: String): Uri? {
    
        val values = ContentValues().apply {
            put(MediaStore.Images.Media.DISPLAY_NAME, file.name)
    
            val mimeType = when (file.extension) {
                "jpg", "jpeg" -> "jpeg"
                "png" -> "png"
                else -> return null
            }
    
            put(MediaStore.Images.Media.MIME_TYPE, "image/$mimeType")
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath)
                put(MediaStore.MediaColumns.IS_PENDING, 1)
            }
        }
    
        val collection = when (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            true -> MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
            false -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        }
    
        val uri = contentResolver.insert(collection, values)
    
        uri?.let {
            contentResolver.openOutputStream(uri)?.use { outputStream ->
                try {
                    outputStream.write(file.readBytes())
                    outputStream.close()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
    
    
            values.clear()
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                values.put(MediaStore.Images.Media.IS_PENDING, 0)
                contentResolver.update(uri, values, null, null)
            }
    
        } ?: throw RuntimeException("MediaStore failed for some reason")
    
        return uri
    }
    

    Then with that Uri that you're returned, share via Intent as follows:

        val filePath = "/data/data/io.jammy.withintent/files/IMG-20200321_093350_2020-122758.jpg" // this is an example path from an app-internal image file
    
        val context: Context? = this
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "image/*"
    
        insertImageToMediaStore(File(filePath), "Pictures/Your Subdirectory")?.let { uri ->
    
            val clipData = ClipData.newRawUri("Image", uri)
    
            intent.clipData = clipData
            intent.putExtra(Intent.EXTRA_STREAM, uri)
    
            val target = Intent.createChooser(intent, "Share Image")
            target?.let { context?.startActivity(it) }
    
        } ?: run {
            Log.e(TAG, "Unsupported image file")
            return
        }
    

    Whilst it's not ideal, as the image is then written to the MediaStore, which may not be desired behaviour in many cases, it re-enables the ability to share in the medium term whilst Instagram fixes their whoopsie.

    0 讨论(0)
  • 2021-01-31 06:57

    Ok, I searched and found a solution. I do not know this is right way but solved my problem..

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    

    Found solution in this answer.

    0 讨论(0)
  • 2021-01-31 06:59

    your uri is "content://packagename/xxx.jpg", it need to be "content://media/external/images/media/..."; it will be worked.

    0 讨论(0)
  • 2021-01-31 07:16

    Looks like Facebook already has the bug for this issue: https://developers.facebook.com/support/bugs/1326888287510350/

    As a temporary workaround you can save media to MediaStore. This is the method we use to store and then return the uri for Instagram sharing.

        private fun insertImageToMediaStore(file: File): Uri? {
    
        val fileUri = FileProvider.getUriForFile(
            context,
            "${context.applicationContext.packageName}.provider",
            file
        )
        val mimeType = context.contentResolver.getType(fileUri) ?: "image/*"
        val isImage = mimeType.contains("image")
        val values = ContentValues().apply {
    
            put(
                if (isImage) {
                    MediaStore.Images.Media.DISPLAY_NAME
                } else {
                    MediaStore.Video.Media.DISPLAY_NAME
                },
                file.name
            )
    
            put(
                if (isImage) {
                    MediaStore.Images.Media.MIME_TYPE
                } else {
                    MediaStore.Video.Media.MIME_TYPE
                },
                mimeType
            )
        }
    
        val collection = if (isImage) {
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        } else {
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI
        }
    
        val uri = context.contentResolver.insert(collection, values)
    
        uri?.let {
            context.contentResolver.openOutputStream(uri)?.use { outputStream ->
                try {
                    outputStream.write(file.readBytes())
                    outputStream.close()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
    
    
            values.clear()
    
        } ?: throw RuntimeException("MediaStore failed for some reason")
    
        return uri
    }
    
    0 讨论(0)
  • 2021-01-31 07:21

    Facebook is working on this bug, let's wait ! Subscribe to receive notification of updates to this bug report. https://developers.facebook.com/support/bugs/1326888287510350/

    0 讨论(0)
提交回复
热议问题