fix saving photos on SD card
This commit is contained in:
parent
d74800dcc2
commit
8c2a5479f1
|
@ -3,16 +3,13 @@ package com.simplemobiletools.camera
|
|||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import android.util.Log
|
||||
|
||||
import com.simplemobiletools.camera.activities.MainActivity
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import com.simplemobiletools.camera.extensions.getFileDocument
|
||||
import com.simplemobiletools.camera.extensions.needsStupidWritePermissions
|
||||
import java.io.*
|
||||
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 {
|
||||
private val TAG = PhotoProcessor::class.java.simpleName
|
||||
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 {
|
||||
var fos: FileOutputStream? = null
|
||||
var fos: OutputStream? = null
|
||||
val path: String
|
||||
try {
|
||||
if (uri != null) {
|
||||
|
@ -37,10 +34,17 @@ class PhotoProcessor(activity: MainActivity, val uri: Uri?) : AsyncTask<ByteArra
|
|||
}
|
||||
|
||||
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]
|
||||
fos = FileOutputStream(photoFile)
|
||||
fos.write(data)
|
||||
fos.close()
|
||||
fos?.write(data)
|
||||
fos?.close()
|
||||
return photoFile.absolutePath
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.e(TAG, "PhotoProcessor file not found: " + e.message)
|
||||
|
|
|
@ -12,8 +12,7 @@ import android.view.View
|
|||
import android.widget.AdapterView
|
||||
import com.simplemobiletools.camera.R
|
||||
import com.simplemobiletools.camera.dialogs.WritePermissionDialog
|
||||
import com.simplemobiletools.camera.extensions.isKitkat
|
||||
import com.simplemobiletools.camera.extensions.isPathOnSD
|
||||
import com.simplemobiletools.camera.extensions.needsStupidWritePermissions
|
||||
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
|
||||
import kotlinx.android.synthetic.main.activity_settings.*
|
||||
import java.io.File
|
||||
|
@ -68,7 +67,7 @@ class SettingsActivity : SimpleActivity() {
|
|||
|
||||
override fun onSuccess(pickedPath: String) {
|
||||
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 {
|
||||
override fun onCancelled() {
|
||||
mCurrPath = mConfig.savePhotosFolder
|
||||
|
|
|
@ -15,7 +15,7 @@ class WritePermissionDialog(val context: Context, val listener: OnWritePermissio
|
|||
.setTitle(context.resources.getString(R.string.confirm_storage_access_title))
|
||||
.setView(view)
|
||||
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
|
||||
.setOnCancelListener { listener?.onCancelled() }
|
||||
.setOnCancelListener { listener.onCancelled() }
|
||||
.create()
|
||||
|
||||
dialog?.show()
|
||||
|
|
|
@ -1,9 +1,26 @@
|
|||
package com.simplemobiletools.camera.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.support.v4.provider.DocumentFile
|
||||
import com.simplemobiletools.camera.Config
|
||||
import com.simplemobiletools.filepicker.extensions.getSDCardPath
|
||||
|
||||
fun Context.needsStupidWritePermissions(path: String) = isPathOnSD(path) && isKitkat()
|
||||
|
||||
fun Context.isPathOnSD(path: String) = path.startsWith(getSDCardPath())
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue