From 6810b3f68b1cc8bcb219311c45e31f570b251133 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 24 Apr 2017 20:27:47 +0200 Subject: [PATCH] fix #235, generate a different file uri for setting wallpaper if one fails --- .../gallery/activities/PhotoVideoActivity.kt | 2 +- .../gallery/activities/ViewPagerActivity.kt | 2 +- .../gallery/extensions/activity.kt | 41 +++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt index d222e148e..098d958cf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -129,7 +129,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.menu_set_as_wallpaper -> setAsWallpaper(File(mMedium!!.path)) + R.id.menu_set_as_wallpaper -> trySetAsWallpaper(File(mMedium!!.path)) R.id.menu_open_with -> openWith(File(mMedium!!.path)) R.id.menu_share -> shareUri(mMedium!!, mUri) R.id.menu_edit -> openEditor(File(mMedium!!.path)) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 308303df2..fc5565de9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -181,7 +181,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View return true when (item.itemId) { - R.id.menu_set_as_wallpaper -> setAsWallpaper(getCurrentFile()) + R.id.menu_set_as_wallpaper -> trySetAsWallpaper(getCurrentFile()) R.id.menu_copy_to -> copyTo() R.id.menu_move_to -> moveTo() R.id.menu_open_with -> openWith(getCurrentFile()) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index 62bdf7921..12dab6bbb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -2,8 +2,10 @@ package com.simplemobiletools.gallery.extensions import android.app.Activity import android.content.Intent +import android.database.Cursor import android.net.Uri import android.os.Build +import android.provider.MediaStore import android.support.v7.app.AppCompatActivity import android.util.DisplayMetrics import android.view.KeyCharacterMap @@ -69,8 +71,16 @@ fun Activity.shareMedia(media: List) { } } -fun Activity.setAsWallpaper(file: File) { - val uri = Uri.fromFile(file) +fun Activity.trySetAsWallpaper(file: File) { + var uri = Uri.fromFile(file) + if (!setAsWallpaper(uri, file)) { + uri = getFileContentUri(file) + setAsWallpaper(uri, file, false) + } +} + +fun Activity.setAsWallpaper(uri: Uri, file: File, showToast: Boolean = true): Boolean { + var success = false Intent().apply { action = Intent.ACTION_ATTACH_DATA setDataAndType(uri, file.getMimeType("image/*")) @@ -79,10 +89,35 @@ fun Activity.setAsWallpaper(file: File) { if (resolveActivity(packageManager) != null) { startActivityForResult(chooser, REQUEST_SET_WALLPAPER) + success = true } else { - toast(R.string.no_wallpaper_setter_found) + if (showToast) { + toast(R.string.no_wallpaper_setter_found) + } + success = false } } + + return success +} + +fun Activity.getFileContentUri(file: File): Uri? { + val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + val projection = arrayOf(MediaStore.Images.Media._ID) + val selection = "${MediaStore.Images.Media.DATA} = ?" + val selectionArgs = arrayOf(file.absolutePath) + + var cursor: Cursor? = null + try { + cursor = contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor?.moveToFirst() == true) { + val id = cursor.getIntValue(MediaStore.Images.Media._ID) + return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "$id") + } + } finally { + cursor?.close() + } + return null } fun Activity.openWith(file: File, forceChooser: Boolean = true) {