无法使用 instagram sdk 意图分享故事
Posted
技术标签:
【中文标题】无法使用 instagram sdk 意图分享故事【英文标题】:Unable to share story using instagram sdk intent 【发布时间】:2019-11-28 12:16:15 【问题描述】:我们正在按照官方文档从应用程序到 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);
【问题讨论】:
【参考方案1】:你可以试试这个
private void shareToInstagram()
path = tvSelectMedia.getText().toString().trim();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null)
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)));
catch (FileNotFoundException e)
e.printStackTrace();
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
else
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.instagram.android"));
startActivity(intent);
【讨论】:
shareIntent.putExtra(Intent.EXTRA_STREAM, uri, path, "Step Up", "Step Up")));此行不起作用,会出现编译错误。【参考方案2】:试试这个代码,它将打开所有 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 !",
这是检查应用程序是否安装的方法:
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 代码中应用逻辑:)
【讨论】:
以上是关于无法使用 instagram sdk 意图分享故事的主要内容,如果未能解决你的问题,请参考以下文章
从使用 Expo SDK 构建的 React-Native 应用程序将照片分享到 Instagram