Android Intent to Import Multiple Photos From Gallery (Kotlin)
June 19, 2018Start intent to select multiple photos from gallery.
class TestFragment : Fragment() {
companion object {
private const val REQUEST_PICK_PHOTO = 1
}
// ...
}
val intent = Intent()
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_PICK_PHOTO)
Handle activity result.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_PICK_PHOTO) {
if (resultCode == RESULT_OK) {
if (data == null) {
// something is wrong
}
val clipData = data.clipData
if (clipData != null) { // handle multiple photo
for (i in 0 until clipData.itemCount) {
val uri = clipData.getItemAt(i).uri
importPhoto(uri)
}
} else { // handle single photo
val uri = data?.data
importPhoto(uri)
}
}
}
}
Method to read image file and save it.
fun isImage(context: Context, uri: Uri): Boolean {
val mimeType = context.contentResolver.getType(uri) ?: return true
return mimeType.startsWith("image/")
}
private fun createImageFile(dir: File, fileName: String? = null): File {
if (fileName == null) {
val timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-kkmmss"))
return File.createTempFile("IMG_$timestamp", ".jpg", dir)
}
return File(dir, fileName)
}
fun copyUriToFile(context: Context, uri: Uri, outputFile: File) {
val inputStream = context.contentResolver.openInputStream(uri)
// copy inputStream to file using okio
/*
val source = Okio.buffer(Okio.source(inputStream))
val sink = Okio.buffer(Okio.sink(outputFile))
sink.writeAll(source)
sink.close()
source.close()
*/
val outputStream = FileOutputStream(outputFile)
inputStream.use { input ->
outputStream.use { output ->
input.copyTo(output)
}
}
}
fun importPhoto(uri: Uri): Boolean {
if (!isImage(context, uri)) {
// not image
return false
}
return try {
val photoFile = createImageFile(context.cacheDir)
copyUriToFile(context, uri, photoFile)
// addImageToGallery(photoFile)
true
}
catch (e: IOException) {
e.printStackTrace()
// handle error
false
}
}
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn