From a5def53373c5ae32b8564156c2b1c1773113c1ab Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 6 Oct 2016 19:54:09 +0200 Subject: [PATCH] save the edited bitmap when done --- .../gallery/activities/ViewPagerActivity.java | 12 ++++++ .../gallery/activities/EditActivity.kt | 40 ++++++++++++++++++- 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, 58 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index b224a306e..f7ffc5077 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -183,6 +183,18 @@ public class ViewPagerActivity extends SimpleActivity startActivityForResult(intent, EDIT_IMAGE); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == EDIT_IMAGE) { + if (resultCode == RESULT_OK && data != null) { + + } else { + Utils.showToast(getApplicationContext(), R.string.image_editing_failed); + } + } + super.onActivityResult(requestCode, resultCode, data); + } + private void setAsWallpaper() { final Bitmap bitmap = BitmapFactory.decodeFile(getCurrentFile().getAbsolutePath()); final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 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 4269939bc..a5d298cbd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -1,14 +1,24 @@ package com.simplemobiletools.gallery.activities +import android.app.Activity +import android.graphics.Bitmap +import android.media.MediaScannerConnection +import android.net.Uri import android.os.Bundle +import android.util.Log import android.view.Menu import android.view.MenuItem import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.toast import com.theartofdev.edmodo.cropper.CropImageView import kotlinx.android.synthetic.main.activity_edit.* +import java.io.File +import java.io.FileOutputStream +import java.io.IOException class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener { + val TAG: String = EditActivity::class.java.simpleName + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_edit) @@ -19,6 +29,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener return } + Log.e("DEBUG", "uri $TAG ${intent.data}") crop_image_view.apply { guidelines = CropImageView.Guidelines.OFF setOnCropImageCompleteListener(this@EditActivity) @@ -47,9 +58,36 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) { if (result.error == null) { - val bitmap = result.bitmap + val path = intent.data.path + val file = File(path) + var out: FileOutputStream? = null + try { + out = FileOutputStream(file) + result.bitmap.compress(getFileExtension(file), 100, out) + setResult(Activity.RESULT_OK, intent) + } catch (e: Exception) { + Log.e(TAG, "Crop compressing failed $e") + } finally { + try { + out?.close() + } catch (e: IOException) { + Log.e(TAG, "FileOutputStream closing failed $e") + } + } + + MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { s: String, uri: Uri -> + finish() + }) } else { toast("${getString(R.string.image_croping_failed)} ${result.error.message}") } } + + private fun getFileExtension(file: File): Bitmap.CompressFormat { + return when (file.extension) { + "png" -> Bitmap.CompressFormat.PNG + "webp" -> Bitmap.CompressFormat.WEBP + else -> Bitmap.CompressFormat.JPEG + } + } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index caa1ee787..73a392aab 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed Über diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index b3c91e7e6..09705e7e9 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed Acerca de diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 21ce2b558..69ef747a2 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed Informazioni diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 7e9d0c386..265a423c0 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed アプリについて diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index f24b1d1c2..8a3f7fbee 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed Sobre diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index c3255af01..ba2097d03 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed Om diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 074a9670f..1bf03ff60 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -49,6 +49,7 @@ Rotate Image croping failed: Invalid image path + Image editing failed About