来自内部存储的安卓图像共享不起作用



我有一个功能可以将特定图像分享到Instagram,图像是从HTTP下载并存储在内部存储中。当我尝试共享图像时,它无法执行此操作。

// Store bitmap to image
String path = context.getFilesDir() + "/MyApp";
File imageFolder = new File(path);
if (!imageFolder.exists()) {
imageFolder.mkdirs();
}
File imageFile = new File(path, UUID.randomUUID().toString() + ".jpg");
if (!imageFile.exists()) {
imageFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
// Share image
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageFile.toURI().toString()));
activity.startActivity(Intent.createChooser(share, "Share to"));

它不会引发任何异常,但是当 instagram 打开时,它会显示加载图像失败,然后关闭。

为了使用文件共享,您需要使用文件提供程序:

要安全地将文件从您的应用提供给另一个应用,您需要 将应用配置为提供文件的安全句柄,格式为 的内容 URI。Android 文件提供程序组件生成内容 文件的 URI,基于您在 XML 中提供的规范

请查看官方文档以获取示例

最新更新