Reputation: 11
I am doing android reader app on maui and stuck on auto finding fb2 and txt files. I've also pushed into storage png and jpg files and program finds it, but not fb2 and txt which are in the same directory. I've tried using MediaStore:
public static List<string> GetFilesFromMediaStore(string[] fileExtensions)
{
var result = new List<string>();
var uri = MediaStore.Files.GetContentUri("external");
var selection = string.Join(" OR ", fileExtensions.Select(ext => $"_data LIKE '%.{ext}'"));
string[] projection = {
MediaStore.Files.FileColumns.Data,
MediaStore.Files.FileColumns.DisplayName
};
using (var cursor = Android.App.Application.Context.ContentResolver.Query(uri, projection, selection, null, null))
{
if (cursor != null && cursor.MoveToFirst())
{
do
{
var filePath = cursor.GetString(cursor.GetColumnIndexOrThrow(MediaStore.Files.FileColumns.Data));
result.Add(filePath);
}
while (cursor.MoveToNext());
}
}
return result;
}
and tried java.io:
public static List<string> GetFilesJavaIO(string rootPath)
{
var result = new List<string>();
var rootDir = new Java.IO.File(rootPath);
if (!rootDir.Exists() || !rootDir.IsDirectory)
{
Debug.WriteLine($"Path is not a directory or doesn't exist: {rootPath}");
return result;
}
var filesAndDirs = rootDir.ListFiles();
if (filesAndDirs == null) return result;
foreach (var fileOrDir in filesAndDirs)
{
if (fileOrDir.IsDirectory)
{
// Рекурсивный вызов для подпапок
result.AddRange(GetFilesJavaIO(fileOrDir.AbsolutePath));
}
else if (fileOrDir.IsFile)
{
result.Add(fileOrDir.AbsolutePath);
}
}
return result;
}
but nothing seems to work correctly
Upvotes: 0
Views: 30