Image Picker - Krunal-Kevadiya/kotlin-common GitHub Wiki

Here i have provided to pick/capture - image/video from local storage. without any kind of permission check because this DSL method in-build check run-time permission. so no need to write more code. you have just 2 minutes to integrated and pick image from Gallery or capture image from camera.

  1. Pick image from gallery.
   PickMediaExtensions.instance.pickFromCamera(this) { resultCode: Int, path: String ->
       resultData(resultCode, path)
   }
  1. Capture image from camera.
   PickMediaExtensions.instance.pickFromGallery(this) { resultCode: Int, path: String ->
       resultData(resultCode, path)
   }
  1. Pick video form gallery.
   PickMediaExtensions.instance.pickFromVideo(this) { resultCode: Int, path: String ->
       resultData(resultCode, path)
   }
  1. Record video from camera and pick.
   PickMediaExtensions.instance.pickFromVideoCamera(this) { resultCode: Int, path: String ->
       resultData(resultCode, path)
   }
  • Here below is resultData method to get actual path.
   private fun resultData(resultCode: Int, path: String) {
        //resultCode return to status for success, fail or permission issue.
        if (resultCode == PickMediaExtensions.PICK_PERMISSION_DENIED) {
            //Here display user to prompt to denied permission to access gallery or camera
        } else if (resultCode == PickMediaExtensions.PICK_SUCCESS) {
            val actualPath = Uri.parse(path).getRealPathFromURI(this)
            if (actualPath != null) {
                `actualPath` // here actualPath will you selected image or video file path.
                requestMediaScanner(actualPath)
            } else {
                val realPath = Uri.parse(path).getRealPath(this)
                if (realPath != null) {
                    `realPath` // here realPath will you selected image or video file path.
                    requestMediaScanner(realPath)
                } else {
                    //error unknown
                }
            }
        }
    }