[英]Unable to attach picture programmatically in email
我有一个功能,我想自动拍摄屏幕截图,然后将其上传到用户首选的电子邮件应用程序。
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();
}
}
这是我的职责 它会自动拍摄屏幕快照,并将其存储在我的本地设备上。 它还提示用户选择其电子邮件应用程序。 然后,我选择一个说“无法附加文件”的应用程序。 我在清单中具有读写权限。
其他应用可能无法访问外部存储。 另外,将targetSdkVersion
至24或更高版本后,您的代码将在Android 7.0+上崩溃。
切换为使用FileProvider
及其getUriForFile()
方法,而不是使用Uri.fromFile()
。
最后,将该磁盘I / O移至后台线程。
检查此链接-
https://www.javacodegeeks.com/2013/10/send-email-with-attachment-in-android.html
希望对您有所帮助。
问题是:
Uri路径= Uri.fromFile(filelocation);
相反,我使用了:
文件文件位置=新文件(MediaStore.Images.Media.DATA + mPath);
Uri myUri = Uri.parse(“ file://” + filelocation);
希望这可以帮助面临相同问题的任何人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.