Xamarin Forms将APK文件从服务器移动到外部存储。

huangapple go评论78阅读模式
英文:

Xamarin Forms move an apk file from a server to external storage

问题

如果您能提供帮助,请帮我将一个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);
英文:

If you can assist, please, I'm trying to move an APK file from a server to external storage.

i Faced this issue : Android.OS.FileUriExposedException: 'file:///storage/emulated/0/Download/MyAppApks/add.txt exposed beyond app through 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);

答案1

得分: 0

使用以下代码:

try this:

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

注意:代码部分没有翻译。

英文:

try this:

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

huangapple
  • 本文由 发表于 2023年3月31日 16:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定