1

I have searched a lot to solve this issue, but couldnt get it fixed.

I am using a camera function in my app and having troubles passing the intent to the callback method. The user can either upload an image from the gallery

retrieveFromGallery()

which works fine. He also can take a picture

takePicture() 

which causes a crash in

onActivityResult()

as the intent is null. It is not null however, when I call

startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

The picture gets saved in the file path defined, but I want to display it in the app, as I do when I retrieve it from the gallery. I followed the Android Developer Tutorial, so my code should be the same as there. Only difference: Im using fragments Any help is greatly appreciated :)

private static final int PICK_IMAGE_REQUEST = 2;
private static final int REQUEST_TAKE_PHOTO = 3;

public void retrieveFromGallery() {
    Intent intent = new Intent();
    // Show only images, no videos or anything else
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    // Always show the chooser (if there are multiple options available)
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

public void takePicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(this.getActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
            Log.d(LOG_TAG, "creating photofile: " + photoFile);
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.d(LOG_TAG, "error creating the file" + ex);

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Log.d(LOG_TAG, "temporary photofile path: " + photoFile);
           takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri photoURI = FileProvider.getUriForFile(this.getActivity(),
                    getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
            Log.d(LOG_TAG, "uri: " + photoURI);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            Log.d(LOG_TAG, "intent: " + takePictureIntent + " , " + REQUEST_TAKE_PHOTO);
        }

    }
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri uri = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
            popupWindowSelectUploadSource.dismiss();
            selectedImage.setVisibility(View.VISIBLE);
            selectedImage.setImageBitmap(bitmap);
            Log.d(LOG_TAG, "img path" + bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
                Log.d(LOG_TAG, "on activity result - intent: " + requestCode + "resultcode "+ resultCode + "data " + data);
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                popupWindowSelectUploadSource.dismiss();
                selectedImage.setImageBitmap(imageBitmap);
            }

}

From my Logcat:

  03-18 13:26:36.752 593-593/com.example.myapp D/SpotMap: opening a popup window
  03-18 13:26:37.721 593-593/com.example.myapp D/SpotMap: opening the popup window to select upload source
  03-18 13:26:38.626 593-593/com.example.myapp D/SpotMap: creating photofile:/storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_132638_5713532469653547292.jpg
  03-18 13:26:38.627 593-593/com.example.myapp D/SpotMap: temporary photofile: path: /storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_132638_5713532469653547292.jpg
  03-18 13:26:38.627 593-593/com.example.myapp D/SpotMap: uri: content://com.example.myapp.fileprovider/my_images/JPEG_20180318_132638_5713532469653547292.jpg
  03-18 13:26:38.681 593-593/com.example.myapp D/SpotMap: intent: Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 clip={text/uri-list U:content://com.example.myapp.fileprovider/my_images/JPEG_20180318_132638_5713532469653547292.jpg} (has extras) } , 3
  03-18 13:26:38.714 593-593/com.example.myapp D/SpotMap: on Pause
  03-18 13:26:47.444 593-593/com.example.myapp D/SpotMap: on activity result - intent: 3resultcode -1data null

My error message:

 E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.myapp, PID: 593
              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65539, result=-1, data=null} to activity {com.example.vreeni.StreetMovementApp/com.example.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4339)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4382)
                  at android.app.ActivityThread.-wrap19(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)

UPDATE:

I tried what was suggested in a similar post, I am able to enter into the OnActivityResult method, and just create the URI there. But then, however, it cannot be converted into Bitmap. Returns null.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri uri = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
            popupWindowSelectUploadSource.dismiss();
            selectedImage.setVisibility(View.VISIBLE);
            selectedImage.setImageBitmap(bitmap);
            Log.d(LOG_TAG, "img path" + bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
            Log.d(LOG_TAG, "creating photofile: " + photoFile);
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.d(LOG_TAG, "error creating the file" + ex);

        }
        if (photoFile != null) {
            Log.d(LOG_TAG, "temporary photofile path: " + photoFile);
            Uri photoURI = FileProvider.getUriForFile(this.getActivity(),
                    getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
            Log.d(LOG_TAG, "uri: " + photoURI);
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), photoURI);
                Log.d(LOG_TAG, "bitmap: " + bitmap);
                selectedImage.setVisibility(View.VISIBLE);
                selectedImage.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
Vr33ni
  • 101
  • 16
  • 2
    can you not just use photoURI since you know that picture is captured – Raghunandan Mar 18 '18 at 12:42
  • Thanks for the tip. Tried it with URI, `Bitmap myBitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath());` and `selectedImage.setImageBitmap(myBitmap);` as well as loading the image using glide now, but no image is being displayed.. – Vr33ni Mar 18 '18 at 15:10
  • Same result sadly. I just tried to set the image resource with `setImageResource` and that works but any other way to set the image fails. I do still get the uri so I am completely lost in what the problem is – Vr33ni Mar 18 '18 at 16:16
  • This is what I get in logcat, when printing `Uri`: `content://com.example.myapp.fileprovider/my_images/JPEG_20180318_173008_5768841254225995534.jpg` Is this not, how it should work? – Vr33ni Mar 18 '18 at 16:33
  • `03-18 17:43:00.410 30813-30813/com.example.myapp D/SpotMap: uri: content://com.example.myapp.fileprovider/my_images/JPEG_20180318_174300_2454911568678617064.jpg 03-18 17:43:00.427 30813-30813/com.example.myapp D/SpotMap: intent: Intent { act=android.media.action.IMAGE_CAPTURE } , 3 03-18 17:43:00.479 30813-30813/com.example.myapp D/SpotMap: on Pause 03-18 17:43:07.476 30813-30813/com.example.myapp D/SpotMap: on activity result content://com.example.myapp.fileprovider/my_images/JPEG_20180318_174300_2454911568678617064.jpg` seems the same to me :/ – Vr33ni Mar 18 '18 at 16:49
  • Actually the Skia doesnt appear anymore now: I had used the worng imageView to test it. (been staring at the code for too long) When I use the correct imageView, I get this: 03-18 18:14:53.424 14793-14793/com.example.myapp D/SpotMap: Glidejava.lang.RuntimeException: setDataSource failed: status = 0xFFFFFFEA – Vr33ni Mar 18 '18 at 17:16
  • Yeah, same here.. but cant take a break, cause I really wana solve this. Yes I tried this too, but it just returned me an empty imageview. I just went over to using glide cause I was hoping for a helpful error message :D – Vr33ni Mar 18 '18 at 17:48
  • Manifest: ` ` – Vr33ni Mar 18 '18 at 18:02
  • Fragment (to get URI): `photoURI = FileProvider.getUriForFile(this.getActivity(), getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);` XML: `` – Vr33ni Mar 18 '18 at 18:02
  • Will try what you suggested with the absolute path :) – Vr33ni Mar 18 '18 at 18:03
  • Hmmm so weirdly, this is what I get. text file length 0, but existing file + path: 03-18 19:29:16.740 9260-9260/com.example.myapp D/SpotMap: test file length: 0 03-18 19:29:16.741 9260-9260/com.example.myapp D/SpotMap: test file Path: /storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_192908_6744038580465499929.jpg – Vr33ni Mar 18 '18 at 18:33
  • `getImageBitmapFromAssets` form the post you referred to, returns null as well.. Yes they all get saved (Got a bunch of random photos on my phone now) and I can upload them later on using the `retrieveFromGallery` function...Maybe there will be no Camera function in my app :D – Vr33ni Mar 18 '18 at 19:07
  • `` Regarding the path and `getExternalFilesDir`, I followed the Android Dvlpmt Tutorial. I am not explicitly asking the user for permission, but according to the tutorial, that is not necessary. I just dont get that I can print out `URI` and `filepath` anytime, but cant load either into my `imageView` object... – Vr33ni Mar 18 '18 at 19:43
  • [link](https://developer.android.com/training/camera/photobasics.html#TaskPath) That's the one I followed. Ill give that a try. Otherwise, that'd be great. Really appreciate the help!! – Vr33ni Mar 18 '18 at 22:54
  • :D So should I rather not have any path in my manifest? I do refer to the xml there as well. If you want to I can share my github link with you. Do I send you a private msg or how does this work here? – Vr33ni Mar 19 '18 at 00:04
  • Yes, but that is what I removed based on the post [link](https://stackoverflow.com/questions/9890757/android-camera-data-intent-returns-null), because passing Extras caused a crash.. – Vr33ni Mar 19 '18 at 01:33
  • it works for you? this is great news, thank you! But did you pass the `EXTRA_OUTPUT` before `OnActivityResult` or not? I am a bit confused now. – Vr33ni Mar 19 '18 at 01:41
  • 1
    Yep, you attach it to the camera `Intent`, so the camera app knows where to store the resulting image. Just put the `takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);` line back where you had it in the original posted code, right before the `startActivityForResult()` line in `takePicture()`: https://github.com/Vreeni/StreetMovementApp_v2/blob/master/app/src/main/java/com/example/vreeni/StreetMovementApp/MapView_Fragment.java#L880. – Mike M. Mar 19 '18 at 01:45
  • 1
    FINALLLYYY. Thank you soo, soo much, I really hadnt expected to get so much help on here. Now I can finally get food and sleep. All the best from Denmark :) – Vr33ni Mar 19 '18 at 02:13
  • 1
    Phew! Good. Honestly, this isn't really how Stack Overflow is supposed to work, but I felt bad for having caused the confusion in the first place. Sorry about that. Anyhoo, glad you got it working. Cheers! – Mike M. Mar 19 '18 at 02:17

0 Answers0