使用共享 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/