replace sending real_file_path with extra_output at the editor

This commit is contained in:
tibbi 2017-10-27 10:34:16 +02:00
parent f0af8777a3
commit 98bee4022e
2 changed files with 22 additions and 22 deletions

View File

@ -14,7 +14,6 @@ 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.*
@ -25,6 +24,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
private val CROP = "crop" private val CROP = "crop"
lateinit var uri: Uri lateinit var uri: Uri
var saveUri: Uri? = null
var resizeWidth = 0 var resizeWidth = 0
var resizeHeight = 0 var resizeHeight = 0
var isCropIntent = false var isCropIntent = false
@ -40,17 +40,15 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
return return
} }
val realPath = intent.extras?.get(REAL_FILE_PATH)?.toString() ?: "" uri = intent.data
if (realPath.isNotEmpty()) { if (uri.scheme != "file" && uri.scheme != "content") {
val file = File(realPath) toast(R.string.unknown_file_location)
uri = Uri.fromFile(file) finish()
} else { return
uri = intent.data }
if (uri.scheme != "file" && uri.scheme != "content") {
toast(R.string.unknown_file_location) if (intent.extras?.containsKey(MediaStore.EXTRA_OUTPUT) == true) {
finish() saveUri = intent.extras!!.get(MediaStore.EXTRA_OUTPUT) as Uri
return
}
} }
isCropIntent = intent.extras?.get(CROP) == "true" isCropIntent = intent.extras?.get(CROP) == "true"
@ -130,15 +128,14 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) { override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) {
if (result.error == null) { if (result.error == null) {
if (isCropIntent && intent.extras?.containsKey(MediaStore.EXTRA_OUTPUT) == true) { if (isCropIntent && saveUri != null) {
val targetUri = intent.extras!!.get(MediaStore.EXTRA_OUTPUT) as Uri
var inputStream: InputStream? = null var inputStream: InputStream? = null
var outputStream: OutputStream? = null var outputStream: OutputStream? = null
try { try {
val stream = ByteArrayOutputStream() val stream = ByteArrayOutputStream()
result.bitmap.compress(CompressFormat.JPEG, 100, stream) result.bitmap.compress(CompressFormat.JPEG, 100, stream)
inputStream = ByteArrayInputStream(stream.toByteArray()) inputStream = ByteArrayInputStream(stream.toByteArray())
outputStream = contentResolver.openOutputStream(targetUri) outputStream = contentResolver.openOutputStream(saveUri)
inputStream.copyTo(outputStream) inputStream.copyTo(outputStream)
} finally { } finally {
inputStream?.close() inputStream?.close()
@ -146,12 +143,12 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
} }
setResult(RESULT_OK) setResult(RESULT_OK)
finish() finish()
} else if (uri.scheme == "file") { } else if (saveUri?.scheme == "file") {
SaveAsDialog(this, uri.path, true) { SaveAsDialog(this, saveUri!!.path, true) {
saveBitmapToFile(result.bitmap, it) saveBitmapToFile(result.bitmap, it)
} }
} else if (uri.scheme == "content") { } else if (saveUri?.scheme == "content") {
val newPath = applicationContext.getRealPathFromURI(uri) ?: "" val newPath = applicationContext.getRealPathFromURI(saveUri!!) ?: ""
if (!newPath.isEmpty()) { if (!newPath.isEmpty()) {
SaveAsDialog(this, newPath, true) { SaveAsDialog(this, newPath, true) {
saveBitmapToFile(result.bitmap, it) saveBitmapToFile(result.bitmap, it)

View File

@ -18,7 +18,10 @@ 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.* import com.simplemobiletools.gallery.helpers.IS_FROM_GALLERY
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
@ -103,8 +106,8 @@ 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()) { if (isNougatPlus()) {
putExtra(REAL_FILE_PATH, uri.path) putExtra(MediaStore.EXTRA_OUTPUT, uri)
} }
if (resolveActivity(packageManager) != null) { if (resolveActivity(packageManager) != null) {