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!