2017-05-25 90 views
1

我正嘗試從Xamarin Android中的URL下載並打開PDF文件。我可以打開URL沒有問題,這當然可以將PDF保存在Downloads文件夾中。有沒有一種方法可以訪問同一個文件,並在應用程序中下載後自動打開它?以下是我如何打開要下載的URL:在Xamarin Android應用程序中下載並打開PDF文件?

Intent i = new Intent(Intent.ActionView); 
Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse("http://sampleURL.com")); 

StartActivity(browserIntent); 

回答

0

您首先必須知道下載的文件的名稱。

然後,你可以回答這樣做

var filename = "file.pdf"; 
var file = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString(), filename); 
var uri = Android.Net.Uri.Parse(file); 

Intent intent = new Intent(Intent.ActionView); 
intent.SetFlags(ActivityFlags.ClearTop); 
intent.SetDataAndType(uri, "application/pdf"); 
try 
{ 
    StartActivity(intent); 
} 
catch (ActivityNotFoundException e) 
{ 
    Toast.MakeText(Application.Context, "Install a pdf viewer.", ToastLength.Long).Show(); 
} 
+0

謝謝!我使用默認的PDF查看器應用程序打開PDF,因爲我使用URL訪問該應用程序,但是當我們採用不同的路線時,這會派上用場。 – jcbrowni

相關問題