(Android)从Drawable文件夹直接分享PNG到WhatsApp

时间:2016-01-15 19:03:49

标签: android android-intent

我从我的应用程序中找到了直接与WharsApp共享文本的代码:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
sendIntent.setPackage("com.whatsapp");

现在我想将Drawable文件夹(R.drawable.image2share)中的PNG直接共享给WhatsApp。我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

Uri imageUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/image2share");
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/png");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setPackage("com.whatsapp");
startActivity(Intent.createChooser(sendIntent, "send"));