handle kitkat permissions at saving edited photos

This commit is contained in:
tibbi 2016-11-06 19:40:03 +01:00
parent 84f81ed3e2
commit dd983c9753
2 changed files with 41 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.gallery package com.simplemobiletools.gallery
import android.Manifest import android.Manifest
import android.annotation.TargetApi
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -125,7 +126,7 @@ class Utils {
View.SYSTEM_UI_FLAG_IMMERSIVE View.SYSTEM_UI_FLAG_IMMERSIVE
} }
fun getRealPathFromURI(context: Context, uri: Uri): String { fun getRealPathFromURI(context: Context, uri: Uri): String? {
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
val projection = arrayOf(MediaStore.Images.Media.DATA) val projection = arrayOf(MediaStore.Images.Media.DATA)
@ -157,5 +158,14 @@ class Utils {
} }
return document return document
} }
@TargetApi(Build.VERSION_CODES.KITKAT)
fun saveTreeUri(context: Context, resultData: Intent) {
val treeUri = resultData.data
Config.newInstance(context).treeUri = treeUri.toString()
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
context.contentResolver.takePersistableUriPermission(treeUri, takeFlags)
}
} }
} }

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.gallery.activities package com.simplemobiletools.gallery.activities
import android.app.Activity import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.Uri import android.net.Uri
@ -8,14 +9,18 @@ import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog
import com.simplemobiletools.gallery.extensions.toast import com.simplemobiletools.gallery.extensions.toast
import com.theartofdev.edmodo.cropper.CropImageView import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.* import kotlinx.android.synthetic.main.activity_edit.*
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.io.OutputStream
class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener { class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
val TAG: String = EditActivity::class.java.simpleName val TAG: String = EditActivity::class.java.simpleName
@ -93,9 +98,25 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
return return
} }
var out: FileOutputStream? = null var out: OutputStream? = null
try { try {
if (Utils.needsStupidWritePermissions(this, path)) {
if (!file.canWrite() && Config.newInstance(this).treeUri.isEmpty()) {
WritePermissionDialog(this, object : WritePermissionDialog.OnWritePermissionListener {
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE)
}
})
return
}
val document = Utils.getFileDocument(this, path)
out = contentResolver.openOutputStream(document.uri)
} else {
out = FileOutputStream(file) out = FileOutputStream(file)
}
bitmap.compress(getCompressionFormat(file), 90, out) bitmap.compress(getCompressionFormat(file), 90, out)
setResult(Activity.RESULT_OK, intent) setResult(Activity.RESULT_OK, intent)
} catch (e: Exception) { } catch (e: Exception) {
@ -116,6 +137,13 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
}) })
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == Constants.OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) {
Utils.saveTreeUri(this, resultData)
}
}
private fun getCompressionFormat(file: File): Bitmap.CompressFormat { private fun getCompressionFormat(file: File): Bitmap.CompressFormat {
return when (file.extension.toLowerCase()) { return when (file.extension.toLowerCase()) {
"png" -> Bitmap.CompressFormat.PNG "png" -> Bitmap.CompressFormat.PNG