0

I'm having a share button which would share an image from drawable with text.

public boolean shareGame(String msg) {
    if (Configuration.Share_WITH_IMAGE) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("image/png");
        Uri uri = Uri
                .parse("android.resource://" + getPackageName() + "/" + R.drawable.share_image);

        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, msg + " " + GOOGLE_PLAY_URL);
        startActivity(Intent.createChooser(shareIntent, "Share: " + msg));
    } else {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, msg + GOOGLE_PLAY_URL);
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, msg));
    }

    return true;

}

Currently, it returns an empty file without any extension when I try to share it through email. WhatsApp share gives an error of unsupported file type.

Do I need to convert this to Bitmap? If so, what's the best way to do it.

Thanks.

Tahir
  • 73
  • 2
  • 11
  • 1
    `android.resource` is a little-used `Uri` scheme. Some apps may not expect it and may have problems using it. "Do I need to convert this to Bitmap?" -- no, because `ACTION_SEND` does not support a `Bitmap`. You may wish to use a `ContentProvider` to serve the image. – CommonsWare Aug 15 '16 at 18:43

2 Answers2

2

this code works.. i have tested it for both gmail and whatsapp

Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "share_image.jpg");

    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());

    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_TEXT, "send text");

    share.putExtra(Intent.EXTRA_STREAM,
            Uri.parse("file:///sdcard/share_image.jpg"));
    startActivity(Intent.createChooser(share, "Share Image"));

Edit: i dont know why you are getting that error hope you have added permissions if not add following permissions

 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • Still doesn't work. Here is what I get in logcat. W/ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:830 – Tahir Aug 18 '16 at 18:26
  • Worked finally. I had to use `Uri.parse("file:///storage/emulated / 0 /share_image_new.jpg"));` Don't know why but sdcard wasn't working for me although I have already updated the permission. Isn't there any way where I could access the image directly from drawable, so that I didn't have to save the image manually on storage? – Tahir Aug 19 '16 at 10:17
-1
Uri imageUri = Uri.parse("android.resource://" + getPackageName()
    + "/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));

From https://stackoverflow.com/a/20333318/2316935

Community
  • 1
  • 1
Hobo Joe
  • 737
  • 1
  • 9
  • 11
  • Unfortunately this isn't working for me & I'm having the same problem. – Tahir Aug 16 '16 at 11:09
  • do not just post code w/o explanation. If the explanation and code came from another question then you only need link to that question in a comment. – Rabbit Guy Aug 17 '16 at 13:51