gpt4 book ai didi

android - 使用 ACTION_IMAGE_CAPTURE 拍摄的图像 - 第一个 decodeStream 调用失败,其他正常

转载 作者:搜寻专家 更新时间:2023-11-01 09:47:40 24 4
gpt4 key购买 nike

我正在使用 ACTION_IMAGE_CAPTURE 和预定的目标 Uri,这与文档中的建议非常相似。但是,当我尝试在 Activity 获取图像后立即对其进行解码时,decodeStream() 失败。如果我在几秒钟后再次尝试,它会正常工作。我想文件是在后台异步写入的。我如何知道它何时可用?

以下是我的代码的关键部分:

确定目标文件名:

String filename = String.format("pic%d.jpg", new Date().getTime());
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);

try {
file.createNewFile();
} catch (IOException e) {
file = new File(context.getFilesDir(), filename);
}
targetUri = Uri.fromFile(photoFile);

拍照:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri);
fragment.startActivityForResult(takePictureIntent, RESULT_TAKE_PICTURE);

onActivityResult()中:

if (resultCode == Activity.RESULT_OK) {
if (data != null) {
// Note that data.getData() is null here.
InputStream is = getContentResolver().openInputStream(targetUri);
if (is != null) {
Bitmap bm = BitmapFactory.decodeStream(is);

decodeStream 返回 null。如果我在几秒钟后再次调用相同的电话,它就会成功。有什么可以告诉我文件何时可用吗?

更新:按照 greenapps 的建议,我首先使用 inJustDecodeBounds 进行 decodeStream 调用以获取尺寸以查看它是否是内存问题。事实证明,第一个仅限边界的解码过程失败了,但现在紧随其后的实际 decodeStream 调用成功了!如果我再做一次,它们都会成功!

所以似乎第一次调用 decodeStream 总是失败,之后的所有其他调用都很好,即使它们紧接着发生(=在同一方法中)。所以这可能不是异步写入的问题。但还有别的。但是什么?

最佳答案

if (requestCode == Utility.GALLERY_PICTURE) {

Uri selectedImageUri = null;
try {
selectedImageUri = data.getData();
if (mImgProfilePic != null) {
// mImgProfilePic.setImageURI(selectedImageUri);
mImgProfilePic.setImageBitmap(decodeUri(getActivity(),
selectedImageUri, 60));
// decodeUri

}
} catch (Exception e) {

}
// //////////////
try {
// Bundle extras = data.getExtras();
// // get the cropped bitmap
// Bitmap thePic = extras.getParcelable("data");
// mImgProfilePic.setImageBitmap(thePic);

final Uri tempUri = selectedImageUri;
Log.d("check", "uri " + tempUri);
// http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=companylogo
upLoadServerUri = "http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=employee_profile_pic&image=";
upLoadServerUri = Utility.EMPLOYEE_PROFILE_PIC_URL
+ "&employee_id=" + empId;
dialog = ProgressDialog.show(getActivity(), "",
"Uploading file...", true);

new Thread(new Runnable() {
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
// messageText.setText("uploading started.....");
}
});
uploadFilePath = getRealPathFromURI(tempUri);
uploadFile(uploadFilePath + "");
// uploadFile(tempUri+"");
}
}).start();

} catch (Exception e) {

}

// ///////

}

关于android - 使用 ACTION_IMAGE_CAPTURE 拍摄的图像 - 第一个 decodeStream 调用失败,其他正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37202044/

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