Android ACTION_SEND事件:"消息无法上传附件"

Cas*_*law 2 android

我正在尝试实现一个共享按钮,用于捕获屏幕截图并通过标准Android界面共享.我能够创建屏幕截图(我可以在浏览SD卡时看到它),但是当我尝试发送它时,消息应用程序会出错:"消息无法上传附件."

我的代码

File imageDir = new File(Environment.getExternalStorageDirectory(), "inPin");
if(!imageDir.exists()) { imageDir.mkdirs(); }
File closeupImageFile = new File(imageDir, "closeup.png");
File overviewImageFile = new File(imageDir, "overview.png");

View mapView = findViewById(R.id.floor_map);
saveScreenshotToFile(mapView, closeupImageFile);

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.fromFile(closeupImageFile));

String message = String.format("I'm on %s %s", building.name, getCurrentFloor().name);

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("image/*");
startActivity(Intent.createChooser(sendIntent, "Share via"));
Run Code Online (Sandbox Code Playgroud)

saveScreenshotToFile方法

private static void saveScreenshotToFile(View view, File saveFile) throws IOException {
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);
    Bitmap bitmap = view.getDrawingCache();
    FileOutputStream out = new FileOutputStream(saveFile);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.close();
}
Run Code Online (Sandbox Code Playgroud)

我在模拟器上使用Android Marshmallow,API级别23 - 我不知道这是否有所作为,但我能够在模拟器上使用其他应用程序共享它并且工作正常.

ian*_*ake 6

您发送的文件URI Intent.FLAG_GRANT_READ_URI_PERMISSION不适用,不应使用,如本视频及其随附的博文中所述.其他应用程序需要存储权限才能访问您的文件.

来自博文:

相反,您可以使用URI权限授予其他应用程序访问特定Uris的权限.虽然URI权限对file://URI的生成Uri.fromFile()不起作用,但它们可以在与内容提供者关联的Uris上运行.您可以而且应该按照文件共享培训中的说明使用FileProvider,而不是仅为此实现自己的.