无法使用 Instagram sdk 意图分享故事

Moh*_*tri 8 android android-intent instagram

我们正在关注从应用程序到 Instagram 分享故事的官方文档

https://developers.facebook.com/docs/instagram/sharing-to-stories/

预期的行为是,当我尝试分享故事时,Instagram 应用程序的故事屏幕应该打开,并在意图中发送图像。实际行为是 instagram 应用程序未打开且用户停留在同一屏幕上。

// Define image asset URI and attribution link URL
Uri backgroundAssetUri = "your-image-asset-uri-goes-here;"
String attributionLinkUrl = "https://www.my-aweseome-app.com/p/BhzbIOUBval/";

// Instantiate implicit intent with ADD_TO_STORY action,
// background asset, and attribution link
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");
intent.setDataAndType(backgroundAssetUri, MEDIA_TYPE_JPEG);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("content_url", attributionLinkUrl);

// Instantiate activity and verify it will resolve implicit intent
Activity activity = getActivity();
if (activity.getPackageManager().resolveActivity(intent, 0) != null) {
  activity.startActivityForResult(intent, 0);
}
Run Code Online (Sandbox Code Playgroud)

Vis*_*kar 0

尝试此代码,它将打开所有 Instagram 可能的意图选择器,例如在 Instagram 提要或故事中共享:

if (isPackageInstalled(this, "com.instagram.android")) {

                val media = File(imagePath)
                val uri = Uri.fromFile(media)


                val targetedShareIntents = ArrayList<Intent>()
                val shareIntent = Intent(android.content.Intent.ACTION_SEND)
                shareIntent.setType("image/*")
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
                val resInfo = getPackageManager().queryIntentActivities(shareIntent, 0)
                if (!resInfo.isEmpty()) {

                    val packageName = "com.instagram.android"
                    val targetedShareIntent = Intent(Intent.ACTION_SEND)
                    targetedShareIntent.setType("image/*")
                    targetedShareIntent.putExtra(Intent.EXTRA_STREAM, uri)
                    targetedShareIntent.setPackage(packageName)
                    targetedShareIntents.add(targetedShareIntent)

                    val chooserIntent = Intent.createChooser(
                        targetedShareIntents.removeAt(0),
                        "Share"
                    )
                    chooserIntent.putExtra(
                        Intent.EXTRA_INITIAL_INTENTS,
                        targetedShareIntents.toArray(
                            arrayOfNulls<Parcelable>(
                                targetedShareIntents.size
                            )
                        )
                    )
                    startActivity(chooserIntent)
                }
            } else {

               // "Instagram application is not installed !",

            }
Run Code Online (Sandbox Code Playgroud)

这是检查应用程序是否安装的方法:

fun isPackageInstalled(c: Context, targetPackage: String): Boolean {
        val pm = c.getPackageManager()
        try {
            val info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA)
        } catch (e: PackageManager.NameNotFoundException) {
            return false
        }
        return true
    }
Run Code Online (Sandbox Code Playgroud)

看起来您的代码是用 Java 编写的,抱歉,但是您可以应用上面 kotlin 代码中的逻辑:)