2014-04-04 52 views
-1

如何從原始文件夾打開PDF文件? 這種訪問代碼的PDF文件從SD卡文件..從原始文件夾打開PDF文件?

File file = new File(getFilesDir(), "teen_ebook.pdf"); 

    try { 

     if (file.exists()) { 

      Uri path = Uri.fromFile(file); 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(path, "application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 
      Log.e("IR", "No exception"); 

     } 
     else { 
      Toast.makeText(PdfActivity.this, "File NotFound",Toast.LENGTH_SHORT).show(); 
     } 
    } 
    catch (ActivityNotFoundException e) { 

    Toast.makeText(PdfActivity.this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show(); 
    } 

但我想訪問的PDF文件從原文件夾? 任何一個在這裏。請給我建議。

+1

的(http://stackoverflow.com/questions/19865276/best-way-to-include-pdfs-in-an-app) – CommonsWare

+0

[以包括在應用中的PDF文件的最佳方式]可能重複請告訴我以其他方式訪問PDF文件。 –

回答

1

您不能直接從原始文件夾打開PDF文件。它需要PDFViewer應用程序打開PDF文件。

你將不得不復制你的PDF文件到SD卡,然後你可以打開它來閱讀它。

public void openPDF(){ 

    copyFile(getResources().openRawResource(R.raw.hello_world); 
     , new FileOutputStream(new File(getFilesDir(), "yourPath/teen_ebook.pdf"))); 

     File pdfFile = new File(getFilesDir(), "yourPath/teen_ebook.pdf"); 
     Uri path = Uri.fromFile(pdfFile); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.setDataAndType(path, "application/pdf"); 
     startActivity(intent); 
    } 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
    }