0

我按照android开发人员的指南使相机工作但事件我没有忘记任何我有空异常的东西

这是代码:

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

public class MakePhotoActivity extends Activity {

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
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), "MyCameraApp");
    // 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("MyCameraApp", "failed to create directory");
            return null;
        }else{
            Log.d("MyCameraApp", "success to create directory");
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    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;
}


private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_tuto);

    // 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);
}

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

}

这是我拍照时的例外:

10-10 07:46:12.104: E/AndroidRuntime(1279): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.android_app_test/com.example.android_app_test.MakePhotoActivity}: java.lang.NullPointerException

我该如何解决这个问题

编辑** * *

这是错误的全文:

    10-10 09:02:50.434: D/AndroidRuntime(1735): Shutting down VM
10-10 09:02:50.434: W/dalvikvm(1735): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-10 09:02:50.633: E/AndroidRuntime(1735): FATAL EXCEPTION: main
10-10 09:02:50.633: E/AndroidRuntime(1735): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.android_app_test/com.example.android_app_test.MakePhotoActivity}: java.lang.NullPointerException
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.os.Looper.loop(Looper.java:137)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at java.lang.reflect.Method.invoke(Method.java:525)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at dalvik.system.NativeStart.main(Native Method)
10-10 09:02:50.633: E/AndroidRuntime(1735): Caused by: java.lang.NullPointerException
10-10 09:02:50.633: E/AndroidRuntime(1735):     at com.example.android_app_test.MakePhotoActivity.onActivityResult(MakePhotoActivity.java:88)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.Activity.dispatchActivityResult(Activity.java:5322)
10-10 09:02:50.633: E/AndroidRuntime(1735):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
10-10 09:02:50.633: E/AndroidRuntime(1735):     ... 11 more
4

0 回答 0