摄像头意图:拍照后数据为空

时间:2018-01-20 15:20:40

标签: android android-camera-intent

嗨所以我想用相机意图拍照但拍完照片后我得到以下错误:

ERROR:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {radautiul_civic.e_radauti/radautiul_civic.e_radauti.Civic_Alert}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:5004)
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5047)
                     at android.app.ActivityThread.access$1600(ActivityThread.java:229)
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                     at android.os.Handler.dispatchMessage(Handler.java:102)
                     at android.os.Looper.loop(Looper.java:148)
                     at android.app.ActivityThread.main(ActivityThread.java:7331)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                     at radautiul_civic.e_radauti.Civic_Alert.onActivityResult(Civic_Alert.java:86)
                     at android.app.Activity.dispatchActivityResult(Activity.java:7165)
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:5000)

这是我的代码:

 static final int REQUEST_TAKE_PHOTO = 1;
 private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

这是我的StartActivityForResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        imgBitmap = (Bitmap) data.getExtras().get("data");
        img.setImageBitmap(imgBitmap);
        Toast.makeText(getApplicationContext(),mCurrentPhotoPath,Toast.LENGTH_SHORT);
    }
}

所以这就是我在错误之前所做的事情:1。打开我的相机意图2.勾画图3.我按下确定,我得到结果中的数据为空错误

我从谷歌跟踪此文档:https://developer.android.com/training/camera/photobasics.html#TaskGallery 所以你们能告诉我我做错了什么吗?提前致谢

1 个答案:

答案 0 :(得分:0)

如果您使用相机意图传递额外参数MediaStore.EXTRA_OUTPUT,则相机活动会将捕获的图像写入该路径,并且不会以onActivityResult方法返回位图。

如果您要检查通过的路径,那么您将知道实际的相机已将捕获的文件写入该路径。

所以你需要对代码进行一些更改

要启动相机活动,请使用

Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

捕获图像后,您将在onActivityResult方法中以位图格式获取捕获的图像。现在,当您获得位图时,可以根据需要使用它。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
   if (requestCode == 1) {  
        Bitmap bmp = intent.getExtras().get("data");
        //Use the bitmap as you want to
   }  
} 

注意:此处位图对象由拇指图像组成,它不具有全分辨率图像

一些有用的参考资料

http://dharmendra4android.blogspot.in/2012/04/save-captured-image-to-applications.html

How to get camera result as a uri in data folder?