在Android应用程序中打开PDF [英] Open PDF in android app

查看:40
本文介绍了在Android应用程序中打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我实际上在网上找到了与大多数示例相似的代码.但是,问题是我无法打开文件并且控件直接转到异常"部分.

代码如下:

公共类 MyPDFDemo 扩展 Activity{/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);按钮 OpenPDF = (Button) findViewById(R.id.button);OpenPDF.setOnClickListener(new View.OnClickListener(){公共无效 onClick(查看 v){File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf");如果(pdfFile.exists()){Uri 路径 = Uri.fromFile(pdfFile);意图 pdfIntent = 新意图(Intent.ACTION_VIEW);pdfIntent.setDataAndType(path, "application/pdf");pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);尝试{开始活动(pdfIntent);}捕获(ActivityNotFoundException e){Toast.makeText(MyPDFDemo.this, "没有应用程序可用于查看 pdf", Toast.LENGTH_LONG).show();}}}});}

当我运行此代码时:我曾经看到没有可用于查看 pdf 的应用程序".任何人都可以请我查看pdf文件.

解决方案

由于您的 catch 块具有 ActivityNotFoundException,这意味着您没有任何可以读取application/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

I am working on an application where need to open the pdf file in the device,

I have actually got the code on the web that is similar to most of the examples. But, the thing is that I am not able to open the file and the control goes to the "Exception" part directly.

Here is the code below:

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(); 
                }
            }

        }
    });

}

When I run this code : i used to see " No Application available to view pdf ". Can anyone please me to view the pdf file.

解决方案

Since your catch block has ActivityNotFoundException, it means that you don't have any activity/application that can read a 'application/pdf' filetype format. Install any pdf viewer from the Android Market (Adobe recently release theirs), or else use the above mentioned open source pdf viewer and your problem will most probably will be solved.

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

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

When you start activity with your given params, it searches for all applications/Activities/Intents that are registered to open pdf format. Since you have none in your device, you are getting the ActivityNotFoundException

这篇关于在Android应用程序中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆