Android 4.4中的相机照片捕获失败

时间:2014-09-10 10:19:43

标签: android

我有以下代码用于从我的活动中启动相机应用程序:

private Uri imageUri;
private void takeCameraPhoto() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

当用户拍摄照片并选择它时,我会在我的onActivtyResult方法中从Android获得回调,我写的如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            tempView.setVisibility(View.INVISIBLE);
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ContentResolver cr = getContentResolver();

            try {
                imageBMP = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int width = getResources().getDisplayMetrics().widthPixels;
            int height = getResources().getDisplayMetrics().heightPixels;
            String newFilePath = selectedImage.toString().replace("file:///mnt/", "/");


            imageBMP = ShrinkBitmap(newFilePath, width, height);
            imageView.setImageBitmap(imageBMP);
            Matrix m1= imageView.getImageMatrix();
            RectF drawableRect1 = new RectF(0, 0, imageBMP.getWidth(), imageBMP.getHeight());
            RectF viewRect1 = new RectF(0, 0, width - 50, height - 100);
            m1.setRectToRect(drawableRect1, viewRect1, Matrix.ScaleToFit.CENTER);
            matrix.set(m1);
            imageBMP = null;
            System.gc();
            /*Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();*/
        }else{
            if (!isFromDialog) {
                PhotoDecorationActivity.this.finish();
            }

        }

        break;

在android 4.4中,我得到NullPointerException,表示imageBMP为null,这可能是因为null newFilePath变量。在我的活动中从相机捕获图像并接收图像的正确代码是什么。有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

试试这个

//This code is for launching the camera activity

    Integer cam = 0;
    Boolean gpicture;
    Intent action = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(action, cam);



//This is the code used to handle the request and display it on a bitmap

public void onActivityResult(int requestCode, int resultCode, Intent data) {

//process image gotten from gallery
                if (requestCode == cam && resultCode == Activity.RESULT_OK) {

                    gpicture = true;

                    Uri selectedImage = data.getData();
                    final String location = data.getDataString();

                    Bitmap bitmap = null;

                    try {
                        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    pic.setImageBitmap(bitmap);
}
    }

此代码仅用于从相机获取图像并将其显示在位图上,希望它可以帮助您