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

public static boolean isPathExist(Activity ac, String path) {
    ContentResolver contentResolver = ac.getContentResolver();
    Uri uri = Uri.parse("content://com.anready.croissant.files")
            .buildUpon()
            .appendQueryParameter("path", path)
            .appendQueryParameter("command", "pathExist")
            .build();

    Cursor cursor = null;
    try {
        cursor = contentResolver.query(uri, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int dataIndex = cursor.getColumnIndex("response");
            if (dataIndex == -1) {
                System.out.println("Data column not found");
                return false;
            }

            JSONArray jsonArray = new JSONArray(cursor.getString(dataIndex));
            if (error(jsonArray)) {
                System.out.println("Error: " + jsonArray.getJSONObject(0).getString("error"));
                return false;
            }

            return jsonArray.getJSONObject(0).getBoolean("result");
        } else {
            System.out.println("Error while getting data!");
        }
    } catch (Exception e) {
        System.out.println("Error while getting data!\n" + e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return false;
}

private boolean error(JSONArray jsonArray) {
    try {
        JSONObject error = jsonArray.getJSONObject(0);
        error.getString("error");
        return true;
    } catch (JSONException e) {
        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