Skip to content

Commit

Permalink
Fix file opening
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambada10 committed Oct 24, 2024
1 parent 6a7c794 commit 6e30fbb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
.kotlin
Empty file.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent>
</queries>

<application
android:enableOnBackInvokedCallback="true"
android:requestLegacyExternalStorage="true"
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/pl/lambada/songsync/util/MiscelaneousUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Build
import android.widget.Toast
import androidx.core.content.FileProvider
import java.io.File
import java.nio.file.Files

fun isLegacyFileAccessRequired(filePath: String?): Boolean {
// Before Android 11, not in internal storage
Expand All @@ -27,8 +28,16 @@ fun openFileFromPath(context: Context, filePath: String) {
Uri.fromFile(file)
}

val mime = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.probeContentType(file.toPath())
} else run {
val extension = file.extension
val mimeTypeMap = android.webkit.MimeTypeMap.getSingleton()
mimeTypeMap.getMimeTypeFromExtension(extension)
}

val intent = Intent(Intent.ACTION_VIEW)
.setDataAndType(uri, "audio/mp3")
.setDataAndType(uri, mime)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

if (intent.resolveActivity(context.packageManager) != null) {
Expand Down

0 comments on commit 6e30fbb

Please sign in to comment.