用Adobe Reader显示PDF文件

问题描述:

我创建了一个类openPDF,它将一个字节数组作为输入并用Adobe Reader显示PDF文件。代码:用Adobe Reader显示PDF文件

private void openPDF(byte[] PDFByteArray) { 


    try { 
     // create temp file that will hold byte array 
     File tempPDF = File.createTempFile("temp", ".pdf", getCacheDir()); 
     tempPDF.deleteOnExit(); 

     FileOutputStream fos = new FileOutputStream(tempPDF); 
     fos.write(PDFByteArray); 
     fos.close(); 

     Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      Uri uri = Uri.fromFile(tempPDF); 
      intent.setDataAndType(uri, "application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  

      startActivity(intent); 


    } catch (IOException ex) { 
     String s = ex.toString(); 
     ex.printStackTrace(); 
    } 
} 

当我经过打算,从Adobe Reader的错误是“无效的文件路径”。我阅读所有其他帖子有关下载和在Android中查看PDF,但力量帮助很大。有什么建议么?

+0

您是否尝试过将非临时文件作为输入到adobe应用程序? – 2012-09-26 11:37:08

我做了这个代码来打开现有Dowloads文件夹与Adobe公司的应用程序中的especific .pdf文件

File folder = new File(Environment.getExternalStorageDirectory(), "Download"); 
    File pdf = new File(folder, "Test.pdf"); 

    Uri uri = Uri.fromFile(pdf); 

    PackageManager pm = getPackageManager(); 
    Intent intent = pm.getLaunchIntentForPackage("com.adobe.reader"); 
    intent.setDataAndType(uri, "application/pdf"); 
    startActivity(intent); 

这对我的作品。所以我想你的问题可以是临时文件。尝试将文件写入SD卡。为此,您需要将android.permission.WRITE_EXTERNAL_STORAGE添加到您的AndroidManifest.xml中。

+1

工作完美..谢谢! – desidigitalnomad 2013-02-04 05:58:55

我认为问题在于其他应用程序无法访问应用程序专用数据区中的文件(如缓存目录)。

候选方案:

  1. 改变文件的模式MODE_WORLD_READABLE,以便它可以通过其他应用

    ... 
    String fn = "temp.pdf"; 
    Context c = v.getContext(); 
    FileOutputStream fos = null; 
    try { 
        fos = c.openFileOutput(fn, Context.MODE_WORLD_READABLE); 
        fos.write(PDFByteArray); 
    } catch (FileNotFoundException e) { 
        // do something 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    } finally { 
        if (fos!=null) { 
         try { 
          fos.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
    } 
    
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    String filename = c.getFilesDir() + File.separator + fn; 
    File file = new File(filename); 
    Uri uri = Uri.fromFile(file); 
    intent.setDataAndType(uri, "application/pdf"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    startActivity(intent); 
    ... 
    
  2. 或写入PDF文件到/ SD卡分区读取。

    您可以使用android.os.Environment API来获取路径,并记得将权限添加到您的应用的AndroidManifest.xml文件。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

问候

紫藤陈