0

I'm trying the following:

1. Click a button to take a photo.
2. Save photo.
3. Add photo into the gallery.
4. Show photo into an ImageView.

1 and 4 works fine, but I'm having problems with 2 and 3.

This is my code:

photoFile = createImageFile();

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp;
    File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageFileName + ".jpg");

    mCurrentPhotoPath = imageFile.getAbsolutePath();

    return imageFile;
}

With this I have a filePath created where I want to store the image.

Now I call the Intent with extra params to store the image:

if (photoFile != null) {                   
    Uri photoURI = FileProvider.getUriForFile(getContext(),
                     "es.antonio.prueba.fileprovider",
                     photoFile);
     takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
    startActivityForResult(takePhotoIntent, REQUEST_CAMERA);
 }

Now, in my onActivityResult I call a function to add the photo into the gallery:

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    getContext().sendBroadcast(mediaScanIntent);
}

And another one to set the photo into the ImageView:

private void setPic(ImageView mImageView) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView.setImageBitmap(bitmap);
}

I'm struggling with save the picture taken into the SD because my method is not working (I'm followind Android Developers tutrial). I thought that passing an extra to the Intent should do te the trick but it's not working.

Any hint?

canillas
  • 414
  • 3
  • 13
  • Permissions in manifest written? – Ivan Nov 22 '17 at 09:23
  • Yes, added for camera and read/write external storage – canillas Nov 22 '17 at 09:30
  • `createNewFile();` returns true or false. You are not checking the return value. Please add code to do so and toasts to inform the user. – greenapps Nov 22 '17 at 09:35
  • please go through this link https://stackoverflow.com/questions/47412992/taking-a-picture-with-a-camera-intent-and-saving-it-to-a-file/47413309#47413309 – Vikas singh Nov 22 '17 at 09:35
  • @greenapps if the image is set into the ImageView it's because the file is created correctly, no? – canillas Nov 22 '17 at 09:37
  • @user7348352 this code save a compressed image, I want to save it full sized – canillas Nov 22 '17 at 09:37
  • `With this I have a file created where I want to store the image.`. You should not already create that file. Leave creation to the camera app that you invoke. You only need a path to a file -that does not have to exist-. – greenapps Nov 22 '17 at 09:38
  • @greenapps so I need to the avoid `imageFile.createNewFile();`? – canillas Nov 22 '17 at 09:39
  • `it's because the file is created correctly, no? `. Yes and no. It can be that it is not created by your app but by the camera app. But again: your app should not try to create that file already. So remove createNewFile() indeed. – greenapps Nov 22 '17 at 09:40
  • `struggling with save the picture taken into the SD `. There is nothing in your code that has anything to do with a removable micro SD card. Your code uses getExternalStorage... And thats not the SD card. – greenapps Nov 22 '17 at 09:48
  • Further i do not understand your strugling. If the picture is set then all works isnt it? – greenapps Nov 22 '17 at 09:49
  • The picture is set in the imageView but when I stop the app the file is not in my filesystem – canillas Nov 22 '17 at 09:50
  • you can save images without compress=https://stackoverflow.com/questions/37730365/how-can-i-save-output-image-from-camera-directly-to-file-without-compression – Vikas singh Nov 22 '17 at 09:51
  • @greenapps I have deleted createfile but still not working – canillas Nov 22 '17 at 10:01
  • The camera app should save the picture to the file you indicated. Only if the camera app saved the picture to file you can later construct a bitmap from that file and set that bitmap to an image view. So if you see that picture in that imageview saving works. – greenapps Nov 22 '17 at 11:10
  • `when I stop the app the file is not in my filesystem`. Please tell exactly what you do if you search for a file on the file system. – greenapps Nov 22 '17 at 11:11
  • @greenapps I check the gallery and filesystem with file explorer and the picture is not saved – canillas Nov 22 '17 at 11:13
  • Checking with the Gallery app makes no sense. Well not yet. And what do you mean with 'file explorer'. Which one? Where? – greenapps Nov 22 '17 at 11:14
  • ES file Explorer into sd and internal. Check with gallery have sense because the method galleryAddPic should update the gallery and show the new picture – canillas Nov 22 '17 at 11:16
  • Using ES File Explorer app is ok. Now did you find the Pictures directory? Why arent you telling what you see? I keep asking for info. Tell complete path of Pictures directory please. – greenapps Nov 22 '17 at 11:18
  • Got it! Thanks dude! It was a miss call to the file provider – canillas Nov 22 '17 at 11:30

1 Answers1

0

SOLVED: It was a miss call to the FileProvider

Updated createImageFile()function:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = timeStamp;
    File album = getAlbumStorageDir("MiBodaAPP");
    File imageFile = File.createTempFile(imageFileName, ".jpg", album);

    mCurrentPhotoPath = imageFile.getAbsolutePath();

    return imageFile;
}

Update onActivityResult:

takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getContext(), "es.antonio.prueba.fileprovider", photoFile));

Here, I was taking the value EXTRA_OUTPUT param incorrectly because I was getting the path from the Environment and not form the FileProvider.

canillas
  • 414
  • 3
  • 13
  • `File.createTempFile(imageFileName, ".jpg", album);`. Wrong! Bad! Do not create that file already. You only need a file name/path. Your original code was ok. – greenapps Nov 22 '17 at 11:48
  • Android Developers Tutorial to take photos make this and now is working... :S – canillas Nov 22 '17 at 11:49
  • Yes i know that it comes from that tutorial. How wrong and bad they can be such tutorials! – greenapps Nov 22 '17 at 11:50