Skip to content

Commit

Permalink
Merge pull request #293 from chenxiaolong/mimetype
Browse files Browse the repository at this point in the history
DocumentFileExtensions.kt: Use simple string operations to preserve extension when renaming
  • Loading branch information
chenxiaolong authored Apr 14, 2023
2 parents 6af5407 + 67fc1c1 commit 63bcaf2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/com/chiller3/bcr/DocumentFileExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.net.Uri
import android.provider.DocumentsContract
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.documentfile.provider.DocumentFile

private const val TAG = "DocumentFileExtensions"
Expand Down Expand Up @@ -112,8 +111,12 @@ fun DocumentFile.renameToPreserveExt(displayName: String): Boolean {
buildString {
append(displayName)

val ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(type)
if (ext != null) {
// This intentionally just does simple string operations because MimeTypeMap's
// getExtensionFromMimeType() and getMimeTypeFromExtension() are not consistent with
// each other. Eg. audio/mp4 -> m4a -> audio/mpeg -> mp3.

val ext = name!!.substringAfterLast('.', "")
if (ext.isNotEmpty()) {
append('.')
append(ext)
}
Expand Down

0 comments on commit 63bcaf2

Please sign in to comment.