send the real file path with edit intent

This commit is contained in:
tibbi 2017-10-27 10:07:58 +02:00
parent b98b6e6cb9
commit f0af8777a3
4 changed files with 25 additions and 23 deletions

View file

@ -7,7 +7,6 @@ import android.graphics.Point
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
@ -15,15 +14,15 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.ResizeDialog import com.simplemobiletools.gallery.dialogs.ResizeDialog
import com.simplemobiletools.gallery.dialogs.SaveAsDialog import com.simplemobiletools.gallery.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.extensions.openEditor import com.simplemobiletools.gallery.extensions.openEditor
import com.simplemobiletools.gallery.helpers.REAL_FILE_PATH
import com.theartofdev.edmodo.cropper.CropImageView import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.view_crop_image.* import kotlinx.android.synthetic.main.view_crop_image.*
import java.io.* import java.io.*
class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener { class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
val TAG = EditActivity::class.java.simpleName private val ASPECT_X = "aspectX"
val ASPECT_X = "aspectX" private val ASPECT_Y = "aspectY"
val ASPECT_Y = "aspectY" private val CROP = "crop"
val CROP = "crop"
lateinit var uri: Uri lateinit var uri: Uri
var resizeWidth = 0 var resizeWidth = 0
@ -41,18 +40,24 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
return return
} }
val realPath = intent.extras?.get(REAL_FILE_PATH)?.toString() ?: ""
if (realPath.isNotEmpty()) {
val file = File(realPath)
uri = Uri.fromFile(file)
} else {
uri = intent.data uri = intent.data
if (uri.scheme != "file" && uri.scheme != "content") { if (uri.scheme != "file" && uri.scheme != "content") {
toast(R.string.unknown_file_location) toast(R.string.unknown_file_location)
finish() finish()
return return
} }
}
isCropIntent = intent.extras?.get(CROP) == "true" isCropIntent = intent.extras?.get(CROP) == "true"
crop_image_view.apply { crop_image_view.apply {
setOnCropImageCompleteListener(this@EditActivity) setOnCropImageCompleteListener(this@EditActivity)
setImageUriAsync(intent.data) setImageUriAsync(uri)
if (isCropIntent && shouldCropSquare()) if (isCropIntent && shouldCropSquare())
setFixedAspectRatio(true) setFixedAspectRatio(true)
@ -66,9 +71,10 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
if (isEditingWithThirdParty) if (isEditingWithThirdParty) {
finish() finish()
} }
}
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_editor, menu) menuInflater.inflate(R.menu.menu_editor, menu)
@ -152,11 +158,9 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
} }
} else { } else {
toast(R.string.image_editing_failed) toast(R.string.image_editing_failed)
finish()
} }
} else { } else {
toast(R.string.unknown_file_location) toast(R.string.unknown_file_location)
finish()
} }
} else { } else {
toast("${getString(R.string.image_editing_failed)}: ${result.error.message}") toast("${getString(R.string.image_editing_failed)}: ${result.error.message}")
@ -175,9 +179,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Crop compressing failed $path $e") showErrorToast(e)
toast(R.string.image_editing_failed)
finish()
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error) toast(R.string.out_of_memory_error)
} }

View file

@ -18,10 +18,7 @@ import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.BuildConfig
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.helpers.IS_FROM_GALLERY import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.helpers.REQUEST_EDIT_IMAGE
import com.simplemobiletools.gallery.helpers.REQUEST_SET_AS
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.views.MySquareImageView import com.simplemobiletools.gallery.views.MySquareImageView
@ -106,6 +103,10 @@ fun Activity.openEditor(uri: Uri) {
setDataAndType(newUri, getMimeTypeFromUri(newUri)) setDataAndType(newUri, getMimeTypeFromUri(newUri))
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (uri.scheme == "file" && isNougatPlus()) {
putExtra(REAL_FILE_PATH, uri.path)
}
if (resolveActivity(packageManager) != null) { if (resolveActivity(packageManager) != null) {
val chooser = Intent.createChooser(this, getString(R.string.edit_image_with)) val chooser = Intent.createChooser(this, getString(R.string.edit_image_with))
startActivityForResult(chooser, REQUEST_EDIT_IMAGE) startActivityForResult(chooser, REQUEST_EDIT_IMAGE)

View file

@ -10,7 +10,6 @@ import android.os.Build
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.commons.extensions.getFilePublicUri import com.simplemobiletools.commons.extensions.getFilePublicUri
import com.simplemobiletools.commons.extensions.getMimeTypeFromPath import com.simplemobiletools.commons.extensions.getMimeTypeFromPath
import com.simplemobiletools.commons.extensions.getRealPathFromURI
import com.simplemobiletools.commons.extensions.humanizePath import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.BuildConfig
import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.activities.SettingsActivity
@ -81,7 +80,6 @@ fun Context.getSortedDirectories(source: ArrayList<Directory>): ArrayList<Direct
} }
fun Context.getMimeTypeFromUri(uri: Uri): String { fun Context.getMimeTypeFromUri(uri: Uri): String {
val path = getRealPathFromURI(uri)
var mimetype = uri.path.getMimeTypeFromPath() var mimetype = uri.path.getMimeTypeFromPath()
if (mimetype.isEmpty()) { if (mimetype.isEmpty()) {
mimetype = contentResolver.getType(uri) mimetype = contentResolver.getType(uri)

View file

@ -67,6 +67,7 @@ val DIRECTORIES = "directories2"
val IS_VIEW_INTENT = "is_view_intent" val IS_VIEW_INTENT = "is_view_intent"
val IS_FROM_GALLERY = "is_from_gallery" val IS_FROM_GALLERY = "is_from_gallery"
val PICKED_PATHS = "picked_paths" val PICKED_PATHS = "picked_paths"
val REAL_FILE_PATH = "real_file_path"
val REQUEST_EDIT_IMAGE = 1 val REQUEST_EDIT_IMAGE = 1
val REQUEST_SET_AS = 2 val REQUEST_SET_AS = 2