Here is the code:

private File createImageFile() throws IOException { // Create an image file name StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = MainActivity.login + "_" + timeStamp; File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath(), "file_folder"); if (!storageDir.exists()) { storageDir.mkdir(); Log.e("SD", storageDir.exists() + ""); } File image = File.createTempFile(imageFileName, ".jpg", storageDir); return image; } 

With the help of the explorer, I found the directory created, and the file (img), but they do not appear in the gallery.
Please explain what you need to do to the catalog and photos displayed in the gallery.

  • Because the gallery still does not know that something has appeared somewhere, try to tell her: ru.stackoverflow.com/a/439433/11515 - woesss
  • @woesss, sort of did as said, but did not work (. Here is my code: MediaScannerConnection.scanFile (CreateClientActivity.this, new String [] {"/ sdcard / DCIM / file_folder /"}, null, null); - Amirhon
  • MediaScannerConnection.scanFile(CreateClientActivity.this, new String[]{ image.getAbsolutePath() }, null, null); - not a folder, but transfer the file (s) to it. - woesss
  • @woesss, thanks for the help) - Amirhon

1 answer 1

In general, I searched and found probably what is needed in this case:

 public class SingleMediaScanner implements MediaScannerConnectionClient { private MediaScannerConnection mMs; private File mFile; public SingleMediaScanner(Context context, File f) { mFile = f; mMs = new MediaScannerConnection(context, this); mMs.connect(); } @Override public void onMediaScannerConnected() { mMs.scanFile(mFile.getAbsolutePath(), null); } @Override public void onScanCompleted(String path, Uri uri) { mMs.disconnect(); } } 

Where necessary, after the appearance of new photos, call:

 new SingleMediaScanner(this, file); 

I hope someone will help.