共享 URL 图像预览数据共享表

Sharing a URL with image preview data sharesheet

要尝试使用本机 Android 共享表从我们的应用程序共享数据,我们一直在关注此文档:

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

这给了我们下面的例子:

从那以后,我们一直在定义我们自己的分享版本,在库存中看起来像这样 Android:

但是,到目前为止,我们似乎未能成功 Android 正确显示共享表。我们尝试了以下代码,我附上了每个代码块的结果截图,以便您可以与要求进行比较:

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));

结果显示的只是想象:

然后我们尝试了以下方法:

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));

然后导致...什么都不显示哈哈:

最后我们尝试了第三种也是最后一种方法:

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));

这至少让我们得到了 link,但没有 link 预览缩略图:

所以我们想知道我们做错了什么,我们无法获得 Google 文档中所示的“完整”预期结果?

我在这个问题上做了很多工作。最后,我做到了。您应该尝试第三个解决方案并添加此命令 shareIntent.setClipData(ClipData.newRawUri("", yourImageUri)); 它非常适合我