【问题标题】:Android camera does not save picturesAndroid相机不保存图片
【发布时间】:2013-07-20 19:12:16
【问题描述】:

嘿,我正在编写代码来捕获图像,但我的 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();
    }

}

【问题讨论】:

  • LogCat 中有什么值得注意的消息吗?
  • 你确定onClick() 被调用过吗?

标签: android android-camera


【解决方案1】:

完全脱离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));

【讨论】:

  • OP 直接使用Camera 类,而不是Intent
猜你喜欢
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多