分享图片到Instagram故事

时间:2018-11-02 05:36:42

标签: android image android-intent sharing

使用共享意图,我尝试使用以下代码将图像上传到 Instagram

  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;
}

当我尝试直接发布到Instagram故事时,它会显示黑屏,并带有Toast消息无法刷新供稿

2 个答案:

答案 0 :(得分:1)

将Instagram-app-package设置为intent-package将完成您的工作。

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.."));

希望它对您有帮助。

答案 1 :(得分:0)

经过长时间的研究,终于找到了答案。

   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);
}