制作照片后,无法从相机接收Intent.extras()

时间:2015-04-26 08:37:57

标签: android android-camera-intent android-bundle

我发现这是捕捉照片和接收全尺寸照片而不是缩略图的常见问题(根据:http://developer.android.com/training/camera/photobasics.html)。拍摄照片和接收缩略图很容易,但教程的其余部分似乎不完整而且无法正常工作。有人轻松解决了这个问题吗?

public class TakePhoto extends Activity{

static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
private String mCurrentPhotoPath;
private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.take_photo);

    mImageView = (ImageView) findViewById(R.id.imageView);

    dispatchTakePictureIntent();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

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
            Log.i("ASD", ex.toString());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

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 = "file:" + image.getAbsolutePath();
    return image;
}

}

04-25 18:01:14.239  17281-17281/com.(package).bazaar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.(package).bazaar, PID: 17281
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.(package).bazaar/com.(package).bazaar.TakePhoto}: 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:3626)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
        at android.app.ActivityThread.access$1300(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.klangstudios.bazaar.TakePhoto.onActivityResult(TakePhoto.java:46)
        at android.app.Activity.dispatchActivityResult(Activity.java:6161)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
            at android.app.ActivityThread.access$1300(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

3 个答案:

答案 0 :(得分:7)

我不希望data在那里null。但是,Intent上不应该有任何额外内容。如果您指定"data",则只会查找EXTRA_OUTPUT个额外内容。如果您要指定EXTRA_OUTPUT,则可以从EXTRA_OUTPUT中提供的路径中获取照片,并忽略Intent传递给onActivityResult()

关于null data Intent本身,这可能是您正在使用的相机应用程序所特有的。请注意,使用ACTION_IMAGE_CAPTURE表示您依赖第三方应用来拍照,而第三方应用可能会有错误。

答案 1 :(得分:0)

这可能有所帮助:Android Camera Intent: how to get full sized photo?

虽然正在做的是在启动相机意图之前读取您创建的文件,而不是阅读data

答案 2 :(得分:0)

除了“ Jean d'arme”之外,我在同一个问题上付出了数小时的努力,只是我试图获取URI而不是Bitmap。为了节省其他用户的时间,我将通过以下步骤重点介绍“ jean d'arme”和“ Felipe Augusto”的说明,它们帮助我解决了错误:

1)在您的课程中添加一个全局变量

private Uri uri;

2)使用此变量获取dispatchTakePictureIntent()中photoURI的内容

private void dispatchTakePictureIntent() {

        {...}

        if (photoFile != null) {
            uri = photoURI;
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

3)在onActivityResult方法中将此Uri用于您要完成的任务

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        //Perform any task using uri
        //For example set this URI to fill an ImageView like below
        this.imageView.setImageURI(uri);
    }
}

希望这一亮点对您有所帮助!