0
votes

I am using this code to create a simple camera apps. I can take a photo. However, the application crash after taking the photo.

{
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                       

    File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "abc.jpg");


    Uri store= Uri.fromFile(imageFile);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, store);
    intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 0);
    startActivityForResult(intent, 0);
}

/***************************************onActivityResult***************************************/

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    if (imageFile.exists())
                    {
                        Toast.makeText(this, "The file was saved at " + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(this, "Error saving the file ", Toast.LENGTH_LONG).show();
                    }
                        break;
                case Activity.RESULT_CANCELED:
                            break;
                        default:
                            break;
                    }
            }
        }

Below is the logcat.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.camera/com.camera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.exists()' on a null object reference

at android.app.ActivityThread.deliverResults(ActivityThread.java:3758)

at android.app.ActivityThread.handleSendResult(ActivityThread.java:3801)

at android.app.ActivityThread.access$1400(ActivityThread.java:157)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.exists()' on a null object reference

at com.camera.MainActivity.onActivityResult(MainActivity.java:43)

at android.app.Activity.dispatchActivityResult(Activity.java:6463)

at android.app.ActivityThread.deliverResults(ActivityThread.java:3754)

at android.app.ActivityThread.handleSendResult(ActivityThread.java:3801) 

at android.app.ActivityThread.access$1400(ActivityThread.java:157) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405) 

at android.os.Handler.dispatchMessage(Handler.java:102)   at android.os.Looper.loop(Looper.java:148) 

at android.app.ActivityThread.main(ActivityThread.java:5551)  at java.lang.reflect.Method.invoke(Native Method) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

I can't figure out why is this happening. Does anyone know how should I fixed the error? Thanks!

4

4 Answers

1
votes

The Activity is recreated when the user navigates back from the Camera click action to the Activity.

Thus, you need to store the file path in the Bundle within onSaveInstanceState() (make sure to call super.onSaveInstanceState(outState); after calling putString on the outState Bundle), and create the File again within onRestoreInstanceState().

0
votes

Please check if you have the appropriate permissions configured in your app's AndroidManifest.xml
(READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE)

0
votes

You could try to retrieve the image URI from the Intent parameter:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    imageFile = new File(data.getData().toString());
                    if (imageFile.exists())
                    {
0
votes

The only thing I can think of is that you have two imageFile variables, one that you define and send in the intent, and another imageFile global variable that is always null.

This code works perfectly:

public class CameraIntentActivity extends AppCompatActivity {

    private File imageFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_intent);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "abc.jpg");

        Uri store= Uri.fromFile(imageFile);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, store);
        intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 0);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    if (imageFile.exists())
                    {
                        Toast.makeText(this, "The file was saved at " + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(this, "Error saving the file ", Toast.LENGTH_LONG).show();
                    }
                    break;
                case Activity.RESULT_CANCELED:
                    break;
                default:
                    break;
            }
        }
    }