Skip to content

Commit 5ec2563

Browse files
committed
fix: use openFileDescriptor instead of openInputStream for SAF file access
openInputStream fails for SAF tree child URIs on some devices because the content provider requires openFileDescriptor. Using FileDescriptor + FileInputStream as a more compatible alternative.
1 parent 6d3afad commit 5ec2563

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

app/src/main/java/com/ljyh/mei/utils/LocalMusicScanner.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,17 @@ class LocalMusicScanner(
259259
private fun copyToTemp(uri: android.net.Uri): File? {
260260
val tempFile = File(context.cacheDir, "scan_${System.nanoTime()}")
261261
return try {
262-
context.contentResolver.openInputStream(uri)?.use { input ->
263-
tempFile.outputStream().use { output -> input.copyTo(output) }
262+
val fd = context.contentResolver.openFileDescriptor(uri, "r")
263+
if (fd != null) {
264+
fd.use { pfd ->
265+
pfd.fileDescriptor.let { fileDesc ->
266+
java.io.FileInputStream(fileDesc).use { input ->
267+
tempFile.outputStream().use { output ->
268+
input.copyTo(output)
269+
}
270+
}
271+
}
272+
}
264273
}
265274
if (tempFile.exists() && tempFile.length() > 0) tempFile else null
266275
} catch (e: Exception) {

0 commit comments

Comments
 (0)