close #44, add "Save as" to the editor

This commit is contained in:
tibbi 2016-11-06 23:52:57 +01:00
parent 90d704cff4
commit 5cb0bb904f
12 changed files with 138 additions and 15 deletions

View file

@ -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") {
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()) {
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,6 +158,11 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { path: String, uri: Uri ->
setResult(Activity.RESULT_OK, intent)
runOnUiThread {
toast(R.string.file_saved)
}
if (overrideOriginal)
finish()
})
}

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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()
}

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/save_as_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<EditText
android:id="@+id/file_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:singleLine="true"/>
</LinearLayout>

View file

@ -13,11 +13,12 @@
<string name="rename_file">Datei umbenennen</string>
<string name="rename_folder">Ordner umbenennen</string>
<string name="rename_file_error">Konnte die Datei nicht umbenennen</string>
<string name="rename_file_empty">Dateiname darf nicht leer sein</string>
<string name="rename_folder_error">Konnte den Ordner nicht umbenennen</string>
<string name="rename_folder_empty">Ordnername darf nicht leer sein</string>
<string name="rename_folder_ok">Ordner erfolgreich umbenannt</string>
<string name="renaming_folder">Benenne Ordner um</string>
<string name="filename_cannot_be_empty">Dateiname darf nicht leer sein</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">Dateiendung</string>
<string name="file_deleted">Datei gelöscht</string>
<string name="open_camera">Kamera öffnen</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 Ordner gelöscht</item>

View file

@ -13,11 +13,12 @@
<string name="rename_file">Renombrar archivo</string>
<string name="rename_folder">Renombrar carpeta</string>
<string name="rename_file_error">No se pudo renombrar el archivo</string>
<string name="rename_file_empty">El nombre de archivo no puede estar vacío</string>
<string name="rename_folder_error">No se pudo renombrar la carpeta</string>
<string name="rename_folder_empty">El nombre de carpeta no puede estar vacío</string>
<string name="rename_folder_ok">Carpeta renombrada correctamente</string>
<string name="renaming_folder">Renombrando carpeta</string>
<string name="filename_cannot_be_empty">El nombre de archivo no puede estar vacío</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">Extensión</string>
<string name="file_deleted">Archivo eliminado</string>
<string name="open_camera">Abrir cámara</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 carpeta eliminada</item>

View file

@ -13,11 +13,12 @@
<string name="rename_file">Rinomina file</string>
<string name="rename_folder">Rinomina cartella</string>
<string name="rename_file_error">Impossibile rinominare il file</string>
<string name="rename_file_empty">Il nome del file non deve essere vuoto</string>
<string name="rename_folder_error">Impossibile rinominare la cartella</string>
<string name="rename_folder_empty">Il nome della cartella non deve essere vuoto</string>
<string name="rename_folder_ok">Cartella rinominata correttamente</string>
<string name="renaming_folder">Rinominazione cartella</string>
<string name="filename_cannot_be_empty">Il nome del file non deve essere vuoto</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">Estensione</string>
<string name="file_deleted">File eliminato</string>
<string name="open_camera">Apri fotocamera</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 cartella eliminata</item>

View file

@ -13,11 +13,12 @@
<string name="rename_file">ファイルの名前を変更</string>
<string name="rename_folder">フォルダーの名前を変更</string>
<string name="rename_file_error">ファイルの名前を変更できませんでした</string>
<string name="rename_file_empty">ファイル名は空にできません</string>
<string name="rename_folder_error">フォルダーの名前を変更できませんでした</string>
<string name="rename_folder_empty">フォルダー名は空にできません</string>
<string name="rename_folder_ok">フォルダーの名前を正常に変更しました</string>
<string name="renaming_folder">フォルダーの名前を変更中</string>
<string name="filename_cannot_be_empty">ファイル名は空にできません</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">拡張</string>
<string name="file_deleted">ファイルを削除しました</string>
<string name="open_camera">カメラを開く</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 フォルダーを削除しました</item>

View file

@ -13,11 +13,12 @@
<string name="rename_file">Renomear ficheiro</string>
<string name="rename_folder">Renomear pasta</string>
<string name="rename_file_error">Não foi possível renomear o ficheiro</string>
<string name="rename_file_empty">O nome do ficheiro não pode ficar em branco</string>
<string name="rename_folder_error">Não foi possível renomear a pasta</string>
<string name="rename_folder_empty">O nome da pasta não pode ficar em branco</string>
<string name="rename_folder_ok">A pasta foi renomeada com sucesso</string>
<string name="renaming_folder">A renomear pasta</string>
<string name="filename_cannot_be_empty">O nome do ficheiro não pode ficar em branco</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">Extensão</string>
<string name="file_deleted">Ficheiro eliminado</string>
<string name="open_camera">Abrir câmara</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 pasta eliminada</item>

View file

@ -13,11 +13,12 @@
<string name="rename_file">Döp om fil</string>
<string name="rename_folder">Döp om mapp</string>
<string name="rename_file_error">Det gick inte att döpa om filen</string>
<string name="rename_file_empty">Du måste ange ett filnamn</string>
<string name="rename_folder_error">Det gick inte att döpa om mappen</string>
<string name="rename_folder_empty">Du måste ange ett mappnamn</string>
<string name="rename_folder_ok">Mappen döptes om</string>
<string name="renaming_folder">Döper om mappen</string>
<string name="filename_cannot_be_empty">Du måste ange ett filnamn</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">Filändelse</string>
<string name="file_deleted">Fil borttagen</string>
<string name="open_camera">Starta kameran</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 mapp borttagen</item>

View file

@ -13,11 +13,12 @@
<string name="rename_file">Rename file</string>
<string name="rename_folder">Rename folder</string>
<string name="rename_file_error">Could not rename the file</string>
<string name="rename_file_empty">File name must not be empty</string>
<string name="rename_folder_error">Could not rename the folder</string>
<string name="rename_folder_empty">Folder name must not be empty</string>
<string name="rename_folder_ok">Folder renamed successfully</string>
<string name="renaming_folder">Renaming folder</string>
<string name="filename_cannot_be_empty">Filename cannot be empty</string>
<string name="filename_invalid_characters">Filename contains invalid characters</string>
<string name="extension">Extension</string>
<string name="file_deleted">File deleted</string>
<string name="open_camera">Open camera</string>
@ -38,6 +39,7 @@
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<string name="save_as">Save as</string>
<string name="file_saved">File saved successfully</string>
<plurals name="folders_deleted">
<item quantity="one">1 folder deleted</item>