我正在尝试实现一个共享按钮,用于捕获屏幕截图并通过标准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)
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 - 我不知道这是否有所作为,但我能够在模拟器上使用其他应用程序共享它并且工作正常.
| 归档时间: |
|
| 查看次数: |
1356 次 |
| 最近记录: |