Content resolver returns wrong size

拈花ヽ惹草 提交于 2019-12-10 13:53:52

问题


I am picking an image from the gallery and querying its size via ContentResolver API, it returns 29kb.

However when I check the file via adb using ls -al it is 44kb

here is how I query the size of the image:

    private fun getFileInfo(contentResolver: ContentResolver, fileUri: Uri): Pair<String, Long>? {
    val projection = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE)
    val metaCursor = contentResolver.query(fileUri, projection, null, null, null)
        metaCursor?.use {
            if (it.moveToFirst()) {
                val displayNameIndex = it.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                val sizeIndex = it.getColumnIndex(MediaStore.MediaColumns.SIZE)
                return Pair(it.getString(displayNameIndex), it.getLong(sizeIndex))
            }
    }
    return null
}

I am using an Oreo Emulator. I have also checked via emulator's tools, file browser shows as 29kb on the other hand file details shows 45kb. What is going on?

Here are the images from file browser:

Another side note, above situation can be reproducible every time when using camera app on emulator with Android 26-Oreo emulator, however it is fine with emulator Android 25-Nougat.

I have checked, new Document API also returns 29kb


回答1:


Android 8.0 (Oreo) introduced the 'SDCardFS' filesystem, which replaced 'FUSE' on the /sdcard partition.

it is possible that this new filesystem has a large cluster size and is therefore not able to allocate exactly 29kb, but instead the smallest block larger than 29kb, which in your case is 44.*kb.

The difference between ls -al and file details is probably caused by different rounding.



来源:https://stackoverflow.com/questions/48302972/content-resolver-returns-wrong-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!