在我的应用程序中,我可以获取屏幕截图并将其保存到 SD 卡。我想知道是否可以将其从我的应用程序共享到 Whatsapp,例如,就像图库应用程序那样。
在图库中,您有机会将文件“共享”到 Whatsapp、Facebook 或 Twitter。
我可以在我的应用程序中执行同样的操作吗?怎么办?
谢谢!
最佳答案
是的,可以。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings( "deprecation" )
public static Intent shareImage(Context context, String pathToImage) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
shareIntent.setType("image/*");
// For a file in shared storage. For data in private storage, use a ContentProvider.
Uri uri = Uri.fromFile(context.getFileStreamPath(pathToImage));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
}
这里有详细链接:http://android-developers.blogspot.com/2012/02/share-with-intents.html
因此,只需添加一个“共享”按钮并执行 startActivity(shareIntent)
。
关于android - 将图像共享到其他应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12236778/