【问题标题】:Android - Front camera errorAndroid - 前置摄像头错误
【发布时间】:2014-10-14 02:38:26
【问题描述】:

我正在尝试捕获图像,但是在捕获并批准后,onActivityResult(int requestCode, int resultCode, Intent data) data 始终为空。

这就是我对相机的称呼:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
startActivityForResult(intent, Consts.ACTION_JOURNEY_CAPTURE_PHOTO_PATH);

方法getImageUri():

 private Uri getImageUri() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File albumF = helpers.getAlbumDir(getString(R.string.album_name));

        File file = new File(albumF, imageFileName);
        Uri imgUri = Uri.fromFile(file);

        return imgUri;
}

在清单上我有:

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

我做错了什么?

【问题讨论】:

  • data 返回一个缩略图(非常小)图像,可用于大致了解捕获的图像是关于什么的。某些设备可能根本不生成缩略图,或者只为主(后)摄像头生成缩略图。 data == null 并不表示错误。

标签: android camera


【解决方案1】:

图像存储在您使用 getImageUri() 方法获得的路径中。您必须保留该路径并在 onActivityResult() 内部执行以下操作:

    if (resultCode == RESULT_OK) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, options);

        Bitmap b = BitmapFactory.decodeFile(photoPath, options);

    }

如果你想调整图片的大小,你可以设置你的BitmapFactory.Options的inSampleSize,这个方法将有助于计算inSampleSize:

 private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

【讨论】:

  • 谢谢,这就是解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多