2012-05-25 34 views
0

我新來android應用程序開發。我試圖打開存儲在SD卡中的pdf文件。我使用了這段代碼片段,但這會在設備中打開查看器,但不會顯示路徑中給出的文件。任何幫助讚賞。如何使用安裝在android設備上的adobe閱讀器讀取存儲在sdcard中的pdf文件?

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("Environment.getExternalStorageDirectory().getPath()+/sample.pdf")); 
    intent.setType("application/pdf"); 
    intent.setPackage("com.adobe.reader"); selects the adobe reader directly 
    PackageManager pm = getPackageManager(); 
    List<ResolveInfo> activities = pm.queryIntentActivities(intent,0); 
    if (activities.size() >= 0) 
      { 
    startActivity(intent); 


    } else { 
    Toast.makeText(this, "No Application Available to View PDF", 

     Toast.LENGTH_SHORT).show(); 
    } 

回答

1

你可以從SD卡給予路徑讀取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, resource.getString(R.string.noapplicationpdf), Toast.LENGTH_LONG).show(); 
    } 
} 
+0

比你這幫了我。我沒有給出正確的路徑名稱。 – user1390225

+0

你可以給路徑找到如下的pdf文件。 String path = Environment.getExternalStorageDirectory()+「/ your_folder」。 –

+0

我的手機上有adobe acrobat,但沒有打開pdf文件 – user3582433

0

你可以嘗試像

Intent intent = new Intent(Intent.ACTION_VIEW); 
File file = new File(filename); 
intent.setDataAndType(Uri.fromFile(file), "application/pdf"); 
startActivity(intent); 

Intent intent = new Intent(); 
intent.setPackage("com.adobe.reader"); 
intent.setDataAndType(Uri.fromFile(file), "application/pdf"); 
startActivity(intent); 
相關問題