handle content:// uri scheme properly too, close #28

This commit is contained in:
tibbi 2016-10-08 16:08:10 +02:00
parent a42e9b09fe
commit f765bf7dc9
9 changed files with 51 additions and 25 deletions

View file

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

View file

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

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">Über</string>

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">Acerca de</string>

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">Informazioni</string>

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">アプリについて</string>

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">Sobre</string>

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">Om</string>

View file

@ -53,6 +53,7 @@
<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>
<string name="error_saving_file">Could not overwrite the source file</string>
<!-- About -->
<string name="about">About</string>