it's JU
it's JU

Reputation: 31

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

        //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)

Upvotes: 3

Views: 31917

Answers (4)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

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

Upvotes: 0

Anchal Singh
Anchal Singh

Reputation: 329

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;
    }
}

Upvotes: 2

albeee
albeee

Reputation: 1472

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

Upvotes: 2

Prameshwar
Prameshwar

Reputation: 32

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

Upvotes: 0

Related Questions