我从我的应用程序中找到了直接与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。我该怎么做?
答案 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"));