fix saving photos on SD card

This commit is contained in:
tibbi 2016-11-06 11:12:55 +01:00
parent d74800dcc2
commit 8c2a5479f1
4 changed files with 35 additions and 15 deletions

View File

@ -3,16 +3,13 @@ package com.simplemobiletools.camera
import android.net.Uri import android.net.Uri
import android.os.AsyncTask import android.os.AsyncTask
import android.util.Log import android.util.Log
import com.simplemobiletools.camera.activities.MainActivity import com.simplemobiletools.camera.activities.MainActivity
import com.simplemobiletools.camera.extensions.getFileDocument
import java.io.File import com.simplemobiletools.camera.extensions.needsStupidWritePermissions
import java.io.FileNotFoundException import java.io.*
import java.io.FileOutputStream
import java.io.IOException
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
class PhotoProcessor(activity: MainActivity, val uri: Uri?) : AsyncTask<ByteArray, Void, String>() { class PhotoProcessor(val activity: MainActivity, val uri: Uri?) : AsyncTask<ByteArray, Void, String>() {
companion object { companion object {
private val TAG = PhotoProcessor::class.java.simpleName private val TAG = PhotoProcessor::class.java.simpleName
private var mActivity: WeakReference<MainActivity>? = null private var mActivity: WeakReference<MainActivity>? = null
@ -23,7 +20,7 @@ class PhotoProcessor(activity: MainActivity, val uri: Uri?) : AsyncTask<ByteArra
} }
override fun doInBackground(vararg params: ByteArray): String { override fun doInBackground(vararg params: ByteArray): String {
var fos: FileOutputStream? = null var fos: OutputStream? = null
val path: String val path: String
try { try {
if (uri != null) { if (uri != null) {
@ -37,10 +34,17 @@ class PhotoProcessor(activity: MainActivity, val uri: Uri?) : AsyncTask<ByteArra
} }
val photoFile = File(path) val photoFile = File(path)
if (activity.needsStupidWritePermissions(path)) {
var document = activity.getFileDocument(path)
document = document.createFile("", path.substring(path.lastIndexOf('/') + 1))
fos = activity.contentResolver.openOutputStream(document.uri)
} else {
fos = FileOutputStream(photoFile)
}
val data = params[0] val data = params[0]
fos = FileOutputStream(photoFile) fos?.write(data)
fos.write(data) fos?.close()
fos.close()
return photoFile.absolutePath return photoFile.absolutePath
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
Log.e(TAG, "PhotoProcessor file not found: " + e.message) Log.e(TAG, "PhotoProcessor file not found: " + e.message)

View File

@ -12,8 +12,7 @@ import android.view.View
import android.widget.AdapterView import android.widget.AdapterView
import com.simplemobiletools.camera.R import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.dialogs.WritePermissionDialog import com.simplemobiletools.camera.dialogs.WritePermissionDialog
import com.simplemobiletools.camera.extensions.isKitkat import com.simplemobiletools.camera.extensions.needsStupidWritePermissions
import com.simplemobiletools.camera.extensions.isPathOnSD
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
import kotlinx.android.synthetic.main.activity_settings.* import kotlinx.android.synthetic.main.activity_settings.*
import java.io.File import java.io.File
@ -68,7 +67,7 @@ class SettingsActivity : SimpleActivity() {
override fun onSuccess(pickedPath: String) { override fun onSuccess(pickedPath: String) {
mCurrPath = pickedPath.trimEnd('/') mCurrPath = pickedPath.trimEnd('/')
if (!File(pickedPath).canWrite() && isPathOnSD(pickedPath) && isKitkat() && mConfig.treeUri.isEmpty()) { if (!File(pickedPath).canWrite() && needsStupidWritePermissions(pickedPath) && mConfig.treeUri.isEmpty()) {
WritePermissionDialog(this@SettingsActivity, object : WritePermissionDialog.OnWritePermissionListener { WritePermissionDialog(this@SettingsActivity, object : WritePermissionDialog.OnWritePermissionListener {
override fun onCancelled() { override fun onCancelled() {
mCurrPath = mConfig.savePhotosFolder mCurrPath = mConfig.savePhotosFolder

View File

@ -15,7 +15,7 @@ class WritePermissionDialog(val context: Context, val listener: OnWritePermissio
.setTitle(context.resources.getString(R.string.confirm_storage_access_title)) .setTitle(context.resources.getString(R.string.confirm_storage_access_title))
.setView(view) .setView(view)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() }) .setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setOnCancelListener { listener?.onCancelled() } .setOnCancelListener { listener.onCancelled() }
.create() .create()
dialog?.show() dialog?.show()

View File

@ -1,9 +1,26 @@
package com.simplemobiletools.camera.extensions package com.simplemobiletools.camera.extensions
import android.content.Context import android.content.Context
import android.net.Uri
import android.os.Build import android.os.Build
import android.support.v4.provider.DocumentFile
import com.simplemobiletools.camera.Config
import com.simplemobiletools.filepicker.extensions.getSDCardPath import com.simplemobiletools.filepicker.extensions.getSDCardPath
fun Context.needsStupidWritePermissions(path: String) = isPathOnSD(path) && isKitkat()
fun Context.isPathOnSD(path: String) = path.startsWith(getSDCardPath()) fun Context.isPathOnSD(path: String) = path.startsWith(getSDCardPath())
fun Context.isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT fun Context.isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
fun Context.getFileDocument(path: String): DocumentFile {
val relativePath = path.substring(getSDCardPath().length + 1)
var document = DocumentFile.fromTreeUri(this, Uri.parse(Config.newInstance(this).treeUri))
val parts = relativePath.split("/")
for (part in parts) {
val currDocument = document.findFile(part)
if (currDocument != null)
document = currDocument
}
return document
}