Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android share image Intent: WhatsApp - The file format is not supported

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

like image 915
Robert Avatar asked Jun 15 '16 10:06

Robert


3 Answers

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")
}
like image 112
Bharat Vasoya Avatar answered Nov 15 '22 12:11

Bharat Vasoya


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);
}
like image 25
wambada Avatar answered Nov 15 '22 12:11

wambada


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 "));
} 
like image 36
Gon Juarez Avatar answered Nov 15 '22 11:11

Gon Juarez