From 9efb03c98d63d31b78c3c87d822f3aa9846ec896 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 7 Mar 2017 20:56:53 +0100 Subject: [PATCH] add an extension function for getting file uri --- .../commons/extensions/Context-storage.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt index 17f8aae09..a5e615c4c 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt @@ -13,6 +13,7 @@ import android.os.Build import android.os.Environment import android.provider.MediaStore import android.support.v4.content.ContextCompat +import android.support.v4.content.FileProvider import android.support.v4.provider.DocumentFile import android.text.TextUtils import com.simplemobiletools.commons.R @@ -110,6 +111,8 @@ fun Context.isKitkatPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT fun Context.isLollipopPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP +fun Context.isNougatPlus() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N + @SuppressLint("NewApi") fun Context.needsStupidWritePermissions(path: String) = isPathOnSD(path) && isLollipopPlus() @@ -119,6 +122,13 @@ fun Context.isAStorageRootFolder(path: String): Boolean { return trimmed.isEmpty() || trimmed == internalStoragePath || trimmed == sdCardPath } +fun Context.getMyFileUri(file: File): Uri { + return if (isNougatPlus()) + FileProvider.getUriForFile(this, "$packageName.provider", file) + else + Uri.fromFile(file) +} + @SuppressLint("NewApi") fun Context.getFileDocument(path: String, treeUri: String): DocumentFile? { if (!isLollipopPlus())