5

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

Parmu
  • 51
  • 2
  • 2
    "My app is creating lots of images which i dont want to show up in gallery to bother the user" -- then put them in `getFilesDir()` or `getExternalFilesDir()` (methods on `Context`). – CommonsWare Mar 25 '21 at 13:33
  • `My app is creating lots of images which i dont want to show up in gallery` Well then dont use the MediaStore to begin with. But even where you can create such a '.hidden' folder there with mkdir() and place your files in it in the old way the MediaStore will soon scan those files in them. – blackapps Mar 25 '21 at 14:12
  • thanks. Now I am storing the images in app specific folder :) – Parmu Mar 25 '21 at 16:55
  • i am facing the same problem. whenever i create, it create with hyphen symbol "_.test". – NarenderNishad Nov 11 '21 at 11:43
  • Can you put a debug point on the `relativeLocation` line, and check what the relative location value is being set after execution of that line? – bradley101 Nov 13 '21 at 09:20
  • `(Scoped Storage) Android Q` Q==10. AND `am implementing the code for scoped storage android 11` do not match. – blackapps Nov 13 '21 at 12:07
  • Hmmm.. with 21 points i'm amazed you can offer a 50 points bounty.. – blackapps Nov 13 '21 at 12:15

1 Answers1

1
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.

blackapps
  • 8,011
  • 2
  • 11
  • 25