Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to make File by Real Path through Uri #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/src/main/java/gun0912/tedimagepicker/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import gun0912.tedimagepicker.builder.TedImagePicker
import gun0912.tedimagepicker.builder.TedRxImagePicker
import gun0912.tedimagepicker.sample.databinding.ActivityMainBinding
import gun0912.tedimagepicker.sample.databinding.ItemImageBinding
import gun0912.tedimagepicker.util.ToastUtil.context
import gun0912.tedimagepicker.util.fetchImage

class MainActivity : AppCompatActivity() {

Expand All @@ -26,6 +28,7 @@ class MainActivity : AppCompatActivity() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

setNormalSingleButton()
setNormalSingleButtonFile()
setNormalMultiButton()
setRxSingleButton()
setRxMultiButton()
Expand All @@ -40,6 +43,11 @@ class MainActivity : AppCompatActivity() {
}
}

private fun setNormalSingleButtonFile() {
TedImagePicker.with(this)
.start { uri -> showSingleImageToFile(uri) }
}

private fun setNormalMultiButton() {
binding.btnNormalMulti.setOnClickListener {
TedImagePicker.with(this)
Expand Down Expand Up @@ -88,6 +96,12 @@ class MainActivity : AppCompatActivity() {

}

private fun showSingleImageToFile(uri: Uri) {

suspend { fetchImage(context).let { uri.toString() } }

}


private fun showMultiImage(uriList: List<Uri>) {
this.selectedUriList = uriList
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gun0912.tedimagepicker.util

import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore
import androidx.loader.content.CursorLoader
import gun0912.tedimagepicker.builder.TedImagePicker
import gun0912.tedimagepicker.builder.type.MediaType
import java.io.File
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

suspend fun fetchImage(context: Context): File? = suspendCoroutine {
TedImagePicker.with(context)
.mediaType(MediaType.IMAGE)
.showTitle(false)
.cancelListener { it.resume(null) }
.errorListener { _ -> it.resume(null) }
.start { uri ->
it.resume(File(getRealPathFromURI(uri, context)!!))
}
}

private fun getRealPathFromURI(uri: Uri, context: Context): String? {
val proj = arrayOf(MediaStore.Images.Media.DATA)
val loader = CursorLoader(context, uri, proj, null, null, null)
val cursor: Cursor = loader.loadInBackground()!!
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
val result = cursor.getString(columnIndex)
cursor.close()
return result
}