2011-06-27 157 views
1

我在我的應用程序中有resource/raw/color_chart_ciao.pdf中的PDF文件。我想在我的應用程序中顯示該文件。我已經爲此編寫代碼:在android應用程序中顯示pdf

File file = new File("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"); 
     System.out.println("FIle Path is" + file); 
     if (file.exists()) { 
      System.out.println("FIle Path is" + file); 
      Uri path = Uri.fromFile(file); 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(path, "application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      try { 
       startActivity(intent); 
       System.out.println("pdf show"); 
      } 
      catch (ActivityNotFoundException e) { 
       Toast.makeText(CiaoView.this, 
        "No Application Available to View PDF", 
        Toast.LENGTH_SHORT).show(); 
      } 
     } 

但是當我運行我的應用程序時,我無法在我的應用程序中看到PDF。 我在應用程序開發中更新鮮。所以請幫助解決這個問題。

回答

0

,如果你有安裝在設備/仿真器的任何默認PDF閱讀器那麼只有你可以通過意圖打開PDF但你的應用程序將不會是一個單獨應用站。如果你想讓你的應用程序獨立,那麼你必須在你的應用程序中實現PDF閱讀器。有開源項目PDF讀你的,可以按照。

這裏是PDF幾個環節:

  1. apdf
  2. mupdf
+0

感謝ü非常 – user642347

0

- 複製您的活動下面的代碼。從你想要的地方調用函數CopyReadAssets(「File_name.pdf」)。將File_name.pdf文件放在資產文件夾中。它會工作!快樂編碼! :)

private void CopyReadAssets(String pdfname) 
{ 
AssetManager assetManager = getAssets(); 
InputStream in = null; 
OutputStream out = null; 
File file = new File(getFilesDir(), pdfname); 
try 
{ 
    in = assetManager.open(pdfname); 
    out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 
    copyFile(in, out); 
    in.close(); 
    in = null; 
    out.flush(); 
    out.close(); 
    out = null; 
} catch (Exception e) 
{ 
    Toast.makeText(getApplicationContext(), "Pdf Viewer not installed", Toast.LENGTH_SHORT).show(); 
} 
try 
{ 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(
     Uri.parse("file://" + getFilesDir() + "/"+pdfname), 
     "application/pdf"); 

startActivity(intent); 
}catch (Exception e) { 
    // TODO: handle exception 
    Toast.makeText(getApplicationContext(), "Pdf Viewer not installed" ,Toast.LENGTH_SHORT).show(); 
} 

}

相關問題