diff --git a/filepicker/build.gradle b/filepicker/build.gradle index da9e0d4..c016273 100644 --- a/filepicker/build.gradle +++ b/filepicker/build.gradle @@ -44,7 +44,7 @@ afterEvaluate { groupId = 'com.github.atwa' artifactId = 'filepicker' - version = '1.0.4-alpha1' + version = '1.0.4-alpha2' } } repositories { diff --git a/filepicker/src/main/java/com/atwa/filepicker/core/StorageFilePicker.kt b/filepicker/src/main/java/com/atwa/filepicker/core/StorageFilePicker.kt index 8f9a1f1..e300f90 100644 --- a/filepicker/src/main/java/com/atwa/filepicker/core/StorageFilePicker.kt +++ b/filepicker/src/main/java/com/atwa/filepicker/core/StorageFilePicker.kt @@ -20,6 +20,7 @@ import java.lang.ref.WeakReference internal class StorageFilePicker(private val activity: WeakReference) : FilePicker { private lateinit var pickerRequest: PickerRequest + private lateinit var cameraRequest: ImageCameraRequest private val decoder: Decoder by lazy { UriDecoder(activity.get()?.applicationContext) } private val filePickerLauncher = @@ -27,14 +28,19 @@ internal class StorageFilePicker(private val activity: WeakReference + (result.data?.extras?.get("data") as? Bitmap)?.let { processBitmap(it) } + } + override fun pickImage(onImagePicked: (Pair?) -> Unit) { pickerRequest = ImagePickerRequest(decoder, onImagePicked) initialize() } override fun captureCameraImage(onImagePicked: (Pair?) -> Unit) { - pickerRequest = ImageCameraRequest(decoder, onImagePicked) - initialize() + cameraRequest = ImageCameraRequest(decoder, onImagePicked) + cameraCaptureLauncher?.launch(cameraRequest.intent) } override fun pickPdf(onPdfPicked: (Pair?) -> Unit) { @@ -57,4 +63,10 @@ internal class StorageFilePicker(private val activity: WeakReference?> fun getStoragePDF(pdfUri: Uri?): Flow?> fun getStorageFile(pdfUri: Uri?): Flow?> - fun createCameraOutputUri() : Uri + fun saveStorageImage(bitmap: Bitmap): Flow?> } \ No newline at end of file diff --git a/filepicker/src/main/java/com/atwa/filepicker/decoder/UriDecoder.kt b/filepicker/src/main/java/com/atwa/filepicker/decoder/UriDecoder.kt index 83af482..0c6f258 100644 --- a/filepicker/src/main/java/com/atwa/filepicker/decoder/UriDecoder.kt +++ b/filepicker/src/main/java/com/atwa/filepicker/decoder/UriDecoder.kt @@ -9,13 +9,13 @@ import android.net.Uri import android.provider.OpenableColumns import com.atwa.filepicker.stream.FileStreamer import com.atwa.filepicker.stream.Streamer +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow -import java.io.File -import java.io.FileInputStream -import java.io.FileOutputStream -import java.io.IOException +import java.io.* +import java.nio.ByteBuffer import java.util.concurrent.TimeUnit + internal class UriDecoder( private val context: Context?, private val streamer: Streamer = FileStreamer() @@ -60,11 +60,30 @@ internal class UriDecoder( emit(result) } - override fun createCameraOutputUri(): Uri { - val timeStamp = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()).toString() - return Uri.parse("${context?.cacheDir}$timeStamp") + override fun saveStorageImage(bitmap: Bitmap): Flow?> = flow { + val result = try { + getFile(bitmap) + } catch (ex: IOException) { + println(ex.message) + null + } + emit(result) } + private fun getFile(bitmap: Bitmap): Pair { + val fileName = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()).toString() + val imageFile = File(context?.cacheDir, fileName) + + val byteArray = (bitmap.allocationByteCount * bitmap.height).run { + ByteBuffer.allocate(this) + }.apply { bitmap.copyPixelsToBuffer(this) }.array() + + val byteStream = ByteArrayInputStream(byteArray) + val outputStream = FileOutputStream(imageFile) + + streamer.copyFile(byteStream, outputStream) + return Pair(bitmap, imageFile) + } private fun getBitMap(): Pair? { return uri?.let { uri -> diff --git a/filepicker/src/main/java/com/atwa/filepicker/request/ImageCameraRequest.kt b/filepicker/src/main/java/com/atwa/filepicker/request/ImageCameraRequest.kt index ee2d5eb..5619f9d 100644 --- a/filepicker/src/main/java/com/atwa/filepicker/request/ImageCameraRequest.kt +++ b/filepicker/src/main/java/com/atwa/filepicker/request/ImageCameraRequest.kt @@ -2,33 +2,24 @@ package com.atwa.filepicker.request import android.content.Intent import android.graphics.Bitmap -import android.net.Uri -import android.os.Environment import android.provider.MediaStore import com.atwa.filepicker.decoder.Decoder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.collect import kotlinx.coroutines.withContext import java.io.File -import java.util.concurrent.TimeUnit internal class ImageCameraRequest( private val decoder: Decoder, private val onPhotoTaken: (Pair?) -> Unit -) : PickerRequest { +) { - private val outputUri : Uri by lazy { decoder.createCameraOutputUri() } + val intent: Intent + get() = Intent(MediaStore.ACTION_IMAGE_CAPTURE) - override val intent: Intent - get() = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply { - addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) - putExtra(MediaStore.EXTRA_OUTPUT, outputUri) - } - - - override suspend fun invokeCallback(uri: Uri) { + suspend fun invokeCameraCallback(bitmap: Bitmap) { var result: Pair? = null - decoder.getStorageImage(outputUri).collect { result = it } + decoder.saveStorageImage(bitmap).collect { result = it } withContext(Dispatchers.Main) { onPhotoTaken(result) }