2016-09-01 19 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は

がダウンロードへのフルパスを得るためにあなたのコードを変更し、「ダウンロード」を返しますようにフォルダ:以下はAndroidManifestで言及されているかどうかを確認また

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

。 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(); 
     } 
    } 
関連する問題