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);" so my guess is that something goes wrong when I try to save it since the attachmentFile is null.
I can not figure out why the attachmentFile is null since that is the reason I get 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
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...
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());
}