i have problem using camera in my Android application
i have a form for save data, there is a button to Take Picture with camera and the captured image from the camera will be placed on ImageView in my form.
there’s no problem on calling camera intent and placing the image in the form
my problem is :
in the camera application,
if i press back button (back to the form) my application crashed.
why?
this is the code on my button that call the camera
btn_takepic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
this is my onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final EditText et_foto = (EditText) findViewById(R.id.et_foto);
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imv_foto);
image.setImageBitmap(thumbnail);
//toastkeun(data.getExtras().get("data").toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
i’m testing on real device
this is my logCat
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): FATAL EXCEPTION: main
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Looper.loop(Looper.java:123)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at dalvik.system.NativeStart.main(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): Caused by: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.ngimagrid.nigmago.FormAsetTambah.onActivityResult(FormAsetTambah.java:246)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): ... 11 more
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): FATAL EXCEPTION: main
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Looper.loop(Looper.java:123)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at dalvik.system.NativeStart.main(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): Caused by: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.ngimagrid.nigmago.FormAsetTambah.onActivityResult(FormAsetTambah.java:246)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): ... 11 more
from this line, i think that’s the problem…
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
there’s no result returned right?
how to fix that?
thanks
cmiiw
I don’t think this is coming from your app. you can try to surround the whole onActivity result code in a try/catch block. i think what you are experiencing is a Camera bug on some of the phones. also try to try around different combinations of the extras you are sending on startActivityForResult intent.
Also always check the result code before you do anything. you dont want to make the same code if the result is canceled(Activity.RESULT_CANCELED) or ok (Activity.RESULT_OK )
also there is a bug where the result doesn’t come in the EXTRA_OUTPUT (i think it was named like that) but instead it comes as an Uri in intent.getData(). extra output is you providing a path where to save the file and the uri return is usually the image saved in the default camera location. its a bit different as you will see.
You are not using flags atm but you should if you want the image location as a result. Search stack for some sample code for the extras.