From f765bf7dc9c02de7311733019ea3577bbbb371e6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 8 Oct 2016 16:08:10 +0200 Subject: [PATCH] handle content:// uri scheme properly too, close #28 --- .../gallery/MyViewPager.java | 1 - .../gallery/activities/EditActivity.kt | 68 ++++++++++++------- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values-it/strings.xml | 1 + app/src/main/res/values-ja/strings.xml | 1 + app/src/main/res/values-pt-rPT/strings.xml | 1 + app/src/main/res/values-sv/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 9 files changed, 51 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/MyViewPager.java b/app/src/main/java/com/simplemobiletools/gallery/MyViewPager.java index eca6b83cb..734f232ba 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/MyViewPager.java +++ b/app/src/main/java/com/simplemobiletools/gallery/MyViewPager.java @@ -1,7 +1,6 @@ package com.simplemobiletools.gallery; import android.content.Context; -import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index d5b2de823..e636f8263 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -5,6 +5,7 @@ import android.graphics.Bitmap import android.media.MediaScannerConnection import android.net.Uri import android.os.Bundle +import android.provider.MediaStore import android.util.Log import android.view.Menu import android.view.MenuItem @@ -66,31 +67,9 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) { if (result.error == null) { if (uri.scheme == "file") { - val path = uri.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) - 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 -> - setResult(Activity.RESULT_OK, intent) - finish() - }) + saveBitmapToFile(result.bitmap, uri.path) } else if (uri.scheme == "content") { - + saveBitmapToFile(result.bitmap, convertMediaUriToPath(uri)) } else { toast(R.string.unknown_file_location) finish() @@ -100,6 +79,37 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } } + private fun saveBitmapToFile(bitmap: Bitmap, path: String) { + val file = File(path) + if (!file.exists()) { + toast(R.string.error_saving_file) + finish() + return + } + + var out: FileOutputStream? = null + try { + out = FileOutputStream(file) + 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 -> + setResult(Activity.RESULT_OK, intent) + finish() + }) + } + private fun getCompressionFormat(file: File): Bitmap.CompressFormat { return when (file.extension.toLowerCase()) { "png" -> Bitmap.CompressFormat.PNG @@ -107,4 +117,14 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener else -> Bitmap.CompressFormat.JPEG } } + + private fun convertMediaUriToPath(uri: Uri): String { + val proj = arrayOf(MediaStore.Images.Media.DATA) + val cursor = contentResolver.query(uri, proj, null, null, null) + val index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) + cursor.moveToFirst() + val path = cursor.getString(index) + cursor.close() + return path + } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 5c84e553e..6a72f846b 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file Über diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index e8ae1c246..243c789b9 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file Acerca de diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index b8d900149..82b2907f4 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file Informazioni diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 1531c5dc7..6b2ec1827 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file アプリについて diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index a98944baa..2129edbdc 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file Sobre diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index dd7dee951..dd6b87105 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file Om diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f378b8529..88c3fe4b7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,6 +53,7 @@ Edit image with: No image editor found Unknown file location + Could not overwrite the source file About