我有图像列表,我想共享所选的图像。为此,我使用了 FileProvider 。我正在 Android 10 上运行应用程序。我也尝试了没有shareIntent(Intent.EXTRA_STREAM,uri),然后仅通过对话框打开应用程序共享。不共享任何内容后,仅返回应用程序。
在AndroidManifest.xml中;
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
在路径中;
<paths>
<files-path
name="files"
path="/" />
<cache-path
name="cache"
path="/" />
这是我使用和开始意图的方式,
int pos = getAdapterPosition();
File imageFile = new File(imageQuotes.get(pos).getPath());
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", imageFile);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setDataAndType(uri,"image/*");
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
mContext.startActivity(shareIntent);
运行该应用程序后,我为它选择了WhatsApp。我的图像文件作为BIN文件发送,并且在whatsapp上没有图像预览。 uri不是null,而是 content://com.scoto.partestestapplication.provider/files/SavedImageQuotes/Partes_1601815401590