应用程序崩溃与Intent MediaStore.ACTION_IMAGE_CAPTURE

时间:2014-03-23 23:45:52

标签: android android-camera

我试图从相机中挑选一张图片,但我的应用程序崩溃了。我是这样做的:

public void sendImageFromCamera (View v) {
    Uri fileUri;
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Creating file to save
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "PickImageSample");

    // Create app directory directory if it doesn't exists
    if (!mediaStorageDir.exists()){
        if (!mediaStorageDir.mkdirs()){
            Log.e("IMAGE FROM CAMERA", "failed to create directory");
        }
    }

    // Create file itself
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");


    fileUri = Uri.fromFile(mediaFile);

    Log.d("FILE URI", fileUri.toString()); // Prints correctly the file path

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    startActivityForResult(cameraIntent, 2);
}

@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == 2) { // Request from camera
         if (resultCode == RESULT_OK) {
             Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
         }
     }
 }

我按照Android指南完成了它:https://developer.android.com/guide/topics/media/camera.html

我的堆栈错误是:

03-23 20:33:24.148: E/AndroidRuntime(30452): FATAL EXCEPTION: main
03-23 20:33:24.148: E/AndroidRuntime(30452): Process: br.com.sampleApp.activities, PID:  30452
03-23 20:33:24.148: E/AndroidRuntime(30452): java.lang.RuntimeException: Failure  delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity  {br.com.sampleApp.activities/br.com.sampleApp.activities.Chat}: java.lang.NullPointerException
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3446)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3489)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.ActivityThread.access$1300(ActivityThread.java:139)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.os.Looper.loop(Looper.java:136)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.ActivityThread.main(ActivityThread.java:5102)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at java.lang.reflect.Method.invokeNative(Native Method)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at java.lang.reflect.Method.invoke(Method.java:515)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at dalvik.system.NativeStart.main(Native Method)
03-23 20:33:24.148: E/AndroidRuntime(30452): Caused by: java.lang.NullPointerException
03-23 20:33:24.148: E/AndroidRuntime(30452):    at br.com.unichat.activities.Chat.onActivityResult(Chat.java:322)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.Activity.dispatchActivityResult(Activity.java:5446)
03-23 20:33:24.148: E/AndroidRuntime(30452):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3442)
03-23 20:33:24.148: E/AndroidRuntime(30452):    ... 11 more

对于我看到的方法是receiveng null信息,但我无法弄清楚为什么。可能是简单的东西,但它已经卡在我的脑海中,我不能以其他方式看待它,你们可以帮助我吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

你应该把它改成这个

if (!mediaStorageDir.exists()){
    if (!mediaStorageDir.mkdirs()){
        Log.e("IMAGE FROM CAMERA", "successfully created directory");


startActivityForResult(cameraIntent, 3);


 if (requestCode == 3) { // Request from camera
     if (resultCode == RESULT_OK) {
         Toast.makeText(this, "Image saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show(camera);