It was a permissions error. In the Logcat, I saw
2019-07-30 09:32:57.566 11640-12481/? E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=13823, uid=10149 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:633)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:503)
at android.content.ContentProvider$Transport.query(ContentProvider.java:214)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:102)
at android.os.Binder.execTransact(Binder.java:697)
Then I saw that I had commented out the permissions code beforehand to get a better understanding of what's going on. Mission accomplished smh. I actually just deleted the commented code and wrote it from scratch to drive the lesson home.
Here's what fixed it:
- Add permission to
AndroidManifest.xml
<uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE”/>
- Check for, and grant permission to the app when the user presses the Upload Image button
buttonUploadImage!!.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val permissions = arrayOf(permission.READ_EXTERNAL_STORAGE)
val intent = Intent()
intent.action = Intent.ACTION_GET_CONTENT
intent.type = "image/*"
if (checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
permission.READ_EXTERNAL_STORAGE)) {
// Explain why the permission is needed
} else {
ActivityCompat.requestPermissions(this, permissions, PERMISSION_CODE)
}
} else {
// Permission has already been granted
startActivityForResult(intent, PERMISSION_CODE)
}
}
}
Resources