android - 无法从相机解码位图文件

标签 android

我正在尝试使用 native 设备相机拍照。我从发送 Intent 开始:

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 毫秒的中断。如果您有更好的想法,我们将不胜感激。

最佳答案

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

关于android - 无法从相机解码位图文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35103658/

相关文章:

android - 在启用自动链接的 TextView 中控制 onclicklistener

android - "Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16"

java - RecyclerView中的Android居中项目

android - 确保 Android 应用程序是从 Play 商店安装的

java - 生成的 GraphQL 类实现查询接口(interface)而不是订阅接口(interface)

android - 如何在 Android 上运行 ProGuard 测试?

Android – 是否可以使用非字符串参数制作 Http-Post?

java - 滚动不起作用时工具栏自动隐藏和显示

java - Android 阶段性进展

android - 相当于 TextInputLayout 的 setTextColor