Advertisement
jasurbekdev

Pick image from phone storage

Dec 7th, 2022
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.83 KB | Source Code | 0 0
  1. // Method for starting the activity for selecting image from phone storage
  2.     private fun pick() {
  3.         verifyStoragePermissions(requireActivity())
  4.         val intent = Intent(Intent.ACTION_GET_CONTENT)
  5.         intent.type = "image/*"
  6.         resultLauncher.launch(intent)
  7.     }
  8.  
  9.     fun verifyStoragePermissions(activity: Activity?) {
  10.         // Check if we have write permission
  11.         val permission = ActivityCompat.checkSelfPermission(
  12.             requireActivity(),
  13.             WRITE_EXTERNAL_STORAGE
  14.         )
  15.         if (permission != PackageManager.PERMISSION_GRANTED) {
  16.             // We don't have permission so prompt the user
  17.             ActivityCompat.requestPermissions(
  18.                 requireActivity(),
  19.                 PERMISSIONS_STORAGE,
  20.                 REQUEST_EXTERNAL_STORAGE
  21.             )
  22.         }
  23.     }
Tags: image pick
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement