A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hi @Prathamesh Shende ,
Thanks for your question.
The reason count is always 0 on Android is because the folder returned by the FolderPicker is not a real file system path. On Android, folder access is provided through the Storage Access Framework (SAF), which gives your app a URI-based permission, not full file system access.
Because of this, APIs like Directory.EnumerateFiles will not work in this scenario and will return empty results.
I recommend these solutions and example codes:
- To help users avoid picking the folder again, you store and persist the URI permission.
var uri = Android.Net.Uri.Parse(folder.Folder.Path);
Platform.CurrentActivity.ContentResolver.TakePersistableUriPermission(
uri,
ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission
);
// Save uri.ToString() somewhere (Preferences / database)
- When the app restarts:
var savedUri = Android.Net.Uri.Parse(savedUriString);
This allows your app to access the same folder again without asking the user.
- Instead of
Directory.EnumerateFiles, useAndroid’s ContentResolver:
var childrenUri = DocumentsContract.BuildChildDocumentsUriUsingTree(
savedUri,
DocumentsContract.GetTreeDocumentId(savedUri)
);
var cursor = Platform.CurrentActivity.ContentResolver.Query(
childrenUri,
null,
null,
null,
null
);
while (cursor.MoveToNext())
{
var documentId = cursor.GetString(0);
var documentUri = DocumentsContract.BuildDocumentUriUsingTree(
savedUri,
documentId
);
}
However, I would not recommend doing this because once the app has permission to access the folder, it can view its contents, which poses a security risk. Instead, you pick files directly with FilePicker without reading files inside the folder picked.
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback