1

I want to implement the function of adding a photo in my application. The application must add a photo that the user took in the ImageView and show in the gallery of the phone.

I did the first part, but I can’t realize the second one. I see the file in the phone directory, but it is not added to the gallery. If it is easy for someone, tell me more how to create your own catalog in the phone gallery.

public File createFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
);
currentPhotoPath = "file://" + image.getAbsolutePath();
return image;

}

The function of adding photos to the gallery (took from Google's guide https://developer.android.com/training/camera/photobasics)

private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
Log.d("camera", "galleryAddPic: image add");

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (resultCode == RESULT_OK) {
    if (requestCode == REQUEST_IMAGE_CAPTURE){
      Photo.setImageURI(Uri.parse(currentPhotoPath));
      galleryAddPic();
      Log.d("camera", "onActivityResult: photo added");
    }
    else if (requestCode == REQUEST_CATEGORY) {
        presenter.detailCategory((String) data.getSerializableExtra("category"));
    }
}
OhHiMark
  • 55
  • 1
  • 5
  • Possible duplicate of [save image in gallery android](https://stackoverflow.com/questions/31217107/save-image-in-gallery-android) – kashyap Jul 14 '19 at 17:13

0 Answers0