2011-06-27 51 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(); 
} 

}

相关问题