8

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])
ADM
  • 20,406
  • 11
  • 52
  • 83
Heet Parkhiya
  • 117
  • 1
  • 11

2 Answers2

14

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)
        }
    }
Harvi Sirja
  • 2,472
  • 2
  • 18
  • 19
  • I finaly got this to work after hours of looking at the code and trying it with only select one image, not being an android user I didnt know when I click an image it selects it, I realised after a while I had to click next to the name to add a tick to it to be able to select multiple images (Head slap lol) – Tony Merritt Jan 04 '19 at 17:39
  • @TonyMerritt you have to long press for multi select. – Harvi Sirja Jan 05 '19 at 05:07
  • I got code in java but can not use successfully in kotllin. this is worked for me.. thank s man – Rashmi Andani Jan 05 '19 at 05:17
  • what is contentResolver ? – Jaimin Modi Nov 19 '19 at 10:08
-2

This is a solution using Github repo for your requirement.

In your app gradle file add these lines

   implementation 'com.github.esafirm.android-image-picker:imagepicker:1.13.1'
    // for experimental rx picker
    implementation 'com.github.esafirm.android-image-picker:rximagepicker:1.13.1'
    // If you have a problem with Glide, please use the same Glide version or simply open an issue
    implementation 'com.github.bumptech.glide:glide:4.8.0'

in Java class call this to pick or take image

 startActivityForResult(ImagePicker.create(getActivity())
                .multi()
                .folderMode(true)
                .returnMode(ReturnMode.ALL)
                .getIntent(getActivity()), IpCons.RC_IMAGE_PICKER);

and in onActivityResult() get the arraylist of selected images

 @Override
    protected void onActivityResult(int requestCode, final int resultCode, Intent data) {
        if (ImagePicker.shouldHandle(requestCode, resultCode, data)) {
            // Get a list of picked images
            List<Image> images = ImagePicker.getImages(data)
            // do your stuff here

            // or get a single image only
            //Image image = ImagePicker.getFirstImageOrNull(data)
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

This code is less complex and no need to handle image multiple selection , just adding multi() to enable multiple selection.

Note:- Copy this code and paste in your kotlin project , the converter will automatically convert it to kotlin

Quick learner
  • 10,632
  • 4
  • 45
  • 55