take the file uri scheme into account when editing and saving files

This commit is contained in:
tibbi 2016-10-08 12:44:04 +02:00
parent b2cea47c8d
commit ca65ead9a7
8 changed files with 41 additions and 17 deletions

View file

@ -18,6 +18,7 @@ import java.io.IOException
class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener { class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
val TAG: String = EditActivity::class.java.simpleName val TAG: String = EditActivity::class.java.simpleName
lateinit var uri: Uri
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -29,6 +30,13 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
return return
} }
uri = intent.data
if (uri.scheme != "file" && uri.scheme != "content") {
toast(R.string.unknown_file_location)
finish()
return
}
crop_image_view.apply { crop_image_view.apply {
guidelines = CropImageView.Guidelines.OFF guidelines = CropImageView.Guidelines.OFF
setOnCropImageCompleteListener(this@EditActivity) setOnCropImageCompleteListener(this@EditActivity)
@ -57,27 +65,36 @@ 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) {
val path = intent.data.path if (uri.scheme == "file") {
val file = File(path) val path = uri.path
var out: FileOutputStream? = null val file = File(path)
try { var out: FileOutputStream? = null
out = FileOutputStream(file)
result.bitmap.compress(getCompressionFormat(file), 100, out)
setResult(Activity.RESULT_OK, intent)
} catch (e: Exception) {
Log.e(TAG, "Crop compressing failed $e")
toast(R.string.image_editing_failed)
} finally {
try { try {
out?.close() out = FileOutputStream(file)
} catch (e: IOException) { result.bitmap.compress(getCompressionFormat(file), 100, out)
Log.e(TAG, "FileOutputStream closing failed $e") setResult(Activity.RESULT_OK, intent)
} catch (e: Exception) {
Log.e(TAG, "Crop compressing failed $e")
toast(R.string.image_editing_failed)
finish()
} finally {
try {
out?.close()
} catch (e: IOException) {
Log.e(TAG, "FileOutputStream closing failed $e")
}
} }
}
MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { path: String, uri: Uri -> MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { path: String, uri: Uri ->
setResult(Activity.RESULT_OK, intent)
finish()
})
} else if (uri.scheme == "content") {
} else {
toast(R.string.unknown_file_location)
finish() finish()
}) }
} else { } else {
toast("${getString(R.string.image_croping_failed)} ${result.error.message}") toast("${getString(R.string.image_croping_failed)} ${result.error.message}")
} }

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">Über</string> <string name="about">Über</string>

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">Acerca de</string> <string name="about">Acerca de</string>

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">Informazioni</string> <string name="about">Informazioni</string>

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">アプリについて</string> <string name="about">アプリについて</string>

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">Sobre</string> <string name="about">Sobre</string>

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">Om</string> <string name="about">Om</string>

View file

@ -52,6 +52,7 @@
<string name="image_editing_failed">Image editing failed</string> <string name="image_editing_failed">Image editing failed</string>
<string name="edit_image_with">Edit image with:</string> <string name="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string> <string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About --> <!-- About -->
<string name="about">About</string> <string name="about">About</string>