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 {
val TAG: String = EditActivity::class.java.simpleName
lateinit var uri: Uri
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -29,6 +30,13 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
return
}
uri = intent.data
if (uri.scheme != "file" && uri.scheme != "content") {
toast(R.string.unknown_file_location)
finish()
return
}
crop_image_view.apply {
guidelines = CropImageView.Guidelines.OFF
setOnCropImageCompleteListener(this@EditActivity)
@ -57,27 +65,36 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) {
if (result.error == null) {
val path = intent.data.path
val file = File(path)
var out: FileOutputStream? = null
try {
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 {
if (uri.scheme == "file") {
val path = uri.path
val file = File(path)
var out: FileOutputStream? = null
try {
out?.close()
} catch (e: IOException) {
Log.e(TAG, "FileOutputStream closing failed $e")
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)
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()
})
}
} else {
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="edit_image_with">Edit image with:</string>
<string name="no_editor_found">No image editor found</string>
<string name="unknown_file_location">Unknown file location</string>
<!-- About -->
<string name="about">Über</string>

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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