diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt index d09f7c93d0..e040399c28 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt @@ -309,8 +309,8 @@ class LocalLibraryFragment : BaseFragment() { private fun showFileChooser() { val intent = Intent().apply { - action = Intent.ACTION_OPEN_DOCUMENT - type = "application/octet-stream" + action = Intent.ACTION_GET_CONTENT + type = "*/*" addCategory(Intent.CATEGORY_OPENABLE) } try { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index 4be853e7b7..691de57fae 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -48,7 +48,10 @@ import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import java.io.BufferedReader import java.io.File +import java.io.FileDescriptor +import java.io.FileInputStream import java.io.IOException +import java.nio.ByteBuffer object FileUtils { @@ -416,7 +419,21 @@ object FileUtils { uri: Uri ): AssetFileDescriptor? { return try { - context.contentResolver.openFileDescriptor(uri, "r").use { + val documentFile = DocumentFile.fromSingleUri(context, uri) + if (documentFile?.uri == null) { + return null + } + Log.e( + "PERMISSION", + "getAssetFileDescriptorFromUri: can Read file = ${documentFile.canRead()}\n" + + " can right file = ${documentFile.canWrite()}" + ) + context.contentResolver.openFileDescriptor(documentFile.uri, "r", null).use { + Log.e( + "PERMISSION", + "getAssetFileDescriptorFromUri: check file descriptor permission = " + + "${checkReadFileDescriptorPermission(it?.fileDescriptor)}" + ) AssetFileDescriptor( ParcelFileDescriptor.dup(it?.fileDescriptor), 0, AssetFileDescriptor.UNKNOWN_LENGTH @@ -430,4 +447,22 @@ object FileUtils { null } } + + private fun checkReadFileDescriptorPermission(fileDescriptor: FileDescriptor?): Boolean { + if (fileDescriptor?.valid() == false) { + // The FileDescriptor is not valid + return false + } + + return try { + val channel = FileInputStream(fileDescriptor).channel + // Try to check read access + channel.position(0) + channel.read(ByteBuffer.allocate(1)) + true + } catch (ignore: Exception) { + // An exception occurred, indicating a lack of read permission + false + } + } }