最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Trouble scanning android emulator for files on maui - Stack Overflow

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论