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

时间:2020-01-24 08:12:09

标签: java android image

我具有将特定图像共享到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打开时,它说加载图像失败,然后被关闭。

1 个答案:

答案 0 :(得分:0)

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

要将文件从您的应用安全地提供给另一个应用,您需要 将您的应用配置为以以下形式提供文件的安全处理: 内容URI。 Android FileProvider组件生成内容 文件的URI,基于您以XML提供的规范

请查看official documentation以获取示例