相机意图的onActivityResult代码即使用户未接受照片,也会保存(空白)图像

4
当用户点击叉号不接受照片时,它会以与接受所拍摄照片相同的方式结束意图。它会将文件保存到设备画廊,但是文件为空。点击叉号难道不意味着resultCode != RESULT_OK吗?我是否遗漏了其他检查?谢谢。以下是代码。等等,我在活动结果之前保存了图像...这是一个有缺陷的系统,但它出现在官方Android开发者网站上。如果有人能提供修复建议,我会非常感激,因为我曾经在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更改为以下内容,但结果没有改变(空白图像仍然存在于我的相册中,deleted的值为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());
              }

编辑 好的,我相信在删除后需要使用MediaScannerIntent扫描SD卡的适当区域,以便它显示出来,因为现在它似乎已经可以工作了。

1个回答

2
你是否使用createImageFile()创建文件? 你可以保存photoFile,如果result!=RESULT_OK则删除它。
另外,相机应用(甚至是默认应用程序)可能会返回错误的结果。在日志中检查它。如果有问题,不要依赖结果并检查已创建文件的大小。如果大小为0,则删除它。

谢谢,听起来不错。明天我会试一下。或者只在onActivityResult中保存可能对应用程序的工作量较小,尽管我怀疑它是否会产生任何性能差异。 - user3164083
我尝试了一下,但是空白图片仍然在相册中。我把我的尝试放在了问题的底部。请注意,在createImageFile()中,mCurrentPhotoPath被设置为file.getAbsolutePath。谢谢。 - user3164083
现在它可以工作了。也许只需在答案中添加一句话,提醒不要忘记在删除后使用MediaScannerIntent扫描SD卡的适当区域,否则删除操作可能不会立即显示出来,这会令人困惑。谢谢! - user3164083
@user3164083 听起来有点太复杂了。也许你可以尝试在 createImageFile() 中不创建实际的文件(但仍然创建一个带有效路径的有效文件对象)。 - Sam
这很复杂。当我重构时,我会考虑你的建议。 - user3164083

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接