mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 10:46:53 -04:00
Merge pull request #1522 from kiwix/feature/macgills/#1520-illegal-argument-content-query
#1520 Catch IllegalArgumentException
This commit is contained in:
commit
14d7fdf111
@ -17,10 +17,12 @@
|
||||
*/
|
||||
package org.kiwix.kiwixmobile.utils.files
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Build.VERSION_CODES
|
||||
import android.os.Environment
|
||||
import android.provider.DocumentsContract
|
||||
import android.util.Log
|
||||
@ -146,12 +148,12 @@ object FileUtils {
|
||||
}
|
||||
|
||||
@JvmStatic fun getLocalFilePathByUri(
|
||||
ctx: Context,
|
||||
context: Context,
|
||||
uri: Uri
|
||||
): String? {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
|
||||
DocumentsContract.isDocumentUri(ctx, uri)
|
||||
DocumentsContract.isDocumentUri(context, uri)
|
||||
) {
|
||||
if ("com.android.externalstorage.documents" == uri.authority) {
|
||||
val documentId = DocumentsContract.getDocumentId(uri)
|
||||
@ -160,27 +162,34 @@ object FileUtils {
|
||||
if (documentId[0] == "primary") {
|
||||
return "${Environment.getExternalStorageDirectory()}/${documentId[1]}"
|
||||
}
|
||||
} else if ("com.android.providers.downloads.documents" == uri.authority
|
||||
)
|
||||
return contentQuery(
|
||||
ctx,
|
||||
ContentUris.withAppendedId(
|
||||
Uri.parse("content://downloads/public_downloads"),
|
||||
try {
|
||||
DocumentsContract.getDocumentId(uri).toLong()
|
||||
} catch (ignore: NumberFormatException) {
|
||||
0L
|
||||
}
|
||||
)
|
||||
)
|
||||
} else if ("com.android.providers.downloads.documents" == uri.authority)
|
||||
return try {
|
||||
documentProviderContentQuery(context, uri)
|
||||
} catch (ignore: IllegalArgumentException) {
|
||||
null
|
||||
}
|
||||
} else if ("content".equals(uri.scheme!!, ignoreCase = true)) {
|
||||
return contentQuery(ctx, uri)
|
||||
return contentQuery(context, uri)
|
||||
} else if ("file".equals(uri.scheme!!, ignoreCase = true)) {
|
||||
return uri.path
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@TargetApi(VERSION_CODES.KITKAT)
|
||||
private fun documentProviderContentQuery(context: Context, uri: Uri) =
|
||||
contentQuery(
|
||||
context,
|
||||
ContentUris.withAppendedId(
|
||||
Uri.parse("content://downloads/public_downloads"),
|
||||
try {
|
||||
DocumentsContract.getDocumentId(uri).toLong()
|
||||
} catch (ignore: NumberFormatException) {
|
||||
0L
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
private fun contentQuery(
|
||||
context: Context,
|
||||
uri: Uri
|
||||
|
Loading…
x
Reference in New Issue
Block a user