mirror of
https://github.com/SimpleMobileTools/Simple-Gallery.git
synced 2025-06-05 21:59:19 +02:00
close #44, add "Save as" to the editor
This commit is contained in:
@ -10,6 +10,7 @@ import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import com.simplemobiletools.gallery.R
|
||||
import com.simplemobiletools.gallery.Utils
|
||||
import com.simplemobiletools.gallery.dialogs.SaveAsDialog
|
||||
import com.simplemobiletools.gallery.extensions.toast
|
||||
import com.theartofdev.edmodo.cropper.CropImageView
|
||||
import kotlinx.android.synthetic.main.activity_edit.*
|
||||
@ -20,6 +21,8 @@ import java.io.OutputStream
|
||||
|
||||
class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
|
||||
val TAG: String = EditActivity::class.java.simpleName
|
||||
|
||||
var overrideOriginal = false
|
||||
lateinit var uri: Uri
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -54,6 +57,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
R.id.save -> {
|
||||
overrideOriginal = true
|
||||
crop_image_view.getCroppedImageAsync()
|
||||
true
|
||||
}
|
||||
@ -70,17 +74,38 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
|
||||
}
|
||||
|
||||
private fun saveAs() {
|
||||
|
||||
overrideOriginal = false
|
||||
crop_image_view.getCroppedImageAsync()
|
||||
}
|
||||
|
||||
override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) {
|
||||
if (result.error == null) {
|
||||
if (uri.scheme == "file") {
|
||||
saveBitmapToFile(result.bitmap, uri.path)
|
||||
if (overrideOriginal)
|
||||
saveBitmapToFile(result.bitmap, uri.path)
|
||||
else {
|
||||
SaveAsDialog(this, uri.path, object : SaveAsDialog.OnSaveAsListener {
|
||||
override fun onSaveAsSuccess(filename: String) {
|
||||
val parent = File(uri.path).parent
|
||||
val path = File(parent, filename).absolutePath
|
||||
saveBitmapToFile(result.bitmap, path)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if (uri.scheme == "content") {
|
||||
val newPath = Utils.getRealPathFromURI(applicationContext, uri) ?: ""
|
||||
if (!newPath.isEmpty()) {
|
||||
saveBitmapToFile(result.bitmap, newPath)
|
||||
if (overrideOriginal) {
|
||||
saveBitmapToFile(result.bitmap, newPath)
|
||||
} else {
|
||||
SaveAsDialog(this, newPath, object : SaveAsDialog.OnSaveAsListener {
|
||||
override fun onSaveAsSuccess(filename: String) {
|
||||
val parent = File(uri.path).parent
|
||||
val path = File(parent, filename).absolutePath
|
||||
saveBitmapToFile(result.bitmap, path)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
toast(R.string.image_editing_failed)
|
||||
finish()
|
||||
@ -96,7 +121,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
|
||||
|
||||
private fun saveBitmapToFile(bitmap: Bitmap, path: String) {
|
||||
val file = File(path)
|
||||
if (!file.exists()) {
|
||||
if (overrideOriginal && !file.exists()) {
|
||||
toast(R.string.error_saving_file)
|
||||
finish()
|
||||
return
|
||||
@ -108,7 +133,10 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
|
||||
if (Utils.isShowingWritePermissions(this, file))
|
||||
return
|
||||
|
||||
val document = Utils.getFileDocument(this, path)
|
||||
var document = Utils.getFileDocument(this, path)
|
||||
if (!file.exists()) {
|
||||
document = document.createFile("", file.name)
|
||||
}
|
||||
out = contentResolver.openOutputStream(document.uri)
|
||||
} else {
|
||||
out = FileOutputStream(file)
|
||||
@ -117,7 +145,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
|
||||
bitmap.compress(getCompressionFormat(file), 90, out)
|
||||
setResult(Activity.RESULT_OK, intent)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Crop compressing failed $e")
|
||||
Log.e(TAG, "Crop compressing failed $path $e")
|
||||
toast(R.string.image_editing_failed)
|
||||
finish()
|
||||
} finally {
|
||||
@ -130,7 +158,12 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
|
||||
|
||||
MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { path: String, uri: Uri ->
|
||||
setResult(Activity.RESULT_OK, intent)
|
||||
finish()
|
||||
runOnUiThread {
|
||||
toast(R.string.file_saved)
|
||||
}
|
||||
|
||||
if (overrideOriginal)
|
||||
finish()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR
|
||||
val extension = view.file_extension.value
|
||||
|
||||
if (fileName.isEmpty() || extension.isEmpty()) {
|
||||
context.toast(R.string.rename_file_empty)
|
||||
context.toast(R.string.filename_cannot_be_empty)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,51 @@
|
||||
package com.simplemobiletools.gallery.dialogs
|
||||
|
||||
import android.app.Activity
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.LayoutInflater
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.filepicker.extensions.getFilenameFromPath
|
||||
import com.simplemobiletools.gallery.R
|
||||
import com.simplemobiletools.gallery.extensions.isNameValid
|
||||
import com.simplemobiletools.gallery.extensions.toast
|
||||
import com.simplemobiletools.gallery.extensions.value
|
||||
import kotlinx.android.synthetic.main.rename_file.view.*
|
||||
|
||||
class SaveAsDialog(val activity: Activity, val path: String, val listener: OnSaveAsListener) {
|
||||
|
||||
init {
|
||||
val context = activity
|
||||
val view = LayoutInflater.from(context).inflate(R.layout.dialog_save_as, null)
|
||||
view.file_name.setText(path.getFilenameFromPath())
|
||||
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(context.resources.getString(R.string.save_as))
|
||||
.setView(view)
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create().apply {
|
||||
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||
show()
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||
val filename = view.file_name.value
|
||||
|
||||
if (filename.isEmpty()) {
|
||||
context.toast(R.string.filename_cannot_be_empty)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
if (!filename.isNameValid()) {
|
||||
context.toast(R.string.filename_invalid_characters)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
listener.onSaveAsSuccess(filename)
|
||||
dismiss()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
interface OnSaveAsListener {
|
||||
fun onSaveAsSuccess(filename: String)
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.simplemobiletools.gallery.extensions
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
fun String.isNameValid(): Boolean {
|
||||
val pattern = Pattern.compile("^[-_.A-Za-z0-9()#& ]+$")
|
||||
val matcher = pattern.matcher(this)
|
||||
return matcher.matches()
|
||||
}
|
Reference in New Issue
Block a user