asp.net Xamarin Forms将apk文件从服务器移动到外部存储

chy5wohz  于 2023-03-31  发布在  .NET
关注(0)|答案(1)|浏览(141)

如果您可以提供帮助,请,我正在尝试将APK文件从服务器移动到外部存储。

我面对这个问题:Android.OS.FileUriExposedException:'file:///storage/emulated/0/Download/MyAppApks/add.txt通过Intent.getData()暴露在应用程序之外'

var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
                    var apkDirectory = new Java.IO.File(dir, "MyAppApks");
                    apkDirectory.Mkdirs();

                    var client = new HttpClient();
                    var apkUrl = "http://**.**.**.**:2030/com.companyname.app1.apk";
                    var response = await client.GetAsync(apkUrl);

                    var apkFile = new Java.IO.File(apkDirectory, "com.companyname.app1.apk");
                    using (var stream = new FileStream(apkFile.AbsolutePath, FileMode.Create, FileAccess.Write))
                    {
                        await response.Content.CopyToAsync(stream);
                    }

                    var intent = new Intent(Intent.ActionView);
                    intent.SetDataAndType(Android.Net.Uri.FromFile(apkFile), "application/vnd.android.package-archive");
                    intent.SetFlags(ActivityFlags.NewTask);
                    Android.App.Application.Context.StartActivity(intent);
6ie5vjzr

6ie5vjzr1#

试试这个:

using (var client = new WebClient())
    {

client.Headers.Add ("user-agent", "other");
        client.DownloadFile("http://**.**.**.**:2030/com.companyname.app1.apk", Android.OS.Environment.DirectoryDownloads + "/com.companyname.app1.apk");
    }
    
    string apkFile = Android.OS.Environment.DirectoryDownloads + "/com.companyname.app1.apk";
    
    var intent = new Intent(Intent.ActionView);
                        intent.SetDataAndType(Android.Net.Uri.FromFile(apkFile), "application/vnd.android.package-archive");
                        intent.SetFlags(ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(intent);

相关问题