This same code runs like a charm in other devices but in nexus 5 (OS version 5.1.1) am unable to run it.
When I run the below code in nexus 5 I get NullPointerException, and value of resultCode=0 and that of RESULT_OK=-1, thus my if condition in getActivityForResult do not run.
Whereas in other devices (other models like samsung,htc etc) the same code runs like a charm without and exception and the value of resultCode=-1 and RESULT_OK=-1, both the values come same , so the if condition runs and thus the rest of my program.
JAVA CODE mainActivity.java
public void callCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
Log.i("hello", "callCamera");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("hello", "" + requestCode);
Log.i("hello", "" + resultCode);
Log.i("hello", "" + this.RESULT_OK);
if (requestCode == 100 && resultCode == RESULT_OK) {
// fileUri=data.getExtras().get("data");
photo = (Bitmap) data.getExtras().get("data");
//*selectedImage = data.getData();
//photo = (Bitmap) data.getExtras().get("data");
Log.i("hello","onActivityResult");
uploadImage();
}
}
LOGCAT IN CASE OF NEXUS 5
FATAL EXCEPTION: main Process: com.google.android.GoogleCamera, PID: 24887
java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:187)
at com.google.common.base.Optional.of(Optional.java:84)
at com.android.camera.captureintent.state.StateSavingPicture.onEnter(StateSavingPicture.java:77)
at com.android.camera.captureintent.stateful.StateMachineImpl.jumpToState(StateMachineImpl.java:62)
at com.android.camera.captureintent.stateful.StateMachineImpl.processEvent(StateMachineImpl.java:110)
at com.android.camera.captureintent.state.StateOpeningCamera$9.onClick(StateOpeningCamera.java:307)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Remove the intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
line from callCamera() method, this will fix the crash. As if you are getting the uri from the onActivityResult() intent then the intent will recived data as null since you are putting a extra value of uri in the intent at time of starting the camera intent, this defines that you already have a file uri for your camera image and you don't want a data intent in onActivityResult(), also i will suggest you to remove the fileuri variable used inside the callCamera() method as it is of no use.
so your callCamera method should look like:
public void callCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
}