拍照时意图始终为空

时间:2014-04-08 07:05:45

标签: java android android-intent

我正在尝试拍照并将图像放入ImageView中。当代码到达onActivityResult时,Intent始终为null。就我的研究而言,代码似乎是正确的,但我无法让它发挥作用。

@Override
public void onClick(View v) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && data != null) {
        Bundle extras = data.getExtras();
        Bitmap bmap = (Bitmap) extras.get("data");
        switchImage(bmap);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码拍照。这适用于也加载了自定义roms的三星设备:

点击捕获按钮:

private void cameraCapture() {
        File photoFile = null;
        if (pic_number >= 5) {
            Toast.makeText(getApplicationContext(), "Maximum amount of pictures taken", Toast.LENGTH_LONG).show();
        } else {
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "File not created", Toast.LENGTH_LONG).show();
            }
            if (photoFile != null) {
                Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                camera_picture_uri = photoFile.toString();
                camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(camIntent, pic_number);
                pic_number++;
            }
        }
    }


private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getApplicationContext().getExternalFilesDir(null);
        File image = File.createTempFile(imageFileName, ".jpg", storageDir);
        return image;

    }

在onActivityResult中:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        setCameraPicture(data);
    }

private void setCameraPicture(Intent data) {
        // TODO
        // Auto-generated method stub
        Bitmap bm = null;
        try {
            File file = new File(camera_picture_uri);
            ExifInterface ei = new ExifInterface(camera_picture_uri);
            int rotation = 0;
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                rotation = 90;
                bm = rotateCameraImage(rotation, file);
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                rotation = 180;
                bm = rotateCameraImage(rotation, file);
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                rotation = 270;
                bm = rotateCameraImage(rotation, file);
            } else {
                bm = rotateCameraImage(rotation, file);
            }

            // Set bitmap to imageview

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }


private Bitmap rotateCameraImage(int rotation, File f) throws FileNotFoundException {
        // TODO Auto-generated method stub
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);

        BitmapFactory.Options o = new BitmapFactory.Options();
        int scale = 8;
        o.inSampleSize = scale;
        o.inPurgeable = true;
        o.inInputShareable = true;

        Bitmap sourceBitmap = BitmapFactory.decodeFile(f.toString(), o);
        Bitmap bitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
        System.gc();
        return bitmap;
    }

修改

ExIfInterface用于检查图像需要设置的旋转才能正确显示