如何逐个加载pdf页面?

时间:2018-01-14 12:11:18

标签: java android performance pdf

在我的应用程序中,我想加载pdf文件并向用户显示,一些pdf文件超过20mb,页面很多。有时加载pdf需要很长时间。所以,我想知道是否有可能逐页加载页面,

像加载第一页一样,然后在加载第二页时显示给用户..

我正在使用这个库:

'com.github.barteksc:android-pdf-viewer:2.8.2


public class FlyerActivity extends AppCompatActivity {

    private PDFView mPDFview;

    private String mStoreId;

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

        mPDFview = (PDFView) findViewById(R.id.flyer_pdfView);
        showPdf(mStoreId);
    }

    private void showPdf(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();

                ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
                progressBar.setVisibility(View.VISIBLE);

                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();
            ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
            progressBar.setVisibility(View.GONE);

        }
    }

}

小尺寸pdf文件加载很好,但是大尺寸pdf,甚至5mb,10页需要一些时间来加载。

只加载一页也不起作用..看起来这样会加载完整的pdf文件,然后只显示一页。

mPDFview.fromStream(inputStream).pages(0).load();

0 个答案:

没有答案