本文件介紹如何將分享功能整合至 Android 應用程式,以便用戶將圖片、影片和貼圖分享到 Instagram Reels。
Reels LayersThe Reels composer has a background video layer and an optional sticker layer.
Sharing Icon
|
You will need:
By accessing and using this functionality, you acknowledge and agree to be bound by the Meta Platform Terms and Developer Policies. You also represent and warrant that any content made available through your application or website, or shared to Instagram Reels from your application or website, does not infringe upon the intellectual property rights of any third party and that you own, control or have otherwise secured all rights necessary to distribute, copy, publicly perform and display, or otherwise use the content via this functionality, including as uploaded and shared on Instagram Reels. You further represent and warrant that you have the authority to make the foregoing representations on behalf of your organization. If you do not have such authority or are otherwise unable to make the foregoing representations, you are not authorized to continue and should not do so.
您需要使用 Explicit Intent(顯性意圖)來啟動 Instagram 應用程式,並向其傳送內容以作 Reels 之用。Instagram 應用程式會接收此等內容,並在 Reels 編輯器中加以載入,以便用戶將其編輯並發佈到自己的 Reels。
分享流程一般如下:
"com.instagram.android"
,以確保 Instagram 應用程式會處理此 intent。移除您在用戶裝置上建立的任何臨時檔案。
您應在將內容分享到 Reels 時傳送下列資料。
資料 | 說明 |
---|---|
Meta 應用程式編號 字串 | 此為必要項目。您的 Meta 應用程式編號 |
媒體資產 單個資產的字元串或 包含多個資產的清單 | 此為必要項目。需要以下選項之一:
|
貼圖資產 字串 | 貼圖的 URI(此貼圖需要是用戶裝置上的本機檔案)。可接受的貼圖格式: |
下列 Java 和 Kotlin 程式碼範例示範如何傳送單個或多個圖片或影片,以及如何傳送帶貼圖的圖片或影片。
下列程式碼範例會將一個檔案傳送至 Instagram,以便用戶將其編輯並發佈到自己的 Instagram Reels。
// Instantiate an intent
val intent = Intent("com.instagram.share.ADD_TO_REEL")
// Set package
intent.setPackage("com.instagram.android")
// Attach your App ID to the intent
val appId = "your-app-id"
intent.putExtra("com.instagram.platform.extra.APPLICATION_ID", appId)
// Attach your image or video to the intent from a URI
val mediaAssetUri = Uri.parse("your-image-or-video-asset-uri-goes-here")
intent.setDataAndType(mediaAssetUri, "image/* video/*")
intent.putExtra(Intent.EXTRA_STREAM, mediaAssetUri)
// Instantiate an activity
val activity: Activity = getActivity()
// Grant URI permissions
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
val resInfoList = activity.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName
activity.grantUriPermission(packageName, mediaAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
// Verify that the activity resolves the intent and start it
if (activity.packageManager.resolveActivity(intent, 0) != null) {
activity.startActivityForResult(intent, 0)
}
下列程式碼範例會將多個檔案傳送至 Instagram,以便用戶將其編輯並發佈到自己的 Instagram Reels。
// Instantiate an intent
val intent = Intent("com.instagram.share.ADD_TO_REEL_MULTIPLE")
// Set package
intent.setPackage("com.instagram.android")
// Attach your App ID to the intent
val appId = "your-app-id"
intent.putExtra("com.instagram.platform.extra.APPLICATION_ID", appId)
intent.setType("image/* video/*")
// Attach your files to the intent
val uri1 = Uri.parse("your-first-uri-goes-here")
val uri2 = Uri.parse("your-second-uri-goes-here")
val mediaList = mutableListOf<Uri>()
mediaList.addAll(listOf(uri1, uri2))
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(mediaList))
// Instantiate an activity
val activity: Activity = getActivity()
// Grant URI permissions
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
val resInfoList =
activity.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
for (uri in mediaList) {
for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName
activity.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
}
// Verify that the activity resolves the intent and start it
if (activity.packageManager.resolveActivity(intent, 0) != null) {
activity.startActivityForResult(intent, 0)
}
下列程式碼範例會將一個包含選用貼圖的影片傳送至 Instagram,以便用戶將其編輯並發佈到自己的 Instagram Reels。
// Instantiate an intent
val intent = Intent("com.instagram.share.ADD_TO_REEL")
// Set package
intent.setPackage("com.instagram.android")
// Attach your App ID to the intent
val appId = "your-app-id"
intent.putExtra("com.instagram.platform.extra.APPLICATION_ID", appId)
// Attach your video to the intent from a URI
val videoAssetUri = Uri.parse("your-video-asset-uri-goes-here")
intent.setDataAndType(videoAssetUri, "video/*")
intent.putExtra(Intent.EXTRA_STREAM, videoAssetUri)
// Attach your sticker to the intent from a URI
val stickerAssetUri = Uri.parse("your-image-asset-uri-goes-here")
intent.putExtra("interactive_asset_uri", stickerAssetUri)
// Instantiate an activity
val activity: Activity = getActivity()
// Grant URI permissions
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
val resInfoList = activity.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName = resolveInfo.activityInfo.packageName
activity.grantUriPermission(packageName, videoAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.grantUriPermission(packageName, stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
// Verify that the activity resolves the intent and start it
if (activity.packageManager.resolveActivity(intent, 0) != null) {
activity.startActivityForResult(intent, 0)
}
為了讓開發人員更方便使用,Meta 在 GitHub 上發佈了分享到 Instagram Reels 的程式碼範例 fbsamples/share_to_reels_android。觀看以下影片,了解如何使用此 GitHub 範例。