From 28dd99563b52bc7d17274a9b5b4de69acf20a063 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 26 Oct 2017 20:40:24 +0200 Subject: [PATCH] add a helper function for retrieving file content uri --- .../simplemobiletools/commons/extensions/Context.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt index e034a7707..4e29b34c7 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context.kt @@ -12,6 +12,7 @@ import android.os.Looper import android.provider.BaseColumns import android.provider.MediaStore import android.support.v4.content.ContextCompat +import android.support.v4.content.FileProvider import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.View @@ -24,6 +25,7 @@ import com.simplemobiletools.commons.R import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.views.* import kotlinx.android.synthetic.main.dialog_title.view.* +import java.io.File fun Context.isOnMainThread() = Looper.myLooper() == Looper.getMainLooper() fun Context.getSharedPrefs() = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) @@ -159,3 +161,11 @@ fun Context.getPermissionString(id: Int) = when (id) { PERMISSION_WRITE_CALENDAR -> Manifest.permission.WRITE_CALENDAR else -> "" } + +fun Context.getFileContentUri(file: File, applicationId: String): Uri { + return if (isNougatPlus()) { + FileProvider.getUriForFile(this, "$applicationId.provider", file) + } else { + Uri.fromFile(file) + } +}