Make DocumentProvider Support Thumbnail

This  will make DocumentUI or the built-in Android System Internal Files Manager app showing Thumbnail of Image file instead of image type icon.
This commit is contained in:
BlakDulz 2023-12-24 15:34:10 +07:00
parent d667fca8d3
commit 45d3c41d68
1 changed files with 17 additions and 2 deletions

View File

@ -9,9 +9,10 @@
package org.dolphinemu.dolphinemu.features package org.dolphinemu.dolphinemu.features
import android.annotation.TargetApi import android.annotation.TargetApi
import android.content.Context import android.content.res.AssetFileDescriptor
import android.database.Cursor import android.database.Cursor
import android.database.MatrixCursor import android.database.MatrixCursor
import android.graphics.Point
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.CancellationSignal import android.os.CancellationSignal
@ -29,7 +30,7 @@ class DocumentProvider : DocumentsProvider() {
private var rootDirectory: File? = null private var rootDirectory: File? = null
companion object { companion object {
public const val ROOT_ID = "root" const val ROOT_ID = "root"
private val DEFAULT_ROOT_PROJECTION = arrayOf( private val DEFAULT_ROOT_PROJECTION = arrayOf(
DocumentsContract.Root.COLUMN_ROOT_ID, DocumentsContract.Root.COLUMN_ROOT_ID,
@ -111,6 +112,16 @@ class DocumentProvider : DocumentsProvider() {
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode)) return ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode))
} }
override fun openDocumentThumbnail(
documentId: String,
sizeHint: Point,
signal: CancellationSignal
): AssetFileDescriptor {
val file = documentIdToPath(documentId)
val pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
return AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH)
}
override fun createDocument( override fun createDocument(
parentDocumentId: String, parentDocumentId: String,
mimeType: String, mimeType: String,
@ -178,6 +189,10 @@ class DocumentProvider : DocumentsProvider() {
} else { } else {
file.name file.name
} }
val mimeType = getTypeForFile(file)
if (file.exists() && mimeType.startsWith("image/")) {
flags = flags or DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL
}
cursor.newRow().apply { cursor.newRow().apply {
add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, pathToDocumentId(file)) add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, pathToDocumentId(file))
add(DocumentsContract.Document.COLUMN_MIME_TYPE, getTypeForFile(file)) add(DocumentsContract.Document.COLUMN_MIME_TYPE, getTypeForFile(file))