I have an issue for months, need to show all list file path (or Uri) of file ".epub" on my phone (SD-card, memory card). I use this to get root Uri (user pick folder):
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
resultUri = data.getData();
The result uri will looks like: content://com.android.externalstorage.documents/tree/primary%3ABooks
I used a recursive function like this to fetch that Uri, but the function run very slow for result:
DocumentFile F0 = DocumentFile.fromTreeUri(context, inputUri);
if (F0 != null) {
DocumentFile[] F1 = F0.listFiles();
for (DocumentFile F2 : F1) {
recuisiveUri(F2);
}
}
private void recuisiveUri(DocumentFile F2) {
Log.d(TAG, "recuisiveUri: F2 name = " + F2.getName()) ;
if(F2.canRead()
&& F2.exists()
&& F2.canWrite()
&& F2.canRead()
&& !F2.getName().contains("Android")
&& !F2.getName().contains("data")
&& !F2.getName().contains("DCIM")
){
if (F2.isDirectory()) {
DocumentFile[] F3 = F2.listFiles();
for (DocumentFile F4 : F3) {
recuisiveUri(F4);
}
} else {
if (Objects.requireNonNull(F2.getName()).endsWith(".epub"))
listUri.add(F2.getUri());
}
}
}
Anyone if you have better solution (faster) or found something wrong in my code, please help me, thank very a lot!