我有一个功能,我想自动截取屏幕截图,然后将其上传到用户首选的电子邮件应用程序。
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(Environment.getExternalStorageDirectory().getAbsolutePath() + now + mPath);
Uri path = Uri.fromFile(filelocation);
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, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Journey : ");
startActivity(Intent.createChooser(emailIntent , "Select your preferred email app.."));
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
这是我的职责。它会自动拍摄屏幕截图并将其存储在我的本地设备上。它还提示用户选择他们的电子邮件应用程序。然后我选择一个显示“无法附加文件”的应用程序。我在 list 中拥有读取和写入权限。
最佳答案
其他应用程序可能无法访问外部存储。另外,一旦您将 targetSdkVersion
提高到 24 或更高版本,您的代码将在 Android 7.0+ 上崩溃。
切换到using FileProvider
及其 getUriForFile()
方法,而不是使用 Uri.fromFile()
。
最终,将该磁盘 I/O 移至后台线程。
关于java - 无法以编程方式在电子邮件中附加图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416829/