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

13,391

Solution 1

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.

Solution 2

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")
}

Solution 3

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);
}
Share:
13,391
Robert
Author by

Robert

Updated on July 21, 2022

Comments

  • Robert
    Robert almost 2 years

    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

  • Robert
    Robert almost 8 years
    This doesn't work. If I use a png image and change the code to shareIntent.setType("image/png"); , then the same error occurs.
  • M.ArslanKhan
    M.ArslanKhan almost 5 years
    For all type of files you need shareIntent.setType(" * / * "). no space between asterisks
  • Saurabh Gaddelpalliwar
    Saurabh Gaddelpalliwar over 3 years
    Not Working in Android 11 (API level 30) . how to solve issue of File format not supported in Android R.
  • Ashwin Nirmale
    Ashwin Nirmale about 3 years
    @SaurabhGaddelpalliwar have you found solution?
  • Saurabh Gaddelpalliwar
    Saurabh Gaddelpalliwar about 3 years
    Yes I found the Solution
  • Saurabh Gaddelpalliwar
    Saurabh Gaddelpalliwar about 3 years
    You Have to save Image on Android 11 Using below code ,, filesDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTU‌​RES), ConstantString.IMAGE_FILE_SHARE_PATH_FULL); and use this generated path to share image on WhatsApp.
  • Saurabh Gaddelpalliwar
    Saurabh Gaddelpalliwar about 3 years
    Use This for Android 11 , String mediaPath = MediaStore.Images.Media.insertImage(mActivity.getContentReso‌​lver(), result, newFile.getName(), null); imageInternalUri = Uri.parse(mediaPath);
  • Saurabh Gaddelpalliwar
    Saurabh Gaddelpalliwar about 3 years
    Then pass this above Uri like this to share on whatsapp, ... Intent waIntent = new Intent(Intent.ACTION_SEND); waIntent.setType("image/*"); waIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); waIntent.putExtra(Intent.EXTRA_STREAM, imageInternalUri); waIntent.putExtra(Intent.EXTRA_TEXT, textYouWant); mActivity.startActivity(Intent.createChooser(waIntent, "Share with"));
  • wambada
    wambada over 2 years
    MediaStore.Images.Media.insertImage() is deprecated now
  • Arth Tilva
    Arth Tilva over 2 years
    @SaurabhGaddelpalliwar 's solution is only working.
  • Aryan Bisht
    Aryan Bisht about 2 years
    @SaurabhGaddelpalliwar what is result insertImage() ????