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,12 +67,30 @@ 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
saveBitmapToFile(result.bitmap, uri.path)
} else if (uri.scheme == "content") {
saveBitmapToFile(result.bitmap, convertMediaUriToPath(uri))
} else {
toast(R.string.unknown_file_location)
finish()
}
} else {
toast("${getString(R.string.image_croping_failed)} ${result.error.message}")
}
}
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)
result.bitmap.compress(getCompressionFormat(file), 100, out)
bitmap.compress(getCompressionFormat(file), 100, out)
setResult(Activity.RESULT_OK, intent)
} catch (e: Exception) {
Log.e(TAG, "Crop compressing failed $e")
@ -89,15 +108,6 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
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}")
}
}
private fun getCompressionFormat(file: File): Bitmap.CompressFormat {
@ -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>