Reputation: 241
I've problems with sharing images from my App to WhatsApp.
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"));
This code works fine with Facebook Messenger or Androids build in messenger. But it doesn't work with WhatsApp. I get this error message:
"The file format is not supported!"
I've solved this problem by using @CommonsWare solution: https://github.com/commonsguy/cwac-provider
Upvotes: 13
Views: 18038
Reputation: 69
In android 11 you need to use the file provider in order to share media files.
public void shareFile(){
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/*");
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", ImageUri));
startActivity(Intent.createChooser(sendIntent,"Share "));
}
Upvotes: 2
Reputation: 144
This is what worked for me even on Android 11. It based on this Android documentation: https://developer.android.com/training/data-storage/shared/media. In my usecase, I needed to share an image generated from converting a layout to a bitmap. I had to first save the image to shared media storage but I believe private storage should work as as well.
public void shareImage(Bitmap bitmap) {
Uri contentUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
contentUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
ContentResolver contentResolver = getApplicationContext().getContentResolver();
ContentValues newImageDetails = new ContentValues();
newImageDetails.put(MediaStore.Images.Media.DISPLAY_NAME, "filename");
Uri imageContentUri = contentResolver.insert(contentUri, newImageDetails);
try (ParcelFileDescriptor fileDescriptor =
contentResolver.openFileDescriptor(imageContentUri, "w", null)) {
FileDescriptor fd = fileDescriptor.getFileDescriptor();
OutputStream outputStream = new FileOutputStream(fd);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bufferedOutputStream);
bufferedOutputStream.flush();
bufferedOutputStream.close();
} catch (IOException e) {
Log.e(TAG, "Error saving bitmap", e);
}
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageContentUri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "some text here");
sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("image/*");
Intent shareIntent = Intent.createChooser(sendIntent, "Share with");
startActivity(shareIntent);
}
Upvotes: 4
Reputation: 65
Replace shareIntent.setType("image/jpeg");
with this shareIntent.setType("image/*");
this will select all types of images and maybe this will work for you, because its workig for me just fine.
Upvotes: 0
Reputation: 361
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")
}
Upvotes: 1
Reputation: 61
shareIntent.setType("image/jpeg");
REPLACE BY shareIntent.setType("image/*"); //it support all type of files.
Upvotes: -5
Reputation: 3887
mIntent.setType("image/png");
Replace with it this. It may work.
Upvotes: -4