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

标签 android android-intent android-camera-intent onactivityresult

当用户单击叉号表示不接受照片时,它会以与接受他们拍摄的照片时相同的方式结束 Intent 。它将文件保存到设备库中。但它是空白的。单击叉号不应该意味着 resultCode != RESULT_OK 吗?我还缺少一张支票吗?谢谢。这是代码。等等,我正在保存 Activity 结果之前的图像……这是一个有缺陷的系统,但它在官方 Android 开发者网站上。如果有人可以提出修复建议,我将非常感激,因为我以前将图像保存在 onActivtyResult 中,但它在某些手机上不起作用,导致异常,所以我改成了这个。

开始 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(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 中的相机 Intent 案例:

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 卡的适当区域,以便它显示,因为它现在似乎可以工作。

最佳答案

您不是使用 createImageFile() 创建文件吗? 您可以保存照片文件并在结果中!=RESULT_OK 删除它

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

关于android - 即使照片未被用户接受,相机 Intent onActivityResult 代码也会保存(空白)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21669141/

相关文章:

java - 初学者 Android : Prompt to Leave

android - 获取隐式 Intent 打开的应用程序

android - 为什么我的 Android 应用程序中的图像选择器会自动将纵向图像旋转为横向图像?

android - ViewBinder setViewValue 有时只工作

java - Android:用户能够导入文件的存储选项方法

java - 文件提供者 : Failed to find configured root

android - 如果 ID 匹配或文件名匹配,搜索并单击按钮

java - 尝试添加到 ArrayAdapter 时应用程序崩溃

android - dlopen 失败 : library "/system/lib64/libhwuibp.so" not found : Honor 4C 64-bit octa-core CPU

java - 我可以使用 UrlEncodedFormEntity 为多个部分上传图像和文本吗?