3
        //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
  • 31
  • 1
  • 2
  • 6

4 Answers4

2

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

albeee
  • 1,452
  • 1
  • 12
  • 20
  • Can you give me any reference for share image to external storage and Content Provider to share image – it's JU Apr 27 '17 at 12:25
  • http://stackoverflow.com/questions/12170386/create-and-share-a-file-from-internal-storage – albeee Apr 27 '17 at 15:54
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;
    }
}
Anchal Singh
  • 329
  • 3
  • 13
  • 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. – greenapps Apr 27 '17 at 12:44
  • 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. – Saurabh Gaddelpalliwar Jan 14 '21 at 08:27
0

I am not sure but you can check imageRes with there extension like .jpeg or png for example android.resource://" + getPackageName() +"/drawable/"+"imageRes.jpg"

Prameshwar
  • 32
  • 6
0

try this:

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_round);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "ic_launcher_round", null);
    Uri imageUri = Uri.parse(path);
    intent.putExtra(Intent.EXTRA_STREAM, imageUri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intent , "Share"));

before this code execute make sure you have to give EXTERNAL_STORAGE permission and target and compile sdk higher than lolipop then you have to request permission

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
  • make sure your images in any drawable directory not Minmap – Bhupat Bheda Apr 27 '17 at 12:49
  • 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? – it's JU Apr 29 '17 at 03:22
  • your image name which is in the drawable directory – Bhupat Bheda Apr 29 '17 at 05:15