英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论