5

My requirement is to show directory under Gallery/ Albums, creating a directory in the following way does not full fill my requirement...

 File rootPath = new File(Environment.getExternalStorageDirectory(), "directoryName"); 
 if(!rootPath.exists()) {
                rootPath.mkdirs();
            }

            final File localFile = new File(rootPath,fileName);

by using this code i can see the folder by using "file mangaer" with the path... "deviceStorage/directoryName" but the folder is not visible under Gallery or Albums

for directory creation i tried the following ways too...

 1)File directory = new File(this.getFilesDir()+File.separator+"directoryName");

 2)File directory = new File (Environment.getExternalFilesDir(null) + "/directoryName/");

3)File directory = new File(Environment.
  getExternalStoragePublicDirectory(   
 (Environment.DIRECTORY_PICTURES).toString() + "/directoryName");

but no luck, please help me friends thanks in advance.

Mobiram
  • 71
  • 3
  • this [link](https://stackoverflow.com/questions/20523658/how-to-create-application-specific-folder-in-android-gallery) might help you achieve saving images in gallery like whatsapp – Ali Momeni Oct 12 '20 at 13:16
  • by using the above link also am getting the same result, it is not showing the directory under Gallery, it simply creates a directory which can be found by using file manager, any other way please share i will try.thank you – Mobiram Oct 12 '20 at 13:32

5 Answers5

0

check this solution as well:

String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path, "/appname/media/app images/");
if (!dir.isDirectory()) {
        dir.mkdirs();
}

File file = new File(dir, filename + ".jpg");
String imagePath =  file.getAbsolutePath();
    //scan the image so show up in album
MediaScannerConnection.scanFile(this,
        new String[] { imagePath }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
        if(Config.LOG_DEBUG_ENABLED) {
            Log.d(Config.LOGTAG, "scanned : " + path);
        }
        }
});
Ali Momeni
  • 460
  • 3
  • 13
  • yes, bro i checked this one too but no luck. "Environment.getExternalStorageDirectory()" creating the directory but it's not showing under Gallery/ albums – Mobiram Oct 12 '20 at 13:53
0

Try this one.

File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "albums");
boolean rootCreated = false;
if (!root.exists())
    rootCreated = root.mkdir();
Eyosiyas
  • 1,422
  • 1
  • 9
  • 25
  • it is creating the directory in DCIM, but it is not visible in Gallery. i can see this directory in file manager that after searching by name of the directory, but i want to show my directory in gallery like skype, whatsAppImages – Mobiram Oct 16 '20 at 04:32
  • I think you have to refresh the data in order for it to show up. – Eyosiyas Oct 16 '20 at 16:44
0

You can use MediaStore API to save your media files like that:

val values = ContentValues().apply {
    put(MediaStore.Images.Media.DISPLAY_NAME, "filename")
    put(MediaStore.Images.Media.RELATIVE_PATH, "${Environment.DIRECTORY_DCIM}/$directoryName")
    put(MediaStore.Images.Media.MIME_TYPE, mimeType)
}
val collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val item = context.contentResolver.insert(collection, values) ?: throw IOException("Insert failed")
resolver.openFileDescriptor(item, "w", null)?.use {
   writeImage(it.fileDescriptor)
}

This will save the media files to the media collections and make it available to the gallery. Gallery app normally shows your directoryName as an album. Checked on Samsungs S10 and above with Android 10.

esentsov
  • 6,372
  • 21
  • 28
0

heres the java.nio library way to create directory

Java Code

File targetFile = new File(Environment.getExternalStorageDirectory(), "subDir");
Path sourceFile =  Paths.get(targetFile.getAbsolutePath());
Files.createDirectory(sourceFile);
Leroy
  • 352
  • 2
  • 11
0

some android phone create .nomedia file inside the created app folder to prevent media from being shown in gallery app so you may check if this hidden file exists and delete it if it exists. set folder and files readable. you may wait quite time before system reflect your created file in your gallery app.

        val parentFile = File(Environment.getExternalStorageDirectory(), "MyAppFolder")
        if(!parentFile.exists())
             parentFile.mkdir()
        parentFile.setReadable(true)
        // .nomedia file prevents media from being shown in gallery app
        var nomedia = File(parentFile, ".nomedia")
        if(nomedia.exists()){
            nomedia.delete()
        }
        val file = File(parentFile , "myvideo.mp4")
        file.setReadable(true)
        input.use { input ->
            var output: FileOutputStream? = null
            try {
                output = FileOutputStream(file)
                val buffer = ByteArray(4 * 1024)
                var read: Int = input.read(buffer)
                while (read != -1) {
                    output.write(buffer)
                    read = input.read(buffer)
                }
                output.flush()
                // file written to memory

            } catch (ex: Exception) {
                ex.printStackTrace()
            } finally {
                output?.close()
            }

        }
Ramy Ibrahim
  • 656
  • 4
  • 19