将视频和贴纸图片分享到 Android 上的 Instagram 故事

Share video and sticker Image to Instagram Story on Android

如何将作为背景的视频和作为贴纸的图片一起分享到 Instagram 快拍?

如果内容都是图片,本文档只有一种解决方案。

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

我想发送背景视频和贴纸图片。 Instagram Story 可以做到这一点吗?

我试过了,但不幸的是它没有用:

   // Define image asset URI and attribution link URL
    Uri backgroundAssetUri = Uri.fromFile(new File(backgroundPath));
    Uri stickerAssetUri = Uri.fromFile(new File(stickerPath));

    // 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, "*/*");
    intent.putExtra("interactive_asset_uri", stickerAssetUri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    callbackManager.startActivityForResult(Intent.createChooser(intent, "Share"), NatShareCallbacks.ACTIVITY_SHARE_INSTAGRAM_STORY);

但是包含两个图像的示例可以正常工作。我发现问题主要与 SetType 有关,因为它们是两种不同的内容类型。

[编辑]

单独没有贴纸的视频已经在 Android 上为我工作,并且带有图像背景和图像贴纸的文档示例也很完美。但不是视频和贴纸在一起。

它在 iOS 下工作没有任何问题:

NSData *backgroundVideo = [[NSFileManager defaultManager] contentsAtPath:path];

UIImage *appIcon = [UIImage imageNamed: [[[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIcons"] objectForKey:@"CFBundlePrimaryIcon"] objectForKey:@"CFBundleIconFiles"]  objectAtIndex:0]];

// Verify app can open custom URL scheme, open
NSURL *urlScheme = [NSURL URLWithString:@"instagram-stories://share"];
if ([[UIApplication sharedApplication] canOpenURL:urlScheme]) {
    // Assign background image asset and attribution link URL to pasteboard
    //NSArray *pasteboardItems = @[@{@"com.instagram.sharedSticker.backgroundVideo" : backgroundVideo}];
    NSArray *pasteboardItems = @[@{@"com.instagram.sharedSticker.backgroundVideo" : backgroundVideo, @"com.instagram.sharedSticker.stickerImage" : UIImagePNGRepresentation(appIcon)}];
    NSDictionary *pasteboardOptions = @{UIPasteboardOptionExpirationDate : [[NSDate date] dateByAddingTimeInterval:60 * 5]};
    // This call is iOS 10+, can use 'setItems' depending on what versions you support
    [[UIPasteboard generalPasteboard] setItems:pasteboardItems options:pasteboardOptions]; [[UIApplication sharedApplication] openURL:urlScheme options:@{} completionHandler:nil];
} else {
    // Handle older app versions or app not installed case

}

需要立即检查的最明显的事情是:

  1. 您的资产是否符合这些条件:

    Uri to an image asset (JPG, PNG) or video asset (H.264, H.265, WebM). Minimum dimensions 720x1280. Recommended image ratios 9:16 or 9:18. Videos can be 1080p and up to 20 seconds in duration. The Uri needs to be a content Uri to a local file on the device.

  2. intent.setDataAndType(backgroundAssetUri, "*/*"); - 文档说函数的第二个值可能为空,但我认为“*/*”不是有效的 mime 类型:尝试使用MEDIA_TYPE_VIDEO - Link to Docs intent.setDataAndType(backgroundAssetUri, MEDIA_TYPE_VIDEO);

MEDIA_TYPE_VIDEO added in API level 11

public static final int MEDIA_TYPE_VIDEO

Constant for the MEDIA_TYPE column indicating that file is a video file.

Constant Value: 3 (0x00000003)

  1. 最后 - 您是否测试过启动 activity,如示例所示:
    Activity activity = getActivity();
    activity.grantUriPermission("com.instagram.android", stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    if (activity.getPackageManager().resolveActivity(intent, 0) != null) {
        activity.startActivityForResult(intent, 0);
    }

这是一个错误。

Facebook 写道: "They've added the functionality now to Android as well, so you should be able to send a background with a sticker now."

我尝试了与 Facebook 官方文档相同的方法,然后在 Huawai P9 Lite (N)、Huawai P20 Lite (O) 和 Samsung S8 (O) 上进行了测试 - 它只适用于 Samsung S8 原因尚不清楚。我放弃了尝试,因为很明显,它不适用于大多数手机。

最有趣的是,用同样的方法在 feed 上分享效果很好:

Intent intent = new Intent("com.instagram.share.ADD_TO_FEED"); //feed
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(image, "image/jpeg");

Uri image = getImageUri();

Activity activity = getActivity();
if (activity.getPackageManager().resolveActivity(intent, 0) != null) {
    activity.startActivityForResult(intent, 0);
}