Issue
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:
We have since been defining our own version of this sharing, which looks something like this on stock Android:
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:
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:
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:
So we are wondering what we are doing wrong that we cannot get the "full" intended result as shown in Google's documentation?
Solution
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
Answered By - Hiếu nguyễn văn
Answer Checked By - Gilberto Lyons (JavaFixing Admin)