Camera intention ActivityResult代码保存(空白)图像,即使用户不接受照片



当用户点击十字架不接受照片时,它会以接受他们拍摄的照片时的方式结束意图。它将文件保存到设备库中。但它是空白的。点击十字不应该意味着resultCode!=结果OK?还有一张支票我丢了吗?谢谢这是代码。等等,我正在保存活动结果之前的图像。。。这是一个有缺陷的系统,但它出现在安卓开发者的官方网站上。如果有人能建议修复,我会非常高兴,因为我过去常常把图像保存在onActivtyResult中,但它在一些手机上不起作用,导致了异常,所以我改成了这个。

启动意图:

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(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}  
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,  /* prefix */
".jpg",         /* suffix */
storageDir      /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
ih.galleryAddPic(mCurrentPhotoPath, this.getApplicationContext());
return image;
}

onActivityResult:中的摄像头意图案例

else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == RESULT_OK)){
mProfilePicPath = mCurrentPhotoPath;
mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath, 
GlobalConstants.PROFILE_PICTURE_RESOLUTION, 
GlobalConstants.PROFILE_PICTURE_RESOLUTION);
TextView tv = (TextView) findViewById(id.ProfilePicText);
tv.setText(mProfilePicPath);
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}
}

编辑:我将onActivityResult更改为此,但无效(空白图像随后仍在我的图库中,删除的值为true):

else if (requestCode == REQUEST_TAKE_PHOTO){
if(resultcode == RESULT_OK){
File f = new File(mCurrentPhotoPath);
mProfilePicPath = null;
if (f.exists()) {
if (f.length() != 0){
mProfilePicPath = mCurrentPhotoPath;
mPortraitPhoto = ih.decodeSampledBitmapFromImagePath(mCurrentPhotoPath, 
GlobalConstants.PROFILE_PICTURE_RESOLUTION, 
GlobalConstants.PROFILE_PICTURE_RESOLUTION);
TextView tv = (TextView) findViewById(id.ProfilePicText);
tv.setText(mProfilePicPath);
}
else {
boolean deleted = f.delete();
if (deleted == true){
Log.d("camera0", "deleted");
}
else{
Log.d("camera0", "not deleted");
}
}
}
}
else{
File f = new File(mCurrentPhotoPath);
boolean deleted = f.delete();
if (deleted == true){
Log.d("camera", "deleted");
}
else{
Log.d("camera", "not deleted");
}
}
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}

编辑好的,我想我需要在删除后用MediaScannerContent扫描SD卡的适当区域,以便它显示出来,就像现在一样。

您不是在用createImageFile()创建文件吗?你可以保存照片文件和结果=RESULT_OK删除

顺便说一下,相机应用程序(即使是默认的)可能会返回错误的结果。请在日志中查看。如果他们这样做了,就不要依赖结果;检查已创建文件的大小。如果==0-删除

最新更新