Reputation: 101
We are following the official documentation for sharing stories from app to instagram
https://developers.facebook.com/docs/instagram/sharing-to-stories/
Expected behavior was that when i try to share a story the story screen of instagram app should open with the image send in the intent. Actual behavior is the instagram app doesn't open and user stays on the same screen.
// 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);
}
Upvotes: 8
Views: 2990
Reputation: 938
Finally, I tested and found a very good solution to share Videos, Photos on Instagram (The sticker option is currently not).
WARNING:- This method does not work on Emulator (for sometimes). But works fine on Real devices.
I created this code and tested it in all android APIs and works like charm.
The code is below
// This method share photos and videos on Instagram Story
`videoFile` -> you can use here your `imageFile` if you want to share images.
public void shareOnInstagram(File videoFile, Activity activity){
if (isPackageInstalled(this.getPackageManager())){
Uri uri = FileProvider.getUriForFile(this, getPackageName()+".provider", videoFile); // This code works properly if you are sharing your videos or photos from storage (device). Let me know or update my answer if you able to share online images to Instagram story.
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY"); // this is your intent
intent.setDataAndType(uri, "video/*"); // if you are sharing image file then add you type this "images/*" or "images/jpeg" if image is .jpeg
activity.grantUriPermission(
"com.instagram.android", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (activity.getPackageManager().resolveActivity(intent, 0) != null) {
activity.startActivityForResult(intent, 0);
}
}else { // this condition follow if Instagram not installed.
Toast.makeText(activity, "Instagram not found", Toast.LENGTH_SHORT).show();
}
}
// This method checks that Instagram installed or not.
private boolean isPackageInstalled(PackageManager packageManager) {
try {
packageManager.getPackageInfo("com.instagram.android", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
To create a file provider. See this answer https://stackoverflow.com/a/18583876/16765223
to call this method
videoFile = "/storage/emulated/0/Android/data/com.example.app/files/tree/Videos/testingvideo01.mp4"
shareOnInstagram(videoFile,MyActivity.this); // add your videopath or imagepath and add your Complete Activity Name.
If any problem occurred. Let me know
Upvotes: 0
Reputation: 257
Working on my side use this
val intent = Intent("com.instagram.share.ADD_TO_STORY")
intent.type = "image/*"
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("interactive_asset_uri", stickerImageFile)
intent.putExtra("content_url", attributionURL)
Log.d("", activity.toString())
// Instantiate activity and verify it will resolve implicit intent
activity.grantUriPermission(
"com.instagram.android",
stickerImageFile,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
if (activity.packageManager.resolveActivity(intent, 0) != null) {
activeContext!!.startActivity(intent)
result.success("success")
} else {
result.success("error")
}
Upvotes: 0
Reputation: 682
try this code, it will open all instagram possible intents chooser like share in instagram feeds or in stories:
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 !",
}
Here is method for checking if application is installed or not:
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
}
It's look like your code is in Java, Sorry but you can apply logic from above kotlin code :)
Upvotes: 0