gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:51:18 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com