【发布时间】:2018-01-01 02:16:29
【问题描述】:
我在应用中有两个活动。一个有 3 个按钮,另一个有来自 github“com.github.barteksc:android-pdf-viewer:2.6.1”的 PDFView 我正在尝试从此代码中的 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);
}
}
}
这工作正常,但我必须从其他 url 加载另一个 pdf 来自活动 1,当我单击按钮 1 以在此活动中加载另一个 pdf 和按钮 2 从中加载另一个 pdf 时更改该 url“新 RetrievePDFStream”活动。
非常感谢任何答案!
【问题讨论】:
标签: java android-studio pdf url android-activity