diff --git a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt index 4c217675..cc2cbba8 100644 --- a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt +++ b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.kt @@ -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() { +class PhotoProcessor(val activity: MainActivity, val uri: Uri?) : AsyncTask() { companion object { private val TAG = PhotoProcessor::class.java.simpleName private var mActivity: WeakReference? = null @@ -23,7 +20,7 @@ class PhotoProcessor(activity: MainActivity, val uri: Uri?) : AsyncTask dialogConfirmed() }) - .setOnCancelListener { listener?.onCancelled() } + .setOnCancelListener { listener.onCancelled() } .create() dialog?.show() diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt index a63b2741..ae728300 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt @@ -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 +}