无法在按钮单击时打开pdf客户端

时间:2012-10-02 07:42:40

标签: android android-intent android-file

这是我的按钮点击代码:

    private View.OnClickListener onButton1=new View.OnClickListener() {
       public void onClick(View v){
        copyAssets();

        File rootDir = Environment.getExternalStorageDirectory();
        File file = new File(rootDir + "/sdcard/" +"save.pdf");
         if(file.exists())
           {
            // do action here

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
            intent.setType("application/pdf");

            startActivity(intent);
           }
         else {
            Toast.makeText(getApplicationContext(), "File does not exist",         Toast.LENGTH_LONG).show();
         }

       }
     };

copyAssets()是一个函数,通过它我可以将文件从我的资源文件夹复制到我的SD卡,它在我检查设备时正常工作,但是当我点击按钮时我无法打开它。我的文件在SD卡上命名为save.pdf

1 个答案:

答案 0 :(得分:1)

您可以通过以下路径阅读SDCard的pdf文件:

        File pdfFile = new File(path); 
        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(uractivity.this, "File does not exist", Toast.LENGTH_LONG).show(); 
            }
        }