Sharing a URL with image preview data sharesheet

Viewed 1369

To try to share data from our app using the native Android sharesheet, we have been following this documentation:

https://developer.android.com/training/sharing/send

Which gives us the following example:

Description from Google documentation

We have since been defining our own version of this sharing, which looks something like this on stock Android:

enter image description here

However, we seem to be unsuccessful so far in making Android show the sharesheet correctly. We have tried the following code, and I have attached the resulting screenshot for each code block so that you can compare to requirement:

sendIntent.setAction(Intent.ACTION_SEND);

if (event != null && event.getTitle() != null && collection.getTitle() != null) {
    sendIntent.putExtra(Intent.EXTRA_TITLE, event.getTitle() + " - " + collection.getTitle());
}

sendIntent.putExtra(Intent.EXTRA_TEXT, collection.getShareUrl());

File thumbFile = DCPhotoUtils.captureView(rootView, R.id.shareThumbnailLayout);
Uri uriToImage = DCPhotoUtils.getUri(getContext(), thumbFile);
sendIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
sendIntent.setDataAndType(uriToImage, "image/*");
sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(sendIntent, null));

Which results in just an imagine being shown:

enter image description here

We then tried the following approach:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("image/*");
if (event != null && event.getTitle() != null && collection.getTitle() != null) {
    sendIntent.putExtra(Intent.EXTRA_TITLE, event.getTitle() + " - " + collection.getTitle());
}

sendIntent.putExtra(Intent.EXTRA_TEXT, collection.getShareUrl());

File thumbFile = DCPhotoUtils.captureView(rootView, R.id.shareThumbnailLayout);
Uri uriToImage = DCPhotoUtils.getUri(getContext(), thumbFile);
sendIntent.setData(uriToImage);
sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(sendIntent, null));

Which then results in... showing nothing haha:

enter image description here

And then finally we tried our third and last approach:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("image/*");
if (event != null && event.getTitle() != null && collection.getTitle() != null) {
    sendIntent.putExtra(Intent.EXTRA_TITLE, event.getTitle() + " - " + collection.getTitle());
}

sendIntent.putExtra(Intent.EXTRA_TEXT, collection.getShareUrl());

startActivity(Intent.createChooser(sendIntent, null));

Which does at least get us the link, but no link preview thumbnail:

enter image description here

So we are wondering what we are doing wrong that we cannot get the "full" intended result as shown in Google's documentation?

2 Answers

I had work a lot on this issue. Finally, I did it. You should try your third solution and add this command shareIntent.setClipData(ClipData.newRawUri("", yourImageUri)); It works perfectly with me

This Android codelab goes through the preview setup which worked for me.

Related