How to create a hidden folder i.e .MyFolder [(dot)MyFolder] in DCIM directory (Scoped Storage) Android Q

Viewed 376

I want to create a hidden folder i.e .MyFolder [(dot)MyFolder] in DCIM directory. Here is my code:-

final String relativeLocation = Environment.DIRECTORY_DCIM+File.separator+".MyFolder/images";
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME,positionOfPager+".jpeg");
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE,"image/*");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,relativeLocation);
        imageUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
        fos = (FileOutputStream) context.getContentResolver().openOutputStream(Objects.requireNonNull(imageUri));
        inImage.compress(Bitmap.CompressFormat.JPEG,100,fos);

The problem here is that a folder is create with Hyphen symbol "_.MyFolder" i.e.[(Hypen)(dot)MyFolder] due to which the folder is not hidden. My app is creating lots of images which i dont want to show up in gallery to bother the user. Please help me out Note:- I am implementing the code for scoped storage android 11

1 Answers
File file = new File( 
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), ".MyFolder/images");

if ( ! file.existst() )
    if ( !file.mkdirs() )
         return;

Both on 10 and 11 you can use this code.

You can also create your files in this folder using classic file system paths.

No need for MediaStore. Worse: As soon as you use the MediaStore the MediaStore knows about your files and hence Gallery apps that use the MediaStore to list files.

For an Android Q device add android:requestLegacyExternalStorage="true" to application tag in manifest file.

But.... this hidden folder stuff will not prevent the media scanner to scan your files after some time. Also a .nomedia file will often not do now adays.

Related