开发者

Application Crashed When Pressing Back Button on Android Camera Intent

开发者 https://www.devze.com 2023-03-18 00:49 出处:网络
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

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) d开发者_运维知识库ata.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


The NullPointerException is caused by your code assuming that data.getExtras().get("data") will return something. This is an entirely undocumented parameter and on many (most) devices will return null.

If, as it appears, you're trying to get a thumbnail, you can try something like this, which tries both known (to me, anyway) ways in which to extract a thumbnail from the results of the Camera Intent.

Bitmap thumb;   
if (data.getData() != null) {
    try {
        Uri uri = data.getData();
        thumb = Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(uri.getLastPathSegment()), Images.Thumbnails.MICRO_KIND, null);
        } catch (Exception typeNotSupported) {

        }
    }
    if (thumb == null) {
        thumb = (Bitmap) data.getExtras().get("data");
    }
}

Incidentally I think there's another bug - your onActivityResult should also test that resultCode is OK - if the user cancels from the Camera I believe onActivityResult will still be called.

I hope that's useful.


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.


Yes you can handle back button while on gallery or camera or any other by this way.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode != RESULT_OK) 
    {
    //Here you can handle,do anything you want
    }
//other code
}


I think this can be solved with a simple trick

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)   {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==0)
    {
        return;
    }


Your talking about the youtube video I know, I had the same error.

protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try 
    {    
            Button btn = (Button) findViewById(R.id.takePhoto);
            btn.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                       startActivityForResult(intent, 0);
                }

             });
            iv = (ImageView) findViewById(R.id.imageView);

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号