Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Null pointer exception while capturing and saving an image using android emulator

I am trying to capture and save an image through an Android emulator, image is being captured, but the file being saved is corrupted.

What could be causing this? can anyone help me identify possible error points?

Below is my code:

**public void onCreate**(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    **Intent** intent = new **Intent**("android.media.action.IMAGE_CAPTURE");
    try {
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    } 

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    intent.putExtra(**MediaStore**.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

@Override
**protected void onActivityResult**(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            StoreImage(this, data.getData(),
            mediaFile);
            finish();
        } 

        else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } 

        else {
            finish();
            try {
            } 

            catch (Exception e) {
               e.printStackTrace();
            }
        }
    }

}

The Stack trace is below::

04-05 23:55:40.369: E/AndroidRuntime(534): FATAL EXCEPTION: main
04-05 23:55:40.369: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{camera.android/camera.android.CameraActivity}: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)

04-05 23:55:40.369: E/AndroidRuntime(534): Caused by: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.net.Uri.fromFile(Uri.java:441)
04-05 23:55:40.369: E/AndroidRuntime(534):  at camera.android.CameraActivity.getOutputMediaFileUri(CameraActivity.java:72)
04-05 23:55:40.369: E/AndroidRuntime(534):  at camera.android.CameraActivity.onCreate(CameraActivity.java:34)
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.Activity.performCreate(Activity.java:4465)
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

2 Answers

check whether you added the below permission in manifest file::

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

also in the

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

I think you must attach the file name that you want to save too, and giving only the path is not enough....

like image 116
Shankar Agarwal Avatar answered May 26 '26 10:05

Shankar Agarwal


Interchange this line,

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {

to

 if (resultCode == RESULT_OK) {
                    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

First check whether the child activity is done its work successfully or not.

like image 33
Mohammed Azharuddin Shaikh Avatar answered May 26 '26 12:05

Mohammed Azharuddin Shaikh