简体   繁体   中英

Camera crashes after taking photo in emulator

I'm working on an app that brings the user to the camera via an Intent . I've been working inside the code of the Notepad demo. The problem occurs when I take a photo and confirm it. The app crashes and logcat gives me this:

04-10 20:18:29.896: D/gralloc_goldfish(6154): Emulator without GPU emulation detected.
04-10 20:18:35.897: I/Choreographer(6154): Skipped 282 frames!  The application may be doing too much work on its main thread.
04-10 20:18:42.977: W/IInputConnectionWrapper(6154): showStatusIcon on inactive InputConnection
04-10 20:18:44.007: I/Choreographer(6154): Skipped 344 frames!  The application may be doing too much work on its main thread.
04-10 20:18:45.456: D/AndroidRuntime(6154): Shutting down VM
04-10 20:18:45.456: W/dalvikvm(6154): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-10 20:18:45.516: E/AndroidRuntime(6154): FATAL EXCEPTION: main
04-10 20:18:45.516: E/AndroidRuntime(6154): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.android.demo.notepad3/com.android.demo.notepad3.Notepadv3}: java.lang.NullPointerException
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.os.Looper.loop(Looper.java:137)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at java.lang.reflect.Method.invoke(Method.java:511)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at dalvik.system.NativeStart.main(Native Method)
04-10 20:18:45.516: E/AndroidRuntime(6154): Caused by: java.lang.NullPointerException
04-10 20:18:45.516: E/AndroidRuntime(6154):     at com.android.demo.notepad3.Notepadv3.onActivityResult(Notepadv3.java:205)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-10 20:18:45.516: E/AndroidRuntime(6154):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-10 20:18:45.516: E/AndroidRuntime(6154):     ... 11 more

My only lead right now is that it has something to do with how I have onActivityResult configured or where I'm putting the code to save image files. Right now I have all of that in one activity:

public void takePhoto (View view) {
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}


private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "Notepadv3");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("Notepadv3", "failed to create directory");
            return null;
        }
    }


    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(type));
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

Before you use the data from your intent to post your Toast you should test if it isn't null

if (data!=null){
    Toast.makeText(this, "Image saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show();
}

in your case, your Activity returns null, which can be seen in the logcat in this line

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100,
 result=-1, **data=null**} to activity     
{com.android.demo.notepad3/com.android.demo.notepad3.Notepadv3}: 
java.lang.NullPointerException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM