add an extension function for getting file uri

This commit is contained in:
tibbi 2017-03-07 20:56:53 +01:00
parent 3d8bf111d4
commit 9efb03c98d

View file

@ -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())