magnusja / libaums

Open source library to access USB Mass Storage devices on Android without rooting your device
Apache License 2.0
1.26k stars 270 forks source link

为什么读取NTFS、exFAT文件系统的SD卡数据很慢?读取FAT32就很快? #418

Open Kotlin2022 opened 4 months ago

Kotlin2022 commented 4 months ago

我用了不同文件系统的SD卡测试读取SD卡上的照片,发现NTFS、exFAT文件系统的SD卡数据很慢,读取FAT32就很快, 代码如下: ` private fun setupDevice() { try { massStorageDevices[currentDevice].init()

        // we always use the first partition of the device
        currentFs = massStorageDevices[currentDevice].partitions[0].fileSystem.also {
            Log.d(TAG, "Capacity: " + it.capacity)
            Log.d(TAG, "Occupied Space: " + it.occupiedSpace)
            Log.d(TAG, "Free Space: " + it.freeSpace)
            Log.d(TAG, "Chunk size: " + it.chunkSize)
        }

        val root = currentFs.rootDirectory
        val images =   searchImages(root.listFiles())
        images?.apply {
            Log.d(TAG,"images.size = $size") 
        }
    } catch (e: IOException) {
        Log.e(TAG, "error setting up device", e)
    }
}

@Throws(IOException::class) fun searchImages(usbFiles: Array): ArrayList? { val files = ArrayList() for (usbFile in usbFiles) { if (usbFile.isDirectory && usbFile.name == "DCIM") { //找到相册 val photosFolder = usbFile.listFiles() Log.d(TAG, photosFolder.contentToString()) if (photosFolder.isNotEmpty()) { //相册下的文件夹大于0 for (file in photosFolder) { if (file.isDirectory) { val images = file.listFiles() Log.d(TAG, images.contentToString()) for (image in images) { if (image.name.endsWith("JPEG") || image.name.endsWith("JPG")) { files.add(image) } } } } } } } return files }` 同样1500张图片,NTFS、exFAT文件系统的SD卡需要1分多钟才能读完,FAT32文件系统的SD卡只需要几秒钟,这时为什么?

Kotlin2022 commented 4 months ago

我发现实际上是usbFile.listFiles() 这个方法很慢,不清楚为什么?

Kotlin2022 commented 4 months ago

` @Throws(IOException::class) fun refresh() { dialog.setMessage("loading...") dialog.show() CoroutineScope(Dispatchers.IO).launch { Log.d(MainActivity.TAG,"开是检索文件") val usbFiles = currentDir.listFiles() files = listOf(*usbFiles) Log.d(MainActivity.TAG,"文件检索完成 files.size = ${files.size} ${usbFiles.contentToString()}") Collections.sort(files, comparator) withContext(Dispatchers.Main) { notifyDataSetChanged() dialog.dismiss() } }

}`

15:56:05.770 25808-27815 USB_lib com...usbfileman D 开是检索文件 15:56:44.140 25808-27815 USB_lib com...usbfileman D 文件检索完成 files.size = 1 [100MSDCF]

Kotlin2022 commented 4 months ago

39秒才执行完 currentDir.listFiles() 是不是太慢了?

zhishenglin commented 3 months ago

请问下你是怎么读取ntfs文件系统的,我可以读取fat32的u盘。但是当我读取ntfs的移动硬盘时,在执行到init()初始化设备方法时就抛出了不支持的文件系统异常