问题
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