android - 将图像分享到 Instagram 故事

标签 android image android-intent sharing

使用共享 Intent ,我尝试使用以下代码将图像上传到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 消息无法刷新 feed

最佳答案

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

希望对你有帮助。

关于android - 将图像分享到 Instagram 故事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53113164/

相关文章:

android - 服务中广播接收器的 ANR 问题

android - 去电接收器是否在 android 7.0 或更高版本中工作?

android - 如何在不抬起手指的情况下长按并从弹出菜单中选择一个项目?

image - Flutter:具有填充父级背景颜色的透明子级

android - getSerializableExtra() 随机为 null

安卓/NFC : Get Tag in onCreate() without new Intent

android - AsyncTask 的 Listview 的自定义适配器

ios - 使用 NSURLSession 下载后,将图像设置为对象

javascript - 我将如何使用 javascript 制作图像 "jiggle"或 "shiver"?

android - bundle 额外发送错误的字符串