Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android应用程序中打开PDF_Android_Pdf - Fatal编程技术网

在android应用程序中打开PDF

在android应用程序中打开PDF,android,pdf,Android,Pdf,我正在开发一个需要在设备中打开pdf文件的应用程序 实际上,我已经在web上获得了与大多数示例类似的代码。但是,问题是我无法打开文件,控件直接转到“异常”部分 代码如下: public class MyPDFDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState)

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

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

代码如下:

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文件。

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


当您使用给定参数启动“活动”时,它将搜索注册为打开pdf格式的所有应用程序/活动/意图。由于您的设备中没有任何文件,因此您将获得ActivityNotFoundException

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

因为您的设备上没有安装查看器,所以没有任何查看器就无法打开

你可以安装Android


我无法在emulator中打开pdf文件,因此我必须使用我的设备进行测试。

首先在设备上安装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");
    }

        }
    });

}

请检查您的设备是否安装了pdf查看应用程序。请参阅此链接:请参阅以下链接