12

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
  • 161
  • 1
  • 3
  • 9
  • 1
    Many apps will have problems with `android.resource` `Uri` values, as they are uncommon, unexpected, and therefore frequently untested. – CommonsWare Jun 15 '16 at 10:58
  • Try `shareIntent.setType("image/*");` – Janki Gadhiya Jun 15 '16 at 11:06
  • @CommonsWare Thanks for your answer. Is there another way to get the path of my image file without using `android.resource` ? – Robert Jun 15 '16 at 11:58
  • You could move this drawable into `res/raw/` or `assets/`, then use my `StreamProvider` to serve it using a `content:` `Uri`: https://github.com/commonsguy/cwac-provider Or, copy the image to a file on [internal storage](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html), then [use `FileProvider` to serve it using a `content:` `Uri`](https://commonsware.com/blog/2016/03/16/how-publish-files-via-content-uri.html). – CommonsWare Jun 15 '16 at 12:00
  • @CommonsWare Thanks :) I will try out your CWAC-Provider. – Robert Jun 15 '16 at 12:24

6 Answers6

4

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);
}
codeman
  • 144
  • 7
1
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")
}
Prasanthvel
  • 27
  • 2
  • 15
Bharat Vasoya
  • 351
  • 4
  • 7
1

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 "));
} 
Gon Juarez
  • 59
  • 5
0

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.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
-4
mIntent.setType("image/png");

Replace with it this. It may work.

android_griezmann
  • 3,757
  • 4
  • 16
  • 43
  • This doesn't work. If I use a png image and change the code to `shareIntent.setType("image/png");` , then the same error occurs. – Robert Jun 15 '16 at 11:53
-5
shareIntent.setType("image/jpeg"); 

REPLACE BY shareIntent.setType("image/*"); //it support all type of files.

Hiren Vaghela
  • 61
  • 1
  • 1
  • 6