Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自内部存储的Android映像共享不工作_Java_Android_Image - Fatal编程技术网

Java 来自内部存储的Android映像共享不工作

Java 来自内部存储的Android映像共享不工作,java,android,image,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 = n

我有一个功能,可以将特定图像共享到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 FileProvider组件生成内容 文件的URI,基于XML中提供的规范


请查看示例

是否尝试将其共享给其他应用程序?GetFileDir()是应用程序的专用目录。没有其他应用程序可以访问。请尝试GetExternalFileDir()。您应该得到一个FileUriExposedException,而不是使用文件提供程序。@blackapps
GetExternalFileDir
将在外部存储或内部存储中查找目录?请查看函数名。它告诉你什么?