0

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());

    }
mrzipper
  • 11
  • 5
  • 1
    So, which line is the error pointing to? – Aleks G Dec 14 '18 at 13:05
  • This _attachmentFile_ is _null_ – Piyush Dec 14 '18 at 13:16
  • 1
    So, `attachmentFile = cursor.getString(columnIndex);` returns `null` perhaps(?) Debugging your app with a break point set to the beginning of `onActivityResult()` will show you what exactly happens and attaching the actual crash log will give readers a better idea of the problem. – Markus Kauppinen Dec 14 '18 at 13:16
  • But why is the attachmentFile null, shouldnt i get it from the getContentResolver()? Yes it return null but i dont know why, sorry I will edit the crashlog right away! – mrzipper Dec 14 '18 at 13:27
  • Check there should not be a permission issue. – Piyush Dec 14 '18 at 13:27
  • I dont think it is a permission issue, I permitted both INTERNAL_STORAGE and EXTERNAL_STORAGE – mrzipper Dec 14 '18 at 14:16

1 Answers1

2

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)

Apuna12
  • 375
  • 2
  • 6
  • 23
  • I get this error on last line though. android.os.FileUriExposedException: file:///storage/emulated/0/file%3A exposed beyond app through ClipData.Item.getUri() – mrzipper Dec 14 '18 at 14:06
  • ok. Just try this :) https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed It may help :) – Apuna12 Dec 14 '18 at 14:10
  • Did you mean that I should replace my onActivityResult() code with this code right? :) – mrzipper Dec 14 '18 at 14:20
  • Me personally I have it in my Button Listener, but if you need to have it in your onActivityResult(), I think it should work that way – Apuna12 Dec 14 '18 at 14:23
  • I updated the question with the method that takes me to the picture gallery, is it compatible? :) – mrzipper Dec 14 '18 at 14:57
  • I updated once more the code. I changed from what you said, am I doing it right? – mrzipper Dec 14 '18 at 16:11
  • I have changed my application according to the solution for FileUriExposedException but the URI is still null. – mrzipper Dec 15 '18 at 14:28
  • can you post a screenshot of debug of URI? – Apuna12 Dec 16 '18 at 19:32