Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select multiple images from gallery in KOTLIN (want image path) [duplicate]

Tags:

android

kotlin

I am working on an Application for making a video from multiple images in kotlin. I got many code of java but can not convert it in propare way to kotlin code. Alwayse got an error cursor.getString(column_index) must not be null.
I am just beginner at Kotlin. so can anyone give a brief solution for my problem.

val cursor = contentResolver.query(uri,  filePathColumn, null, null, null)
 cursor!!.moveToFirst()
 val columnIndex = cursor.getColumnIndex(filePathColumn[0])
like image 776
Heet Parkhiya Avatar asked Jan 03 '19 10:01

Heet Parkhiya


People also ask

How do you select multiple photos in gallery?

How to select multiple photos on Android. Selecting all the photos saved locally on an Android phone is a lot easier than doing so on an iPhone: On stock Android, open the Files app and go to Images, tap the three dots in the top right corner of your screen, and hit Select all.

How do I get pictures from my kotlin gallery?

This example demonstrates how to pick an image from an image gallery on Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

Hey I m also suffering with same issue nd got the solution. just follow my code.

private var context: Context? = null
var PICK_IMAGE_MULTIPLE = 1
lateinit var imagePath: String
var imagesPathList: MutableList<String> = arrayListOf()

call gallery intent first

if (Build.VERSION.SDK_INT < 19) {
            var intent = Intent()
            intent.type = "image/*"
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
            intent.action = Intent.ACTION_GET_CONTENT
            startActivityForResult(
                Intent.createChooser(intent, "Select Picture")
                , PICK_IMAGE_MULTIPLE
            )
        } else {
            var intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            intent.type = "image/*"
            startActivityForResult(intent, PICK_IMAGE_MULTIPLE);
        }

now check onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        super.onActivityResult(requestCode, resultCode, data)
        // When an Image is picked
        if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK
            && null != data
        ) {
            if (data.getClipData() != null) {
                var count = data.clipData.itemCount
                for (i in 0..count - 1) {
                    var imageUri: Uri = data.clipData.getItemAt(i).uri
                    getPathFromURI(imageUri)
                }
            } else if (data.getData() != null) {
                var imagePath: String = data.data.path
                Log.e("imagePath", imagePath);
            }

            displayImageData()
        }
    }

    private fun getPathFromURI(uri: Uri) {
        var path: String = uri.path // uri = any content Uri

        val databaseUri: Uri
        val selection: String?
        val selectionArgs: Array<String>?
        if (path.contains("/document/image:")) { // files selected from "Documents"
            databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
            selection = "_id=?"
            selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])
        } else { // files selected from all other sources, especially on Samsung devices
            databaseUri = uri
            selection = null
            selectionArgs = null
        }
        try {
            val projection = arrayOf(
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.ORIENTATION,
                MediaStore.Images.Media.DATE_TAKEN
            ) // some example data you can query
            val cursor = contentResolver.query(
                databaseUri,
                projection, selection, selectionArgs, null
            )
            if (cursor.moveToFirst()) {
                val columnIndex = cursor.getColumnIndex(projection[0])
                imagePath = cursor.getString(columnIndex)
               // Log.e("path", imagePath);
                imagesPathList.add(imagePath)
            }
            cursor.close()
        } catch (e: Exception) {
            Log.e(TAG, e.message, e)
        }
    }
like image 156
Harvi Sirja Avatar answered Oct 16 '22 05:10

Harvi Sirja