[英]Why is contentResolver.query() returning null?
I am refactoring the AWS S3TransferUtilitySample from Java to Kotlin, and hit an exception when I try to upload an image. 我将AWS S3TransferUtilitySample从Java重构为Kotlin,并在尝试上传图像时遇到异常。 I traced the error to after the gallery intent returns to my app with an image URI.
在图库意图返回带有图像URI的应用程序后,我将错误跟踪到了。 Within
onActivityResult
, getContentResolver.query(...)
is the problem. 在
onActivityResult
, getContentResolver.query(...)
是问题所在。
cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
translates in my code to cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
. cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
。 The query
argument values at this point are: 此时的
query
参数值为:
uri
: content://media/external/images/media
uri
: content://media/external/images/media
projection
: {"_data"}
projection
: {"_data"}
selection
: "_id=?"
selection
: "_id=?"
selectionArgs
: {"3812"}
selectionArgs
: {"3812"}
sortOrder
: null
sortOrder
: null
I looked up the documentation for contentResolver
and I think I may be unpacking the query arguments wrong. 我查看了
contentResolver
的文档,我认为我可能错误地解开了查询参数。 I saw this answer , but nothing there helped. 我看到了这个答案 ,但没有任何帮助。 Any suggestions?
有什么建议么?
It was a permissions error. 这是一个权限错误。 In the Logcat, I saw
在Logcat中,我看到了
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: 修复问题的方法如下:
AndroidManifest.xml
AndroidManifest.xml
添加权限 <uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE”/>
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 资源资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.