Vivek Faldu
Vivek Faldu

Reputation: 336

Sharing Image to Instagram Story

Using sharing intent i tried to upload image to Instagram using below code

  public static boolean shareBitmap(Context context, Bitmap bitmap, String textToShare, String packageNameOfApp,
                                  String intentMessage) {
    try {
        File file = new File(context.getCacheDir(), "Share.jpg");
        saveBitmapAtLocation(bitmap, Bitmap.CompressFormat.JPEG, 100, file);
        Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, contentUri);
        String textToAppend = null;
        textToAppend = context.getString(R.string.shortenUrl);
        if (textToShare != null && !textToShare.isEmpty())
            intent.putExtra(Intent.EXTRA_TEXT, textToShare + " " + textToAppend);
        else
            intent.putExtra(Intent.EXTRA_TEXT, textToAppend);
        intent.setType("image/*");
        List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        if (packageNameOfApp != null) {
            if (appInstalledOrNot((Activity) context, packageNameOfApp))
                intent.setPackage(packageNameOfApp);
            else
                return false;
        }

        context.startActivity(Intent.createChooser(intent, intentMessage));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

When i try to post directly to the Instagram story it gives a black screen with a Toast message Couldn't refresh feed

Upvotes: 2

Views: 6196

Answers (3)

DanielParedes
DanielParedes

Reputation: 31

I had the same problem, and I found an alternative solution in my own question:

Xamarin Forms can't share image to Instagram stories

The only thing is that I'm developing with xamarin forms, not with native android, but I hope my answer helps 😅

Upvotes: 0

Nidhi Savaliya
Nidhi Savaliya

Reputation: 179

After long time research finally i found ans.

   private void shareStory(){

    Uri backgroundAssetUri = FileProvider.getUriForFile(this, "your_package_name.provider", imgFile);

    Intent storiesIntent = new Intent("com.instagram.share.ADD_TO_STORY");
    storiesIntent.setDataAndType(backgroundAssetUri,  "image/*");
    storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    storiesIntent.setPackage("com.instagram.android");
    this.grantUriPermission(
            "com.instagram.android", backgroundAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    this.startActivity(storiesIntent);
} 

Upvotes: 1

Shamsul
Shamsul

Reputation: 435

Set Instagram-app-package to intent-package will do your work.

Uri uri = Uri.fromFile(new File(YOUR_FILE_PATH))
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setPackage("com.instagram.android"); //Instagram App package
startActivity(Intent.createChooser(shareIntent, "Share.."));

Hopes it help you.

Upvotes: 1

Related Questions