質問

When the user clicks the cross to not accept the photo, it ends the intent in the same way it does when they accept the photo they took. It saves a file to the device gallery. But it's blank. Shouldn't clicking the cross mean that resultCode != RESULT_OK? Is there one more check I am missing? Thanks. Here's the code. Wait, I'm saving the image before activity result...this is a flawed system, but it was on the official Android Developers website. If someone can suggest a fix I would be very greatful, because I used to save the image in onActivtyResult and it did not work on some phones, causing an exception, so I changed to this.

To start the 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;
          }

The camera intent case within 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 blank image is still in my gallery afterwards, and the value of deleted 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());
              }

Edit Ok I believe I needed to scan the appropriate area of the SD card with a MediaScannerIntent after the delete, for it to show, as it seems to work now.

役に立ちましたか?

解決

Aren't you creating file with createImageFile() ? You may save photoFile and on result!=RESULT_OK delete it

By the way, camera apps(even default) may return wrong result. Check it in logs. If they do, just don't rely on result & check created file's size. If ==0 - delete it

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top