Share intent issue in Instagram feed

核能气质少年 提交于 2020-04-07 11:37:27

问题


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.

But i can share image story, direct message, and everywhere... I am having this issue only instagram feed.

public void onShareItemOreo() {
    // Get access to bitmap image from view
    imageView = (ImageView) findViewById(R.id.thumbnail);

    // Get access to the URI for the bitmap
    Uri bmpUri = prepareShareIntent(imageView);
    if (bmpUri != null) {

        //outfile is the path of the image stored in the gallery
        // Construct a ShareIntent with link to image
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.setData(bmpUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_TEXT,marketLink);
        // Launch sharing dialog for image
        startActivity(Intent.createChooser(shareIntent, "Share Image"));
    } else {
        //
    }
}


public Uri prepareShareIntent(ImageView imageView) {

    // Fetch Bitmap Uri locally
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }

    Uri bmpUri = getBitmapFromDrawable(bmp);// see previous remote images section and notes for API > 23
    // Construct share intent as described above based on bitmap
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.setType("image/*");

    return bmpUri;
}

回答1:


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/




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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
}


来源:https://stackoverflow.com/questions/60786976/share-intent-issue-in-instagram-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!