我想分享存储在内部存储中的屏幕截图,但我在共享时得到的是一个空白屏幕。我开始知道另一个应用程序无法使用获得app私有数据的应用程序。这是我用来保存图像的代码:
File path = new File(getApplicationContext().getDir("Default", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING),"myapp");
File directory = new File(path.getAbsolutePath());
directory.mkdirs();
String filename = "myapp.png";
File yourFile = new File(directory, filename);
try
{
FileOutputStream out = new FileOutputStream(yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90,out);
out.flush();
out.close();
send(yourFile);
}catch (IOException e)
{
e.printStackTrace();
}
分享意图是:
public void send(File path)
{
Uri uri = Uri.fromFile(path);
Log.d("uri", String.valueOf(path));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My App");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share via"));
}
保存图像的路径和路径的值为:
/data/user/0/com.sample.myapp/app_Default/myapp/myapp.png
现在我如何确保屏幕截图被访问并可供共享。
答案 0 :(得分:0)
您获取的图像URI不是真实图像路径
获取真实路径检查
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.6</version>
<configuration>
<excludeRoots>
<excludeRoot>target/generated-sources</excludeRoot>
</excludeRoots>
</configuration>
</plugin>
只是传递此图像uri并获得完整的图像路径
答案 1 :(得分:0)
我通过将图像导出到外部存储然后共享来解决它。
String url = null;
try {
url = MediaStore.Images.Media.insertImage(getContentResolver(), path.getAbsolutePath(), path.getName(), path.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(url);