From 19c294779a15c1cf27cc9db231e2b080a5e40e60 Mon Sep 17 00:00:00 2001 From: Acclorite <140836141+Acclorite@users.noreply.github.com> Date: Sun, 16 Mar 2025 10:01:02 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Fix=20error=20when=20ge?= =?UTF-8?q?tting=20file=20from=20full=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/file/CachedFileCompat.kt | 52 +++++++++---------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/ua/acclorite/book_story/domain/file/CachedFileCompat.kt b/app/src/main/java/ua/acclorite/book_story/domain/file/CachedFileCompat.kt index 88373649..206b8cc6 100644 --- a/app/src/main/java/ua/acclorite/book_story/domain/file/CachedFileCompat.kt +++ b/app/src/main/java/ua/acclorite/book_story/domain/file/CachedFileCompat.kt @@ -10,6 +10,7 @@ import android.content.Context import android.net.Uri import android.provider.DocumentsContract import com.anggrayudi.storage.file.DocumentFileCompat +import com.anggrayudi.storage.file.getAbsolutePath object CachedFileCompat { fun fromUri(context: Context, uri: Uri, builder: CachedFileBuilder? = null): CachedFile { @@ -37,37 +38,32 @@ object CachedFileCompat { builder: CachedFileBuilder? = null ): CachedFile? { val uri = try { - val fullPathUri = DocumentFileCompat.fromFullPath(context, path)?.uri - ?: throw NullPointerException("Could not get URI from full path.") + val storageId = DocumentFileCompat.getStorageId(context, path) + if (storageId.isBlank()) throw NullPointerException("Could not get storageId.") - fullPathUri + val basePath = DocumentFileCompat.getBasePath(context, path) + if (basePath.isBlank()) throw NullPointerException("Could not get basePath.") + + val parentUri = context.contentResolver.persistedUriPermissions.find { + try { + val persistedUri = DocumentFileCompat.fromUri(context, it.uri) + val persistedUriPath = persistedUri?.getAbsolutePath(context) + + return@find !(persistedUri == null || + !persistedUri.canRead() || + persistedUriPath.isNullOrBlank() || + !path.startsWith(persistedUriPath, ignoreCase = true)) + } catch (e: Exception) { + e.printStackTrace() + return@find false + } + }?.uri + if (parentUri == null) throw NullPointerException("Could not get parentUri.") + + DocumentsContract.buildDocumentUriUsingTree(parentUri, "$storageId:$basePath") } catch (e: Exception) { e.printStackTrace() - - try { - val parentUri = DocumentFileCompat.getAccessibleRootDocumentFile( - context = context, - fullPath = path - )?.uri - ?: throw NullPointerException("Could not get parent URI.") - - val storageId = DocumentFileCompat.getStorageId(context, path) - if (storageId.isBlank()) throw NullPointerException("Could not get storageId.") - - val basePath = DocumentFileCompat.getBasePath(context, path) - if (basePath.isBlank()) throw NullPointerException("Could not get basePath.") - - val documentUri = DocumentsContract.buildDocumentUriUsingTree( - parentUri, - "$storageId:$basePath" - ) - if (documentUri == null) throw NullPointerException("Could not get document URI.") - - documentUri - } catch (e: Exception) { - e.printStackTrace() - return null - } + return null } val cachedFile = CachedFile(