gpt4 book ai didi

android - 无法使用 Instagram sdk Intent 分享故事

转载 作者:行者123 更新时间:2023-12-02 18:30:56 29 4
gpt4 key购买 nike

我们正在遵循官方文档将故事从应用程序分享到 Instagram

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

预期的行为是,当我尝试分享故事时,Instagram 应用程序的故事屏幕应该打开,并在 Intent 中发送图像。实际行为是 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);
}

最佳答案

尝试此代码,它将打开所有 Instagram 可能的 Intent 选择器,例如在 Instagram Feed 或故事中分享:

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 !",

}

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

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
}

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

关于android - 无法使用 Instagram sdk Intent 分享故事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59088699/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com