19

I follow the instruction on Camera on Android dev site

I just start the Camera Intent, not build my own camera.

The sample code to handle result return after taking a photo is as follows.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}

resultCode is OK, but data is always NULL, which causes a NPE. I looked into the sdcard, the photo was really saved there. Any tip? tks much.

Update: logcat info as requested:

   01-28 19:39:00.547: ERROR/AndroidRuntime(24315): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to resume activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.NullPointerException
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3362)
    at android.app.ActivityThread.access$700(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2442)
    ... 13 more
    Caused by: java.lang.NullPointerException
    at com.example.CameraTest.MyCamera.onActivityResult(MyCamera.java:71)
    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
    ... 14 more                                                                                      
7
  • Post some more code and logcat also.
    – GrIsHu
    Jan 28, 2013 at 8:39
  • 2
    Did you use MediaStore.EXTRA_OUTPUT ?
    – Blackbelt
    Jan 28, 2013 at 8:42
  • 1
    private void fireCameraForImage(int requestCode){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(intent, requestCode); }
    – EyeQ Tech
    Jan 28, 2013 at 8:47
  • 1
    @TungMaiLe Just use fileUri to get the image path. If you want to get the data path by default path, don't put MediaStore.EXTRA_OUTPUT
    – Sieryuu
    Jan 28, 2013 at 8:50
  • 4
    Tks Sieryuu, but why is the Intent data null?
    – EyeQ Tech
    Jan 28, 2013 at 8:52

7 Answers 7

27

The problem with your code is this:

data.getData()

This call does not get the extra with the key "data" from the returned Intent. It gets the field data from the returned Intent which is probably null.

You need to get the extra from the returned Intent like this:

data.getExtras().get("data");

Some of the other answers have indicated this, embedded in tons of other code. That makes it difficult to actually see what the problem is.

5
  • 7
    I have the same error, and in this case, the data parameter is null, so performing data.getExtras() in your answer still produces a NPE
    – KJ50
    May 22, 2014 at 8:53
  • I have the same issue. In some of the Samsung device, i got data parameter null. so, I can not move ahead. Jul 13, 2017 at 14:54
  • 1
    @JigneshM.Mehta did you see KJ50's answer regarding Samsung phones? Jul 13, 2017 at 14:58
  • @David Wasser: Still I got fileUri.toString() Null with KJ50's answer.. :( Jul 14, 2017 at 8:46
  • 1
    @JigneshM.Mehta Please open a new question, post your problem, your code, etc. and maybe someone can help you. This isn't the right place to ask for assistance (in a comment of an answer). Jul 14, 2017 at 11:25
8

Here is the answer from a similar question. It seems like it might be a problem with Samsung phones...

Basically, if you have code like this which creates the Intent:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Then, in onActivityResult, replace data.getData() with fileUri.toString() and it will solve your problem.

4
  • And I am sometimes getting fileUri as null.
    – Darpan
    Jul 21, 2015 at 8:52
  • fileUri.toString() is also not working for samsung mobiles. :'( Jul 15, 2016 at 18:34
  • Thank you > = forever (for real)
    – McLan
    Nov 8, 2016 at 15:44
  • Btw, add Nexus-7 tablet (and Cyanogenmod OS) to the Samsung phones.
    – McLan
    Nov 8, 2016 at 15:45
3

Use @KJ50's solution, and use savedInstanceState to make sure you don't get a null.

/**
     * Here we store the file url as it will be null after returning from camera
     * app
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on screen orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }
2

Try out below code :

 Button m_btnCamera;
  ImageView m_ivCaptureImage;
  String m_curentDateandTime;
  String m_imagePath;
  Bitmap m_bitmap;

   //Start camera to caputre image.
 Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  m_intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
  startActivityForResult(m_intent, TAKE_PHOTO_CODE);

 private Uri getImageUri() throws CustomException
    {
    Uri m_imgUri = null;
    File m_file;
    try
    {
        SimpleDateFormat m_sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        m_curentDateandTime = m_sdf.format(new Date());
        m_imagePath = File.getPath() + File.separator + m_curentDateandTime + ".jpg";
        m_file = new File(m_imagePath);
        m_imgUri = Uri.fromFile(m_file);
    }
    catch (Exception p_e)
    {}      
    return m_imgUri;        
}

 @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
  super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK)
    {
        m_bitmap = ImageHelper.scaleImage(m_imagePath, 200, 200);
            m_bitmap = ImageHelper.rotateImage(m_bitmap, true, m_rotate);
            m_ivCaptureImage.setImageBitmap(m_bitmap);
    }
  }
}
2
  • 24
    catch (Exception p_e){} My eyes are bleeding.
    – m0skit0
    Jan 28, 2013 at 9:48
  • 2
    Catching all kinds of exceptions and then ignoring them (without logging or anything) is sure to improve the robustness of your code :-( May 22, 2014 at 9:41
0

Try this

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case PICK_IMAGE_ID:

            Bitmap bitmap = ImagePicker.getImageFromResult(this.getActivity(), resultCode, data);

            if(data!=null){
                //set image view
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}
0

This code will help you

The getData() method returns data only if Android version is newer than M, else it will return null result.

if (resultCode == Activity.RESULT_OK) {

                //Check Android version, as intent.getData() will return data only if v is above or equal to M otherwise the data will be null
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
                    Uri selectedImageURI = imageReturnedIntent.getData();
                    Toast.makeText(getActivity(), "URI Path:" + selectedImageURI, Toast.LENGTH_LONG).show();
                } else {
                    Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                    String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), photo, "Title", null);
                    Uri selectedImageURI = Uri.parse(path);
                    Toast.makeText(getActivity(), "URI Path:" + selectedImageURI, Toast.LENGTH_LONG).show();
                }
            }
-1

Try this code below.....

btn_capture.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
 protected void onActivityResult(int requestCode, int resultCode, Intent data){


             if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                 Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                 img.setImageBitmap(photo);
             } 
          }

    }

Then finally you add below code to your manifest

   <uses-feature android:name="android.hardware.camera"></uses-feature> 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.