string localStoragePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string localFilename = Path.GetFileName(serverFileName);
string localFilePath = Path.Combine(localStoragePath, localFilename);
if (!System.IO.File.Exists(localFilePath))
{
WebClient webClient = new WebClient();
webClient.DownloadFile(serverFileName, localFilePath);
}
OpenFile(localFilePath);
});
公共無效的OpenFile(字符串文件路徑) {
var bytes = File.ReadAllBytes(filePath);
//Copy the private file's data to the EXTERNAL PUBLIC location
string externalStorageState = global::Android.OS.Environment.ExternalStorageState;
string application = "";
string extension = System.IO.Path.GetExtension(filePath);
switch (extension.ToLower())
{
case ".doc":
case ".docx":
application = "application/msword";
break;
case ".pdf":
application = "application/pdf";
break;
case ".xls":
case ".xlsx":
application = "application/vnd.ms-excel";
break;
case ".jpg":
case ".jpeg":
case ".png":
application = "image/jpeg";
break;
default:
application = "*/*";
break;
}
var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/report" + extension;
File.WriteAllBytes(externalPath, bytes);
Java.IO.File file = new Java.IO.File(externalPath);
file.SetReadable(true);
//Android.Net.Uri uri = Android.Net.Uri.Parse("file://" + filePath);
var uri = global::Android.Net.Uri.FromFile(file);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, application);
intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
try
{
Application.Context.StartActivity(intent);
}
catch (Exception)
{
Toast.MakeText(Application.Context, "No Application Available to View PDF", ToastLength.Short).Show();
}
}
請改爲內嵌代碼的圖像。人們可以將代碼複製粘貼到IDE中,並幫助您更快地修復它。 – matthewrdev
@matthewrdev請檢查。我需要你的建議來解決這個問題 –