The file format is not supported - When a photo is send on WhatsApp

30,156

Solution 1

Try setting the content type in the share Intent as image/png. As you said above, that may or may not work for all apps.

The reason is You can not directly share a uri from you apps internal storage (of course the resourses of your app will be always in the internal storage)

There are two ways of achieving this..

Copy your image to external storage then share it from there. See this

Write a Content Provider to share image. For that refer Create and Share a File from Internal Storage

Solution 2

Here is an example how to do this:

public void shareImageWhatsApp() {

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        new FileOutputStream(f).write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM,
            Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg"));
    if(isPackageInstalled("com.whatsapp",this)){
          share.setPackage("com.whatsapp"); 
          startActivity(Intent.createChooser(share, "Share Image"));

    }else{

        Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
    }

}

private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}
Share:
30,156
it's JU
Author by

it's JU

Updated on July 09, 2022

Comments

  • it's JU
    it's JU almost 2 years
            //Share image to all
            share = (Button)findViewById(R.id.facebook_app_id);
            share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Uri imageUri = Uri.parse("android.resource://" + getPackageName() +"/drawable/"+imageRes);
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("image/*");
    
                    intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(Intent.createChooser(intent , "Share"));
    
    
                }
    
            });
    

    I am trying to build a photo-sharing app. Facebook, Messenger, Skype work perfectly but Whatsapp and Viber show an Error (The file format is not supported)

  • it's JU
    it's JU about 7 years
    Can you give me any reference for share image to external storage and Content Provider to share image
  • greenapps
    greenapps about 7 years
    Pretty bad to convert a png file to a bitmap and then compress it to a stream and using a second stream to write a jpg file. You could instead have copied the drawable directly to file.
  • Bhupat Bheda
    Bhupat Bheda about 7 years
    make sure your images in any drawable directory not Minmap
  • albeee
    albeee about 7 years
  • it's JU
    it's JU about 7 years
    I use ImageView... ImageView image = (ImageView)findViewById(R.id.imageView2); image.setImageResource(imageRes); Is it work, if I use ImageAdapter? According my code "ic_launcher_round"=? what is mine ic_launcher_round?
  • Bhupat Bheda
    Bhupat Bheda about 7 years
    your image name which is in the drawable directory
  • Saurabh Gaddelpalliwar
    Saurabh Gaddelpalliwar over 3 years
    Image File is exist in my Android Emulator Android R . image is showing in imageView perfectly but not share on whatsapp From My App .Always Show File Formate not supported. please help me for this Android R (API 30) . How to Share.