Is path exist
Checking for the existence of a path is one of the easiest tasks; you simply need to specify an incomplete path to the directory. Example of a correct path: /DCIM/Camera. Incorrect path: /storage/emulated/0/DCIM/Camera.
Simple:
Kotlin
fun isPathExist(ac: Activity, path: String): Boolean {
val contentResolver: ContentResolver = ac.contentResolver
val uri = Uri.parse("content://com.anready.croissant.files")
.buildUpon()
.appendQueryParameter("path", path)
.appendQueryParameter("command", "pathExist")
.build()
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, null, null, null, null)
if (cursor != null && cursor.moveToFirst()) {
val dataIndex = cursor.getColumnIndex("response")
if (dataIndex == -1) {
println("Data column not found")
return false
}
val jsonArray = JSONArray(cursor.getString(dataIndex))
if (error(jsonArray)) {
println("Error: " + jsonArray.getJSONObject(0).getString("error"))
return false
}
val fileInfo = jsonArray.getJSONObject(0)
return fileInfo.getBoolean("result")
} else {
println("Error while getting data!")
}
} catch (e: Exception) {
println("Error while getting data!\n" + e.message)
} finally {
cursor?.close()
}
return false
}
private fun error(jsonArray: JSONArray): Boolean {
try {
val error = jsonArray.getJSONObject(0)
error.getString("error")
return true
} catch (e: JSONException) {
return false
}
}
By calling isPathExist(this, "/DCIM/Camera"): It will return true (directory exist), false (directory not exist), or ERR_01, view on page with errors
That's it! Go to the next page