fixed opening file media URI with no mime type in Media Store

This commit is contained in:
Thibault Deckers 2020-11-16 12:30:48 +09:00
parent edb8796ba2
commit 93e385d7c3
2 changed files with 42 additions and 35 deletions

View file

@ -47,10 +47,10 @@ class MediaStoreImageProvider : ImageProvider() {
val contentUri = ContentUris.withAppendedId(VIDEO_CONTENT_URI, id)
if (fetchFrom(context, alwaysValid, onSuccess, contentUri, VIDEO_PROJECTION) > 0) return
}
// the uri can be a file media uri (e.g. "content://0@media/external/file/30050")
// the uri can be a file media URI (e.g. "content://0@media/external/file/30050")
// without an equivalent image/video if it is shared from a file browser
// but the file is not publicly visible
if (fetchFrom(context, alwaysValid, onSuccess, uri, BASE_PROJECTION) > 0) return
if (fetchFrom(context, alwaysValid, onSuccess, uri, BASE_PROJECTION, fileMimeType = mimeType) > 0) return
callback.onFailure(Exception("failed to fetch entry at uri=$uri"))
}
@ -87,6 +87,7 @@ class MediaStoreImageProvider : ImageProvider() {
handleNewEntry: NewEntryHandler,
contentUri: Uri,
projection: Array<String>,
fileMimeType: String? = null,
): Int {
var newEntryCount = 0
val orderBy = "${MediaStore.MediaColumns.DATE_MODIFIED} DESC"
@ -123,11 +124,16 @@ class MediaStoreImageProvider : ImageProvider() {
// for multiple items, `contentUri` is the root without ID,
// but for single items, `contentUri` already contains the ID
val itemUri = if (contentUriContainsId) contentUri else ContentUris.withAppendedId(contentUri, contentId.toLong())
val mimeType = cursor.getString(mimeTypeColumn)
// `mimeType` can be registered as null for file media URIs with unsupported media types (e.g. TIFF on old devices)
// in that case we try to use the mime type provided along the URI
val mimeType: String? = cursor.getString(mimeTypeColumn) ?: fileMimeType
val width = cursor.getInt(widthColumn)
val height = cursor.getInt(heightColumn)
val durationMillis = if (durationColumn != -1) cursor.getLong(durationColumn) else 0L
if (mimeType == null) {
Log.w(LOG_TAG, "failed to make entry from uri=$itemUri because of null MIME type")
} else {
var entryMap: FieldMap = hashMapOf(
"uri" to itemUri.toString(),
"path" to cursor.getString(pathColumn),
@ -164,6 +170,7 @@ class MediaStoreImageProvider : ImageProvider() {
newEntryCount++
}
}
}
cursor.close()
}
} catch (e: Exception) {

View file

@ -44,7 +44,7 @@ object MimeTypes {
else -> isVideo(mimeType)
}
fun isRaw(mimeType: String?): Boolean {
fun isRaw(mimeType: String): Boolean {
return when (mimeType) {
ARW, CR2, DNG, NEF, NRW, ORF, PEF, RAF, RW2, SRW -> true
else -> false