繁体   English   中英

下载文件到内部存储的某个路径{ C# Xamarin }

[英]Download Files into a certain path in the internal Storage { C# Xamarin }

目前我很好奇如何在您的 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);



            });

如果您使用的是 Xamarin forms 那么,这是我的解决方案。在您的 ZE84E30B9390CDB64DDB6888 项目上像这样制作一个 class。

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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM