The onActivityResult camera target retains a (blank) picture, even if the picture was not taken by the user

When the user clicks the cross to not take the photo, he ends the intention in the same way as when taking the photo that they took. It saves the file in the device gallery. But it is empty. Do not click on the cross, what is resultCode! = RESULT_OK? Is there another check I'm skipping? Thank you Here is the code. Wait, I save the image until the result of the operation ... this is an incorrect system, but it was on the official website of Android developers. If someone can suggest a fix, I would be very grateful because I used to save the image in onActivtyResult, and it did not work on some phones, throwing an exception, so I changed this.

To start the intention:

private void dispatchTakePictureIntent() {
              Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              // Ensure that there 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;
          }

Camera Intent Case in 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());
                  }
          }

EDIT: I changed onActivityResult to this, but to no avail (the empty image is still in my gallery after that, and the delete value is 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());
              }

Change Good. It seems to me that I needed to scan the corresponding area of ​​the SD card using MediaScannerIntent after removal so that it appears, as it seems, now.

+3
source share
1 answer

Don't you create a file with createImageFile ()? You can save the photo files and the result! = RESULT_OK delete it

, ( ) . . , . == 0 -

+2

All Articles