Android共享图像意图:WhatsApp-文件格式不受支持
我无法将我的应用程序中的图像共享到WhatsAppAndroid共享图像意图:WhatsApp-文件格式不受支持,android,image,android-intent,share,whatsapp,Android,Image,Android Intent,Share,Whatsapp,我无法将我的应用程序中的图像共享到WhatsApp int imageId = getResources().getIdentifier("image_name", "drawable", getPackageName()); Uri imageUri = Uri.parse("android.resource://com.companyname.packagename/drawable/"+ imageId); Intent shareIntent = new Intent(); shar
int imageId = getResources().getIdentifier("image_name", "drawable", getPackageName());
Uri imageUri = Uri.parse("android.resource://com.companyname.packagename/drawable/"+ imageId);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
此代码适用于Facebook Messenger或Android内置Messenger。
但它不适用于WhatsApp。我收到以下错误消息:
“不支持该文件格式!”
我使用@commonware solution解决了这个问题:
换成这个。这可能有用
shareIntent.setType("image/jpeg");
替换为
shareIntent.setType(“image/*”)//它支持所有类型的文件。
许多应用程序都会遇到android.resourceUri
值的问题,因为这些值不常见、意外,因此经常未经测试。请尝试shareIntent.setType(“image/*”)代码>@commonware谢谢你的回答。有没有其他方法可以在不使用android.resource的情况下获取我的图像文件的路径?您可以将此可绘制文件移动到res/raw/
或assets/
,然后使用我的StreamProvider
使用内容为其提供服务:
Uri
:或者,将图像复制到,那么。@commonware谢谢:)我将试用您的CWAC-Provider。这不起作用。如果我使用png图像并将代码更改为shareIntent.setType(“image/png”)代码>,则会发生相同的错误。对于所有类型的文件,您都需要shareIntent.setType(“*/*”)。星号之间没有空格不适用于Android 11(API级别30)。如何解决Android R中不支持的文件格式问题。@SaurabhGaddelpalliwar您找到解决方案了吗?是的,我找到了解决方案,您必须使用以下代码在Android 11上保存图像,filesDir=新文件(context.getExternalFilesDir(Environment.DIRECTORY\u PICTURES),ConstantString.Image\u File\u SHARE\u PATH\u FULL);并使用此生成的路径在WhatsApp上共享图像;imageInternalUri=Uri.parse(mediaPath);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
//set your message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText);
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);`
try { // should you to check Whatsapp is installed or not
startActivity(shareIntent)
}
catch (android.content.ActivityNotFoundException exception) {
showMessage("Whatsapp have not been installed")
}
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
//set your message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText);
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);`
try { // should you to check Whatsapp is installed or not
startActivity(shareIntent)
}
catch (android.content.ActivityNotFoundException exception) {
showMessage("Whatsapp have not been installed")
}