gpt4 book ai didi

java - Android - IllegalStateException : cursor. getString(idx) 不能为空

转载 作者:行者123 更新时间:2023-11-29 19:05:00 26 4
gpt4 key购买 nike

我已经按照这里的所有内容进行操作,但是为什么 cursor.getString(idx) 说它不能为 null?我提供了所有必要的参数..

这是我的getfilepath:

fun getFilePathFromUri(context: Context, imageURI: Uri): String? {

var cursor : Cursor? = null
var result: String

try {
val projection = arrayOf(MediaStore.Images.Media.DATA)

cursor = context.contentResolver.query(imageURI, projection, null, null, null)

if (cursor == null) {
result = imageURI.path
} else {
Log.d(AppConstants.TAG, "Path_img = " + imageURI)
Log.d(AppConstants.TAG, "Path_pth = " + imageURI.path)
Log.d(AppConstants.TAG, "Path_cursor = " + cursor)

cursor.moveToFirst()
val idx = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)
result = cursor.getString(idx)
}
return result
} finally {
cursor!!.close()
}
}

这里是错误:

12-04 14:34:44.971 2008-2008/com.xxx.project k E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxx.proj, PID: 2008
java.lang.IllegalStateException: cursor.getString(idx) must not be null
at com.xxx.proj.utils.ImageUtils$Companion.getFilePathFromUri(ImageUtils.kt:78)
at com.xxx.proj.api.Layer.createNewLayer(Layer.java:84)
at com.xxx.proj.dialog.EventMapCreationDialog.registerNewMap(EventMapCreationDialog.kt:266)
at com.xxx.proj.dialog.EventMapCreationDialog.onClick(EventMapCreationDialog.kt:82)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

根据 documentation cursor.getString():

Returns the value of the requested column as a String.

The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.

您收到一个 IllegalStateException。虽然范围很广,但这通常意味着在错误/非法时间调用了一个方法。也就是说,应用程序或环境未处于适合此方法的状态。这可能意味着参数、游标或值为空。

调用方法前检查列的返回类型:

if (cursor.getType(idx) == FIELD_TYPE_STRING) {
result = cursor.getString(idx);
}

关于java - Android - IllegalStateException : cursor. getString(idx) 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com