2015-05-08 83 views
1

我正在嘗試創建一個應用程序,讓您查看存儲的PDF,就像簡單的文件閱讀器一樣。在Android應用程序中查看本地PDF文件

我正在使用導航抽屜項目基地,我似乎無法打開PDF。

我在assets /中存儲了一個測試PDF,而且我也嘗試過raw /。如果我嘗試使用資產/每當我嘗試在設備上打開PDF時崩潰,說「不能顯示PDF(test.pdf無法打開)。」

我已經嘗試了幾種方法來嘗試獲取此工作但都沒有盛行,這裏是我的代碼截至目前:。

public void onSectionAttached(int number){ 
    switch (number) { 
     case 1: 
      mTitle = getString(R.string.title_song); 

      File pdfFile = new File("/assets/test.pdf"); 

      Uri path = Uri.fromFile(pdfFile); 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(path, "application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      try 
      { 
       startActivity(intent); 
      } 
      catch(ActivityNotFoundException e) 
      { 
       Toast.makeText(MainActivity.this, "It didn't crash!", Toast.LENGTH_LONG).show(); 
      } 



      break; 

     case 2: 
      mTitle = getString(R.string.title_artist); 
      break; 
    } 


} 
+0

[Display PDF內的應用程序在Android?](http://stackoverflow.com/questions/2456344/display-pdf-within-app-on-android) –

回答

1

你開始的意圖,查看文件的目的是推出另一個應用程序它沒有權限查看您的資產,甚至是你的私人文件,將它複製到某個地方,它有權先讀取文件,或者你需要實現一個ContentProvider併爲它們提供文件訪問權限。

+0

更不用說,資產不是在Android文件系統上的文件,所以'新文件(「/ assets/test.pdf」)不起作用。 – CommonsWare

相關問題