Getting List of Files/Folders
Let's find out how to get a list of folders and files by a specified path. The path doesn't need to be full; for example, instead of passing /storage/emulated/0/DCIM/Camera, you only pass /DCIM/Camera, and this applies EVERYWHERE.
This method will be return a JSONArray, but I changed it to MutableList:
Java
private static List<DirectoryContents> getListOfObjects(Activity ac) {
MainActivity act = (MainActivity) ac;
ContentResolver contentResolver = ac.getContentResolver();
Uri uri = Uri.parse("content://com.anready.croissant.files")
.buildUpon()
.appendQueryParameter("path", act.getPath())
.appendQueryParameter("command", "list")
.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) {
alertDialog(ac, "Data column not found");
return new ArrayList<>();
}
JSONArray jsonArray = new JSONArray(cursor.getString(dataIndex));
if (error(jsonArray)) {
System.out.println("Error: " + jsonArray.getJSONObject(0).getString("error"));
return new ArrayList<>();
}
List<DirectoryContents> fullList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject fileInfo = jsonArray.getJSONObject(i);
fullList.add(new DirectoryContents(
fileInfo.getString("name"),
fileInfo.getBoolean("type"),
fileInfo.getBoolean("visibility")
));
}
return fullList;
} else {
System.out.println("Error while getting data!");
}
} catch (Exception e) {
System.out.println(ac, "Error while getting data!\n" + e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return new ArrayList<>();
}
private static boolean error(JSONArray jsonArray) {
try {
jsonArray.getJSONObject(0).getString("error");
return true;
} catch (JSONException e) {
return false;
}
}
//..... in another class with name DirectoryContents \\
public class DirectoryContents {
public final String name;
public final boolean isDirectory;
public final boolean isHidden;
public DirectoryContents(String s, boolean b, boolean b1) {
this.name = s;
this.isDirectory = b;
this.isHidden = b1;
}
}
By calling this method: getListOfObjects(this, "/"). It will return List of DirectoryContents, you can receive error code: 02 and 01 (See the article Errors). You can use this List as you wish
That's it! Go to the next page