mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 06:42:21 -04:00
Checking permission of fileDescriptor is it has the read access or not.
This commit is contained in:
parent
7c59193bd8
commit
21b34e2b44
@ -309,8 +309,8 @@ class LocalLibraryFragment : BaseFragment() {
|
|||||||
|
|
||||||
private fun showFileChooser() {
|
private fun showFileChooser() {
|
||||||
val intent = Intent().apply {
|
val intent = Intent().apply {
|
||||||
action = Intent.ACTION_OPEN_DOCUMENT
|
action = Intent.ACTION_GET_CONTENT
|
||||||
type = "application/octet-stream"
|
type = "*/*"
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -48,7 +48,10 @@ import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
|||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileDescriptor
|
||||||
|
import java.io.FileInputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
|
||||||
object FileUtils {
|
object FileUtils {
|
||||||
|
|
||||||
@ -416,7 +419,21 @@ object FileUtils {
|
|||||||
uri: Uri
|
uri: Uri
|
||||||
): AssetFileDescriptor? {
|
): AssetFileDescriptor? {
|
||||||
return try {
|
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(
|
AssetFileDescriptor(
|
||||||
ParcelFileDescriptor.dup(it?.fileDescriptor),
|
ParcelFileDescriptor.dup(it?.fileDescriptor),
|
||||||
0, AssetFileDescriptor.UNKNOWN_LENGTH
|
0, AssetFileDescriptor.UNKNOWN_LENGTH
|
||||||
@ -430,4 +447,22 @@ object FileUtils {
|
|||||||
null
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user