[英]Sending an email + attachment from an custom app in Android Studio, why is the URI null?
Hello I am working on an app that can send an email with an attachment. 您好,我正在开发一个可以发送带有附件的电子邮件的应用程序。 I am completly stuck.
我完全被困住了。 I am unable to attach an attachment to my app.
我无法将附件附加到我的应用程序。 I press the attachment button and I can choose a picture but every time I press the picture, the app crash.
我按下附件按钮,可以选择一张图片,但是每次按下图片时,应用都会崩溃。
The LogCat gives me a NullPointerException and saying the problem is at "Log.e("Attachment Path: ", attachmentFile);" LogCat给了我一个NullPointerException,并说问题出在“ Log.e(“附件路径:”,attachmentFile);“ so my guess is that something goes wrong when I try to save it since the attachmentFile is null.
所以我的猜测是由于附件文件为null,因此在尝试保存时出了点问题。
I can not figure out why the attachmentFile is null since that is the reason I get NullPointerException. 我不能弄清楚为什么attachmentFile为null,因为那是我得到NullPointerException的原因。
I appreciate every help I can get. 我感谢我所能提供的一切帮助。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null,null,null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path: ", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
Here is the crash 这是崩溃
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:39 flg=0x1 }} to activity {com.example.william.mailappen/com.example.william.mailappen.MainMail}: java.lang.NullPointerException: println needs a message
UPDATE I use this method to open the picture gallery 更新我使用此方法打开图片库
public void pictureGallery(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
}
2:nd UPDATE 2:更新
Dont know if I am doing it correctly or if this is the right way to go compared to the other method above or is it something super clear that I am missing? 不知道我是否做对了,还是与上述其他方法相比,这是正确的方法,还是我很想念我是否清楚? The URI is still null...
URI仍然为空...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK)
{
File filelocation = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("image/*");
String to[] = {mailAdressTextField.getText().toString()};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_STREAM, path);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, messageTextField.getText().toString());
}
I used something like this. 我用过这样的东西。 Forked fine
分叉的罚款
File filelocation = new File(Environment.getExternalStorageDirectory(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = {"email@email.com"};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_STREAM, path);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your string");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
EDIT 编辑
filename is the name of your file with postfix (for example .jpg) filename是带后缀的文件名(例如.jpg)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.