我希望在从web加载pdf文件时显示进度条(某些pdf文件大于15mb)我该怎么做?
iam使用com.github.barteksc.pdfviewer.PDFView
来显示来自网络的pdf
这是我的pdf节目活动
private PDFView mPDFview;
private String mStoreId;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flyer);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mReference = mFirebaseDatabase.getReference("Store");
if (getIntent()!=null)
{
mStoreId = getIntent().getStringExtra("StoreId");
}
if (!mStoreId.isEmpty())
{
getUrlStoreFlyer(mStoreId);
}
}
private void getUrlStoreFlyer(String storeId) {
mReference.child(storeId).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Store storeChain = dataSnapshot.getValue(Store.class);
String pdfUrl = storeChain.getFlyerPDF().toString();
mPDFview = findViewById(R.id.flyer_pdfView);
new RetrievePDFStream().execute(pdfUrl);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
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) {
mPDFview.fromStream(inputStream).load();
}
}
@Override
public void onBackPressed() {
finish();
overridePendingTransition(0, 0);
}
}
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.FlyerActivity">
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp"
android:id="@+id/progressBar2" />
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/flyer_pdfView"/>
enter code here
</RelativeLayout>
答案 0 :(得分:1)
这是我使用和完美的工作:
@Override
protected void onPostExecute(InputStream inputStream) {
mPDFview.fromStream(inputStream).onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
progressBar.setVisibility(View.GONE);
}
}).load();
}
答案 1 :(得分:0)
private PDFView mPDFview;
private String mStoreId;
//add progressbar
private ProgressBar progressBar;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flyer);
//initialize
progressBar = (ProgressBar) findViewById(R.id.progressBar);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mReference = mFirebaseDatabase.getReference("Store");
if (getIntent()!=null)
{
mStoreId = getIntent().getStringExtra("StoreId");
}
if (!mStoreId.isEmpty())
{
getUrlStoreFlyer(mStoreId);
}
}
private void getUrlStoreFlyer(String storeId) {
mReference.child(storeId).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Store storeChain = dataSnapshot.getValue(Store.class);
String pdfUrl = storeChain.getFlyerPDF().toString();
mPDFview = findViewById(R.id.flyer_pdfView);
new RetrievePDFStream().execute(pdfUrl);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
class RetrievePDFStream extends AsyncTask<String, Void, InputStream>
{
@Override
protected void onPExecute(InputStream inputStream) {
progressBar.setVisibility(View.VISIBLE);
}
@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) {
mPDFview.fromStream(inputStream).load();
progressBar.setVisibility(View.GONE);
}
}
@Override
public void onBackPressed() {
finish();
overridePendingTransition(0, 0);
progressBar.setVisibility(View.GONE);
}
}
答案 2 :(得分:0)
由于您正在使用库从输入流加载PDF,因此可以采用以下方式。
在执行异步任务之前,使进度条可见,PDFView不可见。
progressBar = findViewById(R.id.progressBar2);
mPDFview = findViewById(R.id.flyer_pdfView);
progressBar.setVisibility(View.VISIBLE);
mPDFview.setVisibility(View.INVISIBLE);
new RetrievePDFStream().execute(pdfUrl);
在AsyncTask的onPostExecute()
中,您可以在网络调用完成后执行UI操作。所以在这里,您必须使PDFView可见并且progressBar不可见,但是由于您正在进行网络操作然后将其传输到PDFView,因此PDFView也需要时间来流式传输内容。我查找了你正在使用的库,我相信它有一个名为onLoad(onLoadCompleteListener)
的函数,你可以使用这个函数和fromStream()
来使进度条不可见,pdfView可见。
@Override
protected void onPostExecute(InputStream inputStream) {
mPDFview.fromStream(inputStream).onLoad(this).load();
}
@Override
public void loadComplete(int nbPages) {
progressBar.setVisibilitiy(INVISIBLE);
mPDFView.setVisibility(VISIBLE);
}
或者,由于您使用的库实际上可以从URI加载PDF,因此您根本不需要使用Async Task。
只需致电PDFView.fromUri(uri).onLoad(this).load();