|
|
|
|
@@ -10,6 +10,7 @@ import android.provider.MediaStore.Images
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener
|
|
|
|
|
import org.moxxy.moxxy_native.AsyncRequestTracker
|
|
|
|
|
import org.moxxy.moxxy_native.BUFFER_SIZE
|
|
|
|
|
import org.moxxy.moxxy_native.PICK_FILES_REQUEST
|
|
|
|
|
import org.moxxy.moxxy_native.PICK_FILE_REQUEST
|
|
|
|
|
import org.moxxy.moxxy_native.PICK_FILE_WITH_DATA_REQUEST
|
|
|
|
|
@@ -20,6 +21,26 @@ import java.io.IOException
|
|
|
|
|
import java.io.InputStream
|
|
|
|
|
import java.io.OutputStream
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Attempt to replace the file extension in @fileName with @newExtension. If @newExtension is null,
|
|
|
|
|
* then @fileName is returned verbatim.
|
|
|
|
|
* */
|
|
|
|
|
private fun maybeReplaceExtension(fileName: String, newExtension: String?): String {
|
|
|
|
|
if (newExtension == null) {
|
|
|
|
|
return fileName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(newExtension[0] == '.')
|
|
|
|
|
val parts = fileName.split(".")
|
|
|
|
|
return if (parts.size == 1) {
|
|
|
|
|
"$fileName$newExtension"
|
|
|
|
|
} else {
|
|
|
|
|
// Split at the ".", join all but the list end together and append the new extension
|
|
|
|
|
val fileNameWithoutExtension = parts.subList(0, parts.size - 1).joinToString(".")
|
|
|
|
|
"$fileNameWithoutExtension$newExtension"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PickerResultListener(private val context: Context) : ActivityResultListener {
|
|
|
|
|
/*
|
|
|
|
|
* Attempt to deduce the filename for the URI @uri.
|
|
|
|
|
@@ -43,7 +64,7 @@ class PickerResultListener(private val context: Context) : ActivityResultListene
|
|
|
|
|
|
|
|
|
|
// Note: This is a workaround for the Dart image library failing to parse the file
|
|
|
|
|
// because displayName somehow is always ".jpg", which confuses image.
|
|
|
|
|
result = if (fileExtension != null) "$displayName$fileExtension" else displayName
|
|
|
|
|
result = maybeReplaceExtension(displayName, fileExtension)
|
|
|
|
|
Log.d(TAG, "Returning $result as filename (MIME: $mimeType)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -63,7 +84,7 @@ class PickerResultListener(private val context: Context) : ActivityResultListene
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 33) {
|
|
|
|
|
android.os.FileUtils.copy(input, output)
|
|
|
|
|
} else {
|
|
|
|
|
val buffer = ByteArray(4096)
|
|
|
|
|
val buffer = ByteArray(BUFFER_SIZE)
|
|
|
|
|
while (input.read(buffer).also {} != -1) {
|
|
|
|
|
output.write(buffer)
|
|
|
|
|
}
|
|
|
|
|
@@ -106,7 +127,7 @@ class PickerResultListener(private val context: Context) : ActivityResultListene
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val returnBuffer = mutableListOf<Byte>()
|
|
|
|
|
val readBuffer = ByteArray(4096)
|
|
|
|
|
val readBuffer = ByteArray(BUFFER_SIZE)
|
|
|
|
|
try {
|
|
|
|
|
val inputStream = context.contentResolver.openInputStream(data!!.data!!)!!
|
|
|
|
|
while (inputStream.read(readBuffer).also {} != -1) {
|
|
|
|
|
|