【问题标题】:Open PDF in android app在安卓应用中打开 PDF
【发布时间】:2011-08-30 02:40:15
【问题描述】:

我正在开发一个需要在设备中打开 pdf 文件的应用程序,

我实际上已经在网上获得了与大多数示例相似的代码。但是,问题是我无法打开文件,控件直接转到“异常”部分。

下面是代码:

public class MyPDFDemo extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)   
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button OpenPDF = (Button) findViewById(R.id.button);
    OpenPDF.setOnClickListener(new View.OnClickListener()
    { 
        public void onClick(View v) 
        {
            File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf"); 
            if(pdfFile.exists()) 
            {
                Uri path = Uri.fromFile(pdfFile); 
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }
            }

        }
    });

}

当我运行此代码时:我曾经看到“没有可用于查看 pdf 的应用程序”。谁能请我查看pdf文件。

【问题讨论】:

标签: android pdf


【解决方案1】:

由于您的 catch 块有 ActivityNotFoundException,这意味着您没有任何可以读取“应用程序/pdf”文件类型格式的活动/应用程序。从 Android Market 安装任何 pdf 查看器(Adobe 最近发布了他们的),或者使用上面提到的开源 pdf 查看器,您的问题很可能会得到解决。

http://code.google.com/p/apv/downloads/list

https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result

当您使用给定参数启动活动时,它会搜索所有注册为打开 pdf 格式的应用程序/活动/意图。由于您的设备中没有,您将收到 ActivityNotFoundException

【讨论】:

    【解决方案2】:

    您的代码是正确的,我也使用相同的代码在查看器中打开 pdf 文件。

    由于您的设备上没有安装查看器,因此没有任何查看器就无法打开。

    您可以为 Android 安装 Adobe reader

    我无法在模拟器中打开 pdf 文件,所以我必须使用我的设备进行测试。

    【讨论】:

      【解决方案3】:

      首先在设备上安装 pdf 阅读器。 而不是使用此代码从内存中读取 pdf 文件。

       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          final TextView tv = (TextView)findViewById(R.id.tv);
          Button bt=(Button)findViewById(R.id.openbtn);
          bt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
      
          File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.pdf");
          if(pdfFile.exists())
          {
          Uri path = Uri.fromFile(pdfFile);
          Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
          pdfIntent.setDataAndType(path, "application/pdf");
          pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      
          try{
              startActivity(pdfIntent);
          }catch(ActivityNotFoundException e){
              tv.setText("No Application available to view PDF");
          }
          }
          else
          {
              tv.setText("File not found");
          }
      
              }
          });
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多