2017-07-26 39 views
0

我有兩個活動的應用程序。一個有3個按鈕,其他有PDFView從github「com.github.barteksc:android-pdf-viewer:2.6.1」我試圖從這個代碼的URL加載PDF。它爲我工作解析URL到其他Activity從該URL加載PDF

public class PDFActivity extends AppCompatActivity { 

    PDFView pdfView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pdf); 

     pdfView = (PDFView) findViewById(R.id.pdfview); 

     new RetrievePDFStream().execute("my url to load pdf example http://sample.com/xyz.pdf"); 

    } 


    class RetrievePDFStream extends AsyncTask<String, Void, InputStream> 
    { 

     @Override 
     protected InputStream doInBackground(String... strings) { 
      InputStream inputStream = null; 
      try 
      { 
       URL url = new URL(strings[0]); 
       HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 
       if(urlConnection.getResponseCode() == 200) 
       { 
        inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
       } 
      } 
      catch (IOException e) 
      { 
       return null; 
      } 
      return inputStream; 
     } 

     @Override 
     protected void onPostExecute(InputStream inputStream) { 
      pdfView.fromStream(inputStream); 
     } 
    } 
} 

這工作得很好,但我必須從活動的1來自其他網址加載另一個PDF格式,更改URL「新RetrievePDFStream」當我點擊按鈕1載入本次活動的PDF和按鈕2從此活動加載另一個pdf。

任何答案非常感謝!

回答

1

如果我理解正確,你有3個按鈕和兩個actvities。
在ack1中有3個按鈕,在ack2中有pdf查看器。所以,如果我點擊ack1 button1你想從ack2加載url1,如果我點擊ack1 button2你想加載url2形式ack2等.... 如果這是要求,那麼你可以使用意向演員。 形式ACK1做到這一點:

button1.onclick(new onclicklistener{ 
    publi void onClick(){ 
     Intent intent=new Intent(this,ack2.class); 
     intent.putExtras("url","pdf url1"); 
    }} 

爲BUTTON2 JST改變 「PDF URL1」 到 「PDF URL2」
爲BUTTON3 JST改變 「PDF URL1」 到 「PDF URL3」

在ACK2

做這樣的: 在onCreate()方法:

Intent i=this.getIntent(); 
    String url=i.getExtras("url"); 

從這裏,你會得到你的URL中的第一個活動通過基於你單擊該按鈕。如果用戶點擊了按鈕1,您將得到「pdf url1」,如果他登錄button2,您將獲得「pdf url2」..等等...

編輯:代碼僅供參考不要複製&粘貼它..

+0

謝謝Ajay_Reddy,它的作品很有魅力 –

+0

不客氣。如果它有效,就投票回答。發佈問題前請徹底搜索。我想這已經被問到了。 –