Android ACTION_SEND事件:“邮件上传附件失败" [英] Android ACTION_SEND event: "messaging failed to upload attachment"

查看:167
本文介绍了Android ACTION_SEND事件:“邮件上传附件失败"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个共享按钮,该按钮可以捕获屏幕截图并通过标准的Android界面进行共享.我可以创建屏幕截图(浏览SD卡时可以看到它),但是当我尝试发送它时,消息应用程序显示错误:消息发送附件失败."

I'm trying to implement a share button that captures a screenshot and shares it through the standard Android interface. I'm able to create the screenshot (and I can see it when I browse the SD card), but when I try to send it, the messages app gives the error: "messaging failed to upload attachment."

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"));

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();
}

我在模拟器上使用的是API级别为23的Android Marshmallow,我不知道这是否有所不同,但是我能够在模拟器上使用其他应用程序进行共享,并且运行良好.

I'm using Android Marshmallow, API level 23, on the emulator—I don't know if this makes a difference, but I was able to share using other apps on the emulator and it worked fine.

推荐答案

您正在发送文件URI,如

You are sending File URIs, with which Intent.FLAG_GRANT_READ_URI_PERMISSION does not apply and should not be used, as explained in this video and its accompanying blog post. Other apps would need the storage permission to access your files.

来自博客文章:

相反,您可以使用URI权限来授予其他应用程序对特定Uris的访问权限.虽然URI权限不适用于Uri.fromFile()生成的file:// URI,但它们确实适用于与文件共享中所述培训.

Instead, you can use URI permissions to grant other apps access to specific Uris. While URI permissions don’t work on file:// URIs as is generated by Uri.fromFile(), they do work on Uris associated with Content Providers. Rather than implement your own just for this, you can and should use FileProvider as explained in the File Sharing Training.

这篇关于Android ACTION_SEND事件:“邮件上传附件失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆