无法从相机解码位图文件

时间:2016-01-30 15:43:15

标签: android

我试图用原生设备相机拍照。林'从发送意图开始:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getMainActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

相机启动,文件已创建,我正在拍照,然后使用此代码接收并解码此照片:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

        if (bitmap == null) {
            Crashlytics.log("Bitmap factory returned null");
        }

        mImageCropper.setBitmapPhoto(bitmap);
        expand(mMainImage);
    }
}

这是创建文件并记住它的路径的方法:

private File createImageFile() throws IOException {
    // Create an image file name
    @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    if (!image.exists()) {
        Log.e("take photo", "can't create photo file");
        Crashlytics.logException(new NullPointerException("Can't read photo from camera"));
    } else {
        Log.d("take photo", "photo file created " + image.getPath());
    }

    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

有时我在解码此位图时遇到错误:

D/skia: --- SkImageDecoder::Factory returned null

当我重新启动设备(Nexus 5(6.0 Marshmallow),但它也发生在其他设备上)时,首先运行此过程即可,但随后会出现错误。我无法在genymotion模拟器(Nexus 5,5.0)上重现此错误

更新 - 原因和解决方法

在将位图完全写入持久存储之前,本机Android相机应用程序似乎返回了它的结果。我不感兴趣的代码,但仍然有效:

final Handler handler = new Handler();

@Override
public void onActivityResult(final int requestCode, final int resultCode, Intent data) {
    Runnable decodeRunnable = new Runnable() {
        int counter = 0;

        @Override
        public void run() {
            if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
                Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

                if (bitmap == null && counter < 20) {
                    handler.postDelayed(this, 200);
                }
                mImageCropper.setBitmapPhoto(bitmap);
                expand(mMainImage);
            }
        }
    };
    handler.postDelayed(decodeRunnable, 100);
}

如您所见,此方法将尝试解码位图最多20次,尝试之间间隔为200毫秒。如果您有更好的想法,那将是受欢迎的。

1 个答案:

答案 0 :(得分:0)

我创建了一个AsyncTask来处理这个问题,直到我们找到一种更优雅的方式来处理这个问题,我决定使用this one here