2016-09-01 60 views
1

我想從字節數組中生成PDF。但它提出了問題「訪問路徑被拒絕」。 我已經在清單文件中給予權限。我正在發送我的代碼塊,請檢查一下並提供一些建議和反饋,以便我可以完成它。System.UnauthorizedAccessException:拒絕訪問路徑「/ Download」。在xamarin android

private void ObjbtnViewSlip_Click(object sender, EventArgs e) 
    { 
     var result = objPaySlipViewModel.GetPaySlipByte(GlobalApplicationSession.EmployeeCode, GlobalApplicationSession.CompanyId, selectedPeriod); 
     if (File.Exists(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/temp.pdf") == false) 
     { 
      Directory.CreateDirectory(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/"); 
      File.Create(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/temp.pdf"); 
      System.IO.File.WriteAllBytes(Android.OS.Environment.DirectoryDownloads + "/RSI/PaySlip/", result); 
     } 
    } 
+0

請改爲內嵌代碼的圖像。人們可以將代碼複製粘貼到IDE中,並幫助您更快地修復它。 – matthewrdev

+0

@matthewrdev請檢查。我需要你的建議來解決這個問題 –

回答

2

首先,您提供的路徑不正確。 Android.OS.Environment.DirectoryDownloads只會返回「下載」

修改代碼的下載文件夾,以獲得完整路徑,如:

//The following will return the downloads folder path. 
string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads); 
string file = Path.Combine(directory, "/RSI/PaySlip/temp.pdf"); 

還應檢查以下已經AndroidManifest被提及。 xml文件將權限授予應用程序。

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
0
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(); 
     } 
    } 
相關問題