Home » Android » android – share picture via Intent (Facebook and co)

android – share picture via Intent (Facebook and co)

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m wasting a lot of time, trying to get sharing a simple jpeg image working via sharing Intent.

The most important one as usual is Facebook, and as usual it doesn’t work.

Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri picUri = Uri.parse("http://someserver.com/somepic.jpg");
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, someString);
startActivity(Intent.createChooser(shareIntent, "some text..."));

The chooser opens nicely, Facebook also opens and makes me login, but then it tells me that updoad failed.
I also tried Flicker and Mail, and they all fail.
Then I tried to write the image to local file and send from there, also failed:

    Drawable dr = ImageLoader.getDrawable(url);
    Bitmap bmp = ((BitmapDrawable)dr).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] data = stream.toByteArray();

    FileOutputStream ostream;
    try {
        ostream = this.openFileOutput("pic.jpeg", Context.MODE_WORLD_READABLE);
        ostream.write(data);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Uri picUri = Uri.fromFile(new File("pic.jpeg"));
    shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);

I’ve no clue if I do that correctly, haven’t done that before.

My last try was to just send an HTML string with the image included as img tag. But Facebook seems not to handle type “text/html”, so it’s not an option.
I’m sure it needs only few lines of code. But which ones?

edit

I forgot the line

shareIntent.putExtra(Intent.EXTRA_STREAM, picUri);

in first code snippet. It was there when I tried, didn’t work either.
No sleep for too long…

How to&Answers:

I think this will answer your question. https://stackoverflow.com/a/3553102/478537

It looks like you are on the right path, all I can see is that in your first code snippet, you are not using the picUri anywhere, and therefore its not being sent, and in the second, you are setting the EXTRA_STREAM twice (which wouldn’t cause any issues, just redundant code).

Answer:

..Intent.EXTRA_TEXT, someString.

This will fail your transaction with Facebook- not “someString”- Facebook sharing expects and searches for an URL transmitted over the EXTRA_TEXT. Why – don’t ask..I did not understand the guys

Answer:

Well, I spent a lot of time, and the issue was the extension of file (png made a problem so ignore the file extension in this case and use “jpg”), try the following code

public static void shareImageFileWithIntent(File imageFile, Context context) {
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String type = mime.getMimeTypeFromExtension("jpg");
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType(type);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
    sharingIntent.putExtra(Intent.EXTRA_TEXT, "your extra text");
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "your subject");
    context.startActivity(Intent.createChooser(sharingIntent, context.getString(R.string.share_image_intent)));
}

Answer:

Instead of passing image Via intent, you can create a new Class and save it there from one activity. And access that image from another activity.