fix some Android 7 file uri crashes

This commit is contained in:
Tibor Kaputa 2017-03-06 11:51:13 +01:00
parent 2cadad7e32
commit 1c200cd2f5
4 changed files with 22 additions and 8 deletions

View File

@ -158,5 +158,15 @@
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>

View File

@ -5,7 +5,6 @@ import android.app.WallpaperManager
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.util.Log
@ -333,8 +332,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} else {
val file = File(path)
val isVideo = file.isVideoFast()
val isNougat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
if (isVideo && !isNougat) {
if (isVideo) {
openWith(file, false)
} else {
Intent(this, ViewPagerActivity::class.java).apply {

View File

@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import android.support.v7.app.AppCompatActivity
import android.util.DisplayMetrics
import android.view.KeyCharacterMap
@ -36,7 +37,7 @@ fun Activity.shareUri(medium: Medium, uri: Uri) {
fun Activity.shareMedium(medium: Medium) {
val shareTitle = resources.getString(R.string.share_via)
val file = File(medium.path)
val uri = Uri.fromFile(file)
val uri = FileProvider.getUriForFile(this, "$packageName.provider", file)
Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
@ -54,7 +55,7 @@ fun Activity.shareMedia(media: List<Medium>) {
type = "image/* video/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
media.map { File(it.path) }
.mapTo(uris) { Uri.fromFile(it) }
.mapTo(uris) { FileProvider.getUriForFile(this@shareMedia, "$packageName.provider", it) }
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
startActivity(Intent.createChooser(this, shareTitle))
@ -62,7 +63,7 @@ fun Activity.shareMedia(media: List<Medium>) {
}
fun Activity.setAsWallpaper(file: File) {
val uri = Uri.fromFile(file)
val uri = FileProvider.getUriForFile(this, "$packageName.provider", file)
Intent().apply {
action = Intent.ACTION_ATTACH_DATA
setDataAndType(uri, file.getMimeType("image/*"))
@ -78,7 +79,7 @@ fun Activity.setAsWallpaper(file: File) {
}
fun Activity.openWith(file: File, forceChooser: Boolean = true) {
val uri = Uri.fromFile(file)
val uri = FileProvider.getUriForFile(this, "$packageName.provider", file)
Intent().apply {
action = Intent.ACTION_VIEW
setDataAndType(uri, file.getMimeType("image/jpeg"))
@ -94,9 +95,10 @@ fun Activity.openWith(file: File, forceChooser: Boolean = true) {
}
fun Activity.openEditor(file: File) {
val uri = FileProvider.getUriForFile(this, "$packageName.provider", file)
Intent().apply {
action = Intent.ACTION_EDIT
setDataAndType(Uri.fromFile(file), "image/*")
setDataAndType(uri, "image/*")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (resolveActivity(packageManager) != null) {

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>