Skip to content

Commit

Permalink
implement camera image picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Atwa committed Oct 30, 2022
1 parent 5d08ae8 commit 3eef0de
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ them with api requests to upload them to server, -Not a single permission is req
| 1.0.0 | Initial release |
| 1.0.1 | Increase buffer size to handle larger files (8GB Max) |
| 1.0.3 | Fix bug : LifecycleOwner Activity is attempting to register while current state is RESUMED. |
| 1.0.4 | Enhancement : Avoid possible activity reference leaking
Feature : Implement camera image picker. |



### Contribution
Expand Down
2 changes: 1 addition & 1 deletion filepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ afterEvaluate {

groupId = 'com.github.atwa'
artifactId = 'filepicker'
version = '1.0.3'
version = '1.0.4-alpha1'
}
}
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ interface FilePicker {
*/
fun pickImage(onImagePicked: (Pair<Bitmap?, File?>?) -> Unit)

/**
* launching intent for picking images from gallery.
* This method should be called from activity and the result will be provided in the callback.
*
* Parameters:
* @param onImagePicked Callback to receive the picker gallery image result.
*/
fun captureCameraImage(onImagePicked: (Pair<Bitmap?, File?>?) -> Unit)

/**
* launching intent for picking pdf files.
* This method should be called from activity and the result will be provided in the callback.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.atwa.filepicker.decoder.Decoder
import com.atwa.filepicker.decoder.UriDecoder
import com.atwa.filepicker.request.*
import com.atwa.filepicker.request.FilePickerRequest
import com.atwa.filepicker.request.ImageCameraRequest
import com.atwa.filepicker.request.ImagePickerRequest
import com.atwa.filepicker.request.PdfPickerRequest
import com.atwa.filepicker.request.PickerRequest
Expand All @@ -30,6 +32,11 @@ internal class StorageFilePicker(private val activity: WeakReference<AppCompatAc
initialize()
}

override fun captureCameraImage(onImagePicked: (Pair<Bitmap?, File?>?) -> Unit) {
pickerRequest = ImageCameraRequest(decoder, onImagePicked)
initialize()
}

override fun pickPdf(onPdfPicked: (Pair<String?, File?>?) -> Unit) {
pickerRequest = PdfPickerRequest(decoder, onPdfPicked)
initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ internal interface Decoder {
fun getStorageImage(imageUri: Uri?): Flow<Pair<Bitmap?, File?>?>
fun getStoragePDF(pdfUri: Uri?): Flow<Pair<String?, File>?>
fun getStorageFile(pdfUri: Uri?): Flow<Pair<String?, File>?>
fun createCameraOutputUri() : Uri
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory.decodeFileDescriptor
import android.net.Uri
import android.provider.OpenableColumns
import com.atwa.filepicker.stream.Streamer
import com.atwa.filepicker.stream.FileStreamer
import com.atwa.filepicker.stream.Streamer
import kotlinx.coroutines.flow.flow
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.util.concurrent.TimeUnit

internal class UriDecoder(
private val context: Context?,
Expand Down Expand Up @@ -59,6 +60,11 @@ internal class UriDecoder(
emit(result)
}

override fun createCameraOutputUri(): Uri {
val timeStamp = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()).toString()
return Uri.parse("${context?.cacheDir}$timeStamp")
}


private fun getBitMap(): Pair<Bitmap?, File?>? {
return uri?.let { uri ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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<Bitmap?, File?>?) -> Unit
) : PickerRequest {

private val outputUri : Uri by lazy { decoder.createCameraOutputUri() }

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) {
var result: Pair<Bitmap?, File?>? = null
decoder.getStorageImage(outputUri).collect { result = it }
withContext(Dispatchers.Main) {
onPhotoTaken(result)
}
}
}

0 comments on commit 3eef0de

Please sign in to comment.