Pergunta

My app has a form where it is required to either select a picture from the gallery or to take a photo.

For that purpose, there are two buttons. The one doing the gallery pick is working fine.

The problem comes with the one that lets you take a photo. When clicked, it successfully opens the camera. Then, you can take a picture and it will ask you wether you want to keep it or discard it.

If you choose to save it, it will try to get back to the app but it will crash.

The logcat prompts this error.

11-30 23:20:06.288: ERROR/AndroidRuntime(3045): FATAL EXCEPTION: main
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): java.lang.RuntimeException: Unable to resume activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.os.Looper.loop(Looper.java:130)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.main(ActivityThread.java:3687)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at java.lang.reflect.Method.invoke(Method.java:507)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at dalvik.system.NativeStart.main(Native Method)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.upvar/com.android.upvar.NewPOI}: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     ... 12 more
11-30 23:20:06.288: ERROR/AndroidRuntime(3045): Caused by: java.lang.NullPointerException
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at com.android.upvar.NewPOI.onActivityResult(NewPOI.java:130)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-30 23:20:06.288: ERROR/AndroidRuntime(3045):     ... 13 more

This is the button listener that starts the Activity:

    //mTakeImg is the button
    mTakeImg.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
        }
    });

And this is the Activity result method that takes the response:

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

        switch(requestCode) { 
            case ACTIVITY_SELECT_IMAGE:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    mImg.setText(selectedImage.getPath());
                    InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        mImage.setImageBitmap(yourSelectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
                break;
            case CAMERA_PIC_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri selectedImage = imageReturnedIntent.getData();
                    mImg.setText(selectedImage.getPath());
                    InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                        mImage.setImageBitmap(yourSelectedImage);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   
                }
                break;
        }
    }

I don't see where the error is coming from. I followed the tutorials to do this and they look the same as my code. So any help would be appreciated.

Thanks.

Foi útil?

Solução

Assuming you initialised mImg somewhere else (since you try to call setText on it), try checking if selectedImage is null. The fact that getData() would return null was a known bug on some devices, and I'm not sure it's been fixed.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top