private void sendScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
outputStream.flush();
outputStream.close();
File filelocation = new File(MediaStore.Images.Media.DATA + mPath);
Uri myUri = Uri.parse("file://" + filelocation);
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"Enter your email address"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Journey : " + now );
startActivity(Intent.createChooser(emailIntent , "Select your preferred email app.."));
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
这是我在Android 7.0上完美运行的代码。代码截取屏幕截图,为其添加时间戳,将其保存到本地存储,然后将其附加到用户选择的电子邮件应用程序中。有人有解决方案吗?感谢
答案 0 :(得分:0)
这是我在Android 7.0上完美运行的代码
它应该与FileUriExposedException
崩溃。
现在电子邮件意图无效
你没有解释什么是“不工作”的意思。我猜你正在崩溃FileUriExposedException
。 file
Uri
- 无论是通过Uri.fromFile()
还是Uri.parse("file://"+...)
方法 - 默认情况下在Android 7.0+上无法使用。 Switch to using FileProvider
和getUriForFile()
。