【问题标题】:Download Files into a certain path in the internal Storage { C# Xamarin }将文件下载到内部存储中的某个路径 { C# Xamarin }
【发布时间】:2021-04-21 10:15:38
【问题描述】:

目前我很好奇如何在您的 android 设备上下载文件并将文件保存到 INTERNAL STORAGE 上的某个路径。我想在最后得到的结果是:如果用户单击一个按钮,它将开始下载并替换定义的路径中的文件。

感谢任何帮助!

用我当前的代码,我尝试直接修改它,但没有成功......

祝大家有个美好的一天,感谢阅读!

*霜


            Renegade = new Command(async () =>
            {

                string pak5 = "";

                Stream stream1 = File.OpenRead(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "Android/data/com.epicgames.fortnite/files/InstalledBundles/FortniteBR/FortniteGame/Content/Paks/pakchunk10_s5-Android_ASTCClient.ucas");

                

                /*using (var streamWriter = new StreamWriter(pak5, true))
                {
                    streamWriter.WriteLine(DateTime.UtcNow);
                }

                using (var streamReader = new StreamReader(pak5))
                {
                    string content = streamReader.ReadToEnd();
                    System.Diagnostics.Debug.WriteLine(content);
                }*/

                StreamReader reader = new StreamReader(stream1);
                string pakspath = reader.ReadToEnd();

                //80000000
                //80000000

                //System.IO.File.Delete("/storage/emulated/0/Android/data/com.epicgames.fortnite/files/InstalledBundles/FortniteBR/FortniteGame/Content/Paks/pakchunk10_s5-Android_ASTCClient.ucas ");

                //Utilities.Convert(Body, Body1, pakspath, 80000000);
                //Utilities.Convert(Mat, Mat1, pakspath, 8981062);

                ReplaceBytes(pakspath, 8981045, S1);
                ReplaceBytes(pakspath, 8981045, S2);
                ReplaceBytes(pakspath, 80782548, S3);
                ReplaceBytes(pakspath, 80782548, S4);
                ReplaceBytes(pakspath, 80782571, S5);
                ReplaceBytes(pakspath, 80782571, S6);



            });

【问题讨论】:

  • 因为当前的代码与我的计划无关。就像我说的,我当前的代码是关于如何直接修改它而不是下载修改后的文件并替换它。它与直接问题无关,但是如果您有一些知识并且可以帮助我提供有关如何访问内部存储中文件的方法?
  • @Jason btw 我现在在我的问题中添加了代码,如果你能看一下 - 感谢你阅读它:)
  • 您甚至没有提及您尝试使用的路径。含糊不清的帖子。
  • @blackapps 阅读了代码,你看到了确切的路径吗?此外,它并不是真正需要的,因为它只是关于如何访问内部存储并在其上读/写......

标签: c# android xamarin android-download-manager


【解决方案1】:

如果您使用的是 Xamarin 表单,那么这是我的解决方案。在您的 安卓项目。

public class DroidFileHelper 
    {
        
        public string GetLocalFilePath(string filename)
        {
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            return Path.Combine(path, filename);
        }

        public async Task SaveFileToDefaultLocation(string fileName, byte[] bytes, bool showFile = false)
        {
            Context currentContext = Android.App.Application.Context;
            string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            string file = Path.Combine(directory, fileName);
            System.IO.File.WriteAllBytes(file, bytes);

            //If you want to open up the file after download the use below code
            if (showFile)
            {
               
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.N)
                {

                    string externalStorageState = global::Android.OS.Environment.ExternalStorageState;
                    var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/" + global::Android.OS.Environment.DirectoryDownloads + "/" + fileName;
                    File.WriteAllBytes(externalPath, bytes);

                    Java.IO.File files = new Java.IO.File(externalPath);
                    files.SetReadable(true);

                    string application = "application/pdf";
                    Intent intent = new Intent(Intent.ActionView);
                    Android.Net.Uri uri = FileProvider.GetUriForFile(currentContext, "com.companyname.appname.provider", files);
                    intent.SetDataAndType(uri, application);
                    intent.SetFlags(ActivityFlags.GrantReadUriPermission);
                    Forms.Context.StartActivity(intent);
                }
                else
                {
                    Intent promptInstall = new Intent(Intent.ActionView);
                    promptInstall.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File(file)), "application/pdf");
                    promptInstall.SetFlags(ActivityFlags.NewTask);
                    Forms.Context.StartActivity(promptInstall);
                }
            }
        }
    }

之后就像从您的 xamarin 表单中一样调用它。

Xamarin.Forms.DependencyService.Get<IFileHelper>().SaveFileToDefaultLocation(oFile.FileName, oFile.FileInBytes, true);

它将您的文件保存到下载,因为我们在我们的机器人助手类中将路径设置为Android.OS.Environment.DirectoryDownloads

【讨论】:

  • 感谢您的回复!我添加了该课程并且效果很好,但是因为我是 Xamarin 的新手;以我的形式调用它是什么意思?我不知道我是否做对了 - 我在按钮所在的主 xamarin 文件的按钮的命令部分中添加了它。对不起这个愚蠢的问题
  • 是的,你是对的。你必须添加 Xamarin.Forms.DependencyService.Get().SaveFileToDefaultLocation(oFile.FileName, oFile.FileInBytes, true);从您的按钮命令
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 2017-07-12
  • 2020-08-18
  • 1970-01-01
相关资源
最近更新 更多