androidmediastore

How to access hidden media with MediaStore?


I'm developing a custom gallery app using MediaStore. I want to access all images and videos on device storage, including files which placed in hidden .folders. My problem is that I don't understand how to get hidden files via MediaStore.

I'm using the following cursor to obtain all media files:

        //define fields to interact
        val fieldExternalContentUri: Uri = MediaStore.Files.getContentUri("external")
        val fieldData = MediaStore.MediaColumns.DATA //path on disk
        val fieldMimeType = MediaStore.MediaColumns.MIME_TYPE
        val fieldTimeAdded = MediaStore.MediaColumns.DATE_ADDED
        val fieldTimeModiifed = MediaStore.MediaColumns.DATE_MODIFIED
        //set projection
        val projection = arrayOf(
            fieldData,
            fieldMimeType,
            fieldTimeAdded,
            fieldTimeModiifed
        )
        //set sort order where newest files will be first
        val sortOrder = "${MediaStore.MediaColumns.DATE_ADDED} DESC"
        //create cursor object
        val cursor: Cursor? = context.contentResolver.query(
            fieldExternalContentUri,
            projection,
            null,
            null,
            sortOrder
        )

It woks, but not returning a hidden items. What do I need to change to get all media files, including hidden ones?

UPD: hidden folder doesn't contains .nomedia file, it just starts with dot.


Solution

  • After research of mediastore database (in my case it locates at /data/data/com.android.providers.media.module/databases/external.db) I noticed that hidden files are not caching in Images and Videos tables, but only in Files table. I assumed that there it is necessary to have full access to shared storage, not just Photos and Videos, to get access inside hidden folders. I granted the MANAGE_EXTERNAL_STORAGE permission and after that it works.