2

I'm trying to share an image I have previously saved on disk, sending an Intent.ACTION_SEND. The problem is that I can't find a way to be compatible with different apps, official Gmail app and TweetDeck in my case.

The image I want to share is contained in a File:

File agendaFile; 
// its path using getAbsolutePath() -> /data/data/com.mypackage/files/agenda.jpg

Option A) using Uri.fromFile

Uri agendaUri = Uri.fromFile(agendaFile); 
// the value -> file:///data/data/com.mypackage/files/agenda.jpg

Results

  • Gmail, is the image attatched to the email? NO
  • Tweetdeck, is the image added to the tweet message? YES

Option B) using Uri.parse

Uri agendaUri = Uri.parse(agendaFile.toURI().toString()); 
// the value -> file:/data/data/com.mypackage/files/agenda.jpg

Results

  • Gmail, is the image attatched to the email? YES
  • Tweetdeck, is the image added to the tweet message? NO

Finally

In both cases I send the intent like this:

final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(android.content.Intent.EXTRA_STREAM, agendaUri);
startActivity(Intent.createChooser(intent, "title"));

So, is there any other options to share an image? How is it the best way to share an image being compatible with most apps as possible?

Thanks!

fegabe
  • 101
  • 1
  • 2
  • 8
  • same issue http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya Aug 06 '15 at 06:25

4 Answers4

3

try

intent.setType("image/*");

for me it works for twitter, whatsapp, bluetooth....

EDIT: full code:

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");

        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
        intent.putExtra(android.content.Intent.EXTRA_TEXT, R.string.visita);
        Uri uri = Uri
        .parse("android.resource://com.package.xname/drawable/"
                + i);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
vallllll
  • 2,731
  • 6
  • 43
  • 77
  • It has the same results for me. Which way do you use to get the uri from a File? – fegabe Aug 15 '12 at 15:10
  • i am using the ressources (my pic in in drawable folder) that's maybe the difference, see EDIT – vallllll Aug 15 '12 at 16:55
  • Thank you for sharing your code. I've tried and still having same problem. As you suggest the difference could be my image is from disk and I'm doing something wrong building the uri. Thanks anyway! – fegabe Aug 16 '12 at 05:45
3

I finally solved the problem storing the image at the MediaStore. Instead of using the URI of the File what I do is:

String agendaFilename = agendaFile.getAbsolutePath();

final ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, agendaFilename);
final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

And finally I use contentUriFile:

final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(android.content.Intent.EXTRA_STREAM, contentUriFile);
startActivity(Intent.createChooser(intent, "title"));
fegabe
  • 101
  • 1
  • 2
  • 8
  • 3
    I think you need to change the following line: intent.setType("image/jpg"); to: intent.setType("image/jpeg"); image/jpg is not a proper MIME type and that will cause the image not to be passed to the intent properly. – DiscDev Nov 24 '12 at 05:13
  • Thank you very much! Had similar [issue](http://stackoverflow.com/questions/18031587/facebook-doesnt-appear-on-share-windows-when-trying-to-share-an-image/18033281#18033281) and your way solved my problem. Do you know whats the problem with Uri.formfile that doesnt send the image on facebook? – Alireza Farahani Aug 03 '13 at 13:17
  • what agendaFile? i am getting error in cannot resolve – Aditya Aug 06 '15 at 06:15
  • agendaFile is the File representing the image I want to share in the intent – fegabe Aug 06 '15 at 14:05
0

this code is more easy

Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setData(Uri.parse("mailto:"));
            intent.putExtra(intent.EXTRA_EMAIL,"XXXXX@XXXX.com");
            intent.putExtra(intent.EXTRA_SUBJECT, "XXXXX");
            intent.putExtra(Intent.EXTRA_TEXT, "XXXXX");
            intent.setType("message/rfc822");
            chosser = Intent.createChooser(intent, "Enviar Email");
            intent.putExtra(intent.EXTRA_STREAM, uri);
            startActivity(chosser);
DavidUps
  • 366
  • 3
  • 9
0

For me using FileProvider worked out. I had it set up for taking photos with built-in camera and used it for sharing (see below).

final Uri uri = FileProvider.getUriForFile(mActivity, "com.paeuba.paragonik.fileprovider", photoFile);
Intent intent = ShareCompat.IntentBuilder.from(mActivity).setType("image/jpeg").setStream(uri).createChooserIntent();
mActivity.startActivity(intent);
3trees
  • 1