Android相机不保存图片

时间:2013-07-20 19:12:17

标签: android android-camera

嘿,我正在编写代码来捕获图像,但我的onPictureTaken方法从未被调用过......我在哪里错了?

我的代码是

btnCapture.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            cam.takePicture(null, null, new TakeMyPicture());
        }
    });



   class TakeMyPicture implements PictureCallback
{

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        imagebytearray=data;            
        Toast.makeText(getApplicationContext(), "Image Captured",5).show();
    }

}

1 个答案:

答案 0 :(得分:0)

严格取决于http://developer.android.com/training/camera/photobasics.html

请求使用相机的权限

<uses-feature android:name="android.hardware.camera" />

拍照

private void dispatchTakePictureIntent(int actionCode) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, actionCode);
}

保存照片

storageDir = new File(
    Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES
    ), 
    getAlbumName()
);

设置文件名

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = 
        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
    File image = File.createTempFile(
        imageFileName, 
        JPEG_FILE_SUFFIX, 
        getAlbumDir()
    );
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

通过Intent将位置传递给相机应用

File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));