https://stackoverflow.com/questions/69053061/android-studio-value-must-be-%E2%89%A5-0
Android Studio ์ Anayze > Inspect Code๋ฅผ ์คํํ๊ณ ๋ง๋ ๋นจ๊ฐ ์๋ฌ
Value must be ≥ 0
๋ฌธ์ ์ ์ฝ๋
val cursor = context.contentResolver.query(uri, null, null, null, null) ?: return null
cursor.moveToNext()
return try {
val path: String? = cursor.getString(cursor.getColumnIndex("_data"))
path
} catch (e: Exception) {
null
} finally {
cursor.close()
}
์คํ์ค๋ฒํ๋ก์ฐ ์ ๋ฐฐ๋ ๋ต๋ณ
https://developer.android.com/reference/android/database/Cursor#getColumnIndex(java.lang.String)
Cursor ํด๋์ค ์ ์ฝ๋ ํ์ธ
Cursor.getString() : ์ธ์ ๊ฐ ๋ฒ์๊ฐ 0๋ถํฐ ์์ํ๋ค.
Cursor.getColumnIndex() : ๋ฐํ ๊ฐ ๋ฒ์๊ฐ -1๋ถํฐ ์์ํ๋ค.
Cursor.getColumnIndexOrThrow() : ๋ฐํ ๊ฐ ๋ฒ์๊ฐ 0๋ถํฐ ์์ํ๋ค.
Cursor.getString()์ ์ธ์ ๊ฐ์ผ๋ก Cursor.getColumnIndex() ๋ณด๋ค๋ Cursor.getColumnIndexOrThrow()๋ฅผ ๊ถํ๋ ์ง ์๊ฒ ๋ค.
์์
val cursor = context.contentResolver.query(uri, null, null, null, null) ?: return null
cursor.moveToNext()
return try {
val path: String? = cursor.getString(cursor.getColumnIndexOrThrow("_data"))
path
} catch (e: Exception) {
null
} finally {
cursor.close()
}