2016-07-07 19 views
0

我已經在這個主題上搜索了很多,但我無法找到正確的答案。我懇請不要在沒有回答的情況下重複標記它。在我的android應用程序中打開PDF文件,而無需使用任何瀏覽器或第三方應用程序

我在我的android應用程序中有兩個活動。

1]。點擊任何文件時,服務器上可用的PDF文件列表開始下載並顯示進度條,直到下載完成。 下載完成後彈出兩個按鈕[查看]或[取消]打開。 點擊查看打開第二個活動傳遞PDF文件路徑作爲參數。

2]。在第二個活動中,我想打開僅在應用程序活動中通過意向傳遞的PDF文件。

我不想使用任何瀏覽器,驅動器,文件管理器或任何外部android應用程序。

我該如何做到這一點,請爲我提供適當的解決方案。 預先感謝您:)

+0

你檢查PDF閱讀器庫? https://github.com/barteksc/AndroidPdfViewer – Youngjae

+0

使用意圖打開該PDF文件並傳遞該文件的意圖如下,希望這會幫助你解決你的問題,它適用於我.... 私人void viewPdf(File myFile){Intent.ACTION_VIEW}; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(myFile),「application/pdf」); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); } 此代碼將在默認PDF文件瀏覽器中打開PDF文件,或列出各種PDF查看器。 – Bhavnik

+0

你有沒有嘗試/應用我更新的答案。 – user392117

回答

1

將文件名從FirstActivity發送到SecondActivity。

Intent i=new Intent(FirstActivity.this, SecondActivity.class); 
i.putExtra("file", fileUrl); 
startActivity(i); 
在SeconActivity

,使用WebView

編輯

Intent i = getIntent(); 
    String myPdfUrl = i.getStringExtra("file"); 
    String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl; 
    String doc="<iframe src='"+url+"' width='100%' height='100%' style='border: none;'></iframe>"; 
    WebView web=(WebView)findViewById(R.id.webViewpdf); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadData(doc, "text/html", "UTF-8"); 

layout.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <WebView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/webViewpdf"/> 

</LinearLayout> 
+0

謝謝@userJP,但是這個代碼提供了在Drive或Browser(Chrome,Firefox等)中打開PDF文件的選項。我只想在SecondActivity內打開。 – jil123

+0

看到我編輯的答案@ jil123 – user392117

+0

謝謝userJP @Youngjae我的概念需要你的答案。 – jil123

相關問題