Scooped storage API not returning proper File count from SD card folder and Private files folder on Android 11

huangapple go评论47阅读模式
英文:

Scooped storage API not returning proper File count from SD card folder and Private files folder on Android 11

问题

I have an Android 11 device. Which has SD Card. Given app has ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION.
该设备运行Android 11,拥有SD卡。给定应用程序具有ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION权限。

The app creates the folder on the SD card with NJFiles programmatically.
该应用程序以编程方式在SD卡上创建名为NJFiles的文件夹。

Then the App adds 4000 dummy files to
然后,该应用程序向NJFiles和应用程序的私有文件夹中添加了4000个虚拟文件。

Code that adds 4K files is as below
以下是添加4K文件的代码:

private fun addFiles() {
Thread {
runOnUiThread {
findViewById<MaterialButton>(R.id.addFilesToFolder).text =
"File Creation InProgress"
}
//get folder name
val privateFileFolder = getSdCardPath(this)
val sdCardRoot =
privateFileFolder?.removeSuffix(privateDataFilesFolder)

    for (i in 0 until 4000) {
        val filePrivate = File(&quot;${privateFileFolder}/file${i}.txt&quot;)
        val fileSdCard = File(&quot;${sdCardRoot}/$sdCardNjFolder/file${i}.txt&quot;)
        filePrivate.createNewFile()
        fileSdCard.createNewFile()
    }

    runOnUiThread {
        findViewById&lt;MaterialButton&gt;(R.id.addFilesToFolder).text =
            &quot;Add Dummy Files in Folders&quot;
    }
}.start()

}

Whenever I try to list the files in both folders, I am getting 0 files.
每当我尝试列出这两个文件夹中的文件时,我都得到0个文件。

I used the below code to get the files from the folder.
我使用以下代码从文件夹中获取文件:

private fun getFilesFromFolder(
context: Context,
fileNames: HashSet<String>,
privateFolder: Boolean
): HashSet<String>? {
try {
val projection = arrayOf(MediaStore.Files.FileColumns.DISPLAY_NAME)
val selection = MediaStore.MediaColumns.DATA + " LIKE ? "
val selectionArgs: Array<String> = if (privateFolder) {
arrayOf(privateDataFilesFolder)
} else {
arrayOf(sdCardNjFolder)
}
val cursor = context.contentResolver.query(
MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL),
projection,
selection,
selectionArgs,
null
)
if (cursor != null) {
while (cursor.moveToNext()) {
fileNames.add(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME)))
}
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return fileNames
}

Whenever I try to list the files in both folders, I am getting 0 files, expected count is 4K files.
每当我尝试列出这两个文件夹中的文件时,我都得到0个文件,但预期应该有4K个文件。

英文:

I have an Android 11 device. Which has SD Card. Given app has ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION.
The app creates the folder on the SD card with NJFiles programmatically.

Then the App adds 4000 dummy files to

NJFiles and App private folder

Scooped storage API not returning proper File count from SD card folder and Private files folder on Android 11

/Android/data/com.nj.scopedstorage/files

Scooped storage API not returning proper File count from SD card folder and Private files folder on Android 11

Code that add 4k file is as below

private fun addFiles() {
    Thread {
        runOnUiThread {
            findViewById&lt;MaterialButton&gt;(R.id.addFilesToFolder).text =
                &quot;File Creation InProgress&quot;
        }
        //get folder name
        val privateFileFolder = getSdCardPath(this)
        val sdCardRoot =
            privateFileFolder?.removeSuffix(privateDataFilesFolder)

        for (i in 0 until 4000) {
            val filePrivate = File(&quot;${privateFileFolder}/file${i}.txt&quot;)
            val fileSdCard = File(&quot;${sdCardRoot}/$sdCardNjFolder/file${i}.txt&quot;)
            filePrivate.createNewFile()
            fileSdCard.createNewFile()
        }

        runOnUiThread {
            findViewById&lt;MaterialButton&gt;(R.id.addFilesToFolder).text =
                &quot;Add Dummy Files in Folders&quot;
        }
    }.start()
}

Whenever I try to list the files in both folders I am getting 0 as files.

I used the below code to get the files from the folder.

private fun getFilesFromFolder(
    context: Context,
    fileNames: HashSet&lt;String&gt;,
    privateFolder: Boolean
): HashSet&lt;String&gt;? {
    try {
        val projection = arrayOf(MediaStore.Files.FileColumns.DISPLAY_NAME)
        val selection = MediaStore.MediaColumns.DATA + &quot; LIKE ? &quot;
        val selectionArgs: Array&lt;String&gt; = if (privateFolder) {
            arrayOf(privateDataFilesFolder)
        } else {
            arrayOf(sdCardNjFolder)
        }
        val cursor = context.contentResolver.query(
            MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL),
            projection,
            selection,
            selectionArgs,
            null
        )
        if (cursor != null) {
            while (cursor.moveToNext()) {
                fileNames.add(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME)))
            }
        }
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
    return fileNames
}

Whenever I tries to list the files in both folders I am getting 0 as files, expected it 4K files count.

答案1

得分: 2

MediaStore 永远不会索引位于私有 .../Android/data/<package> 文件夹中的文件。

对于 NJFiles 文件夹,您应该首先让 MediaStore 索引/扫描这些文件。

或者重新启动您的设备。

最好使用 File.listFiles() 进行列表操作。

英文:

The MediaStore will never index files in private .../Android/data/&lt;package&gt; folders.

For the NJFiles folder you should first let the MediaStore index/scan those files.

Or reboot your device.

Better do the listings with File.listFiles().

huangapple
  • 本文由 发表于 2023年6月15日 13:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定