简体   繁体   中英

Show progress bar while loading pdf file

iam fairly new to android development, so what iam trying to make is app that can show pdf from url,

I want to show progress bar while pdf file is loading from web(some pdf files are big over 15mb) How can i do that?

iam using com.github.barteksc.pdfviewer.PDFView to show pdf from web

here is my pdf show activity

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);
}
}

Here is my 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>

Here is what i used and worked perfect:

   @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();

    }
  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);

}
}

Since you are using a library to load PDF from input stream, here is how it can be done.

  1. Just before you execute the Async Task make the progress bar visible and PDFView invisible.

    progressBar = findViewById(R.id.progressBar2); mPDFview = findViewById(R.id.flyer_pdfView); progressBar.setVisibility(View.VISIBLE); mPDFview.setVisibility(View.INVISIBLE); new RetrievePDFStream().execute(pdfUrl);

  2. In onPostExecute() of the AsyncTask is where you do the UI operation after the Network call is complete. So here, you ll have to make the PDFView visible and progressBar invisible, but also since you are making a network operation and then streaming that to PDFView, PDFView will also take time to stream the content. I looked up the library that you are using and I believe it has a function called onLoad(onLoadCompleteListener) , you can use this function along with the fromStream() to make the progress bar invisible and pdfView visible.

    @Override protected void onPostExecute(InputStream inputStream) { mPDFview.fromStream(inputStream).onLoad(this).load(); }

    @Override public void loadComplete(int nbPages) { progressBar.setVisibilitiy(INVISIBLE); mPDFView.setVisibility(VISIBLE); }

Alternatively, Since you are using a library that can actually load PDF from an URI,you need not use Async Task at all. Just call PDFView.fromUri(uri).onLoad(this).load();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM