在模拟器中拍照后相机崩溃

时间:2013-04-22 21:23:02

标签: java android android-emulator android-camera

我正在开发一款通过Intent将用户带到相机的应用。我一直在记事本演示的代码里面工作。当我拍照并确认时会出现问题。应用程序崩溃,logcat给了我这个:

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

我现在唯一的领导是它与我如何配置onActivityResult或我在哪里放置代码来保存图像文件有关。现在我在一项活动中拥有所有这些:

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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在使用意图中的数据发布Toast之前,您应该测试它是否为空

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

在您的情况下,您的Activity返回null,可以在此行的logcat中看到

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