Show PDF file in App

后端 未结 3 1391
日久生厌
日久生厌 2021-02-03 12:03

I found this two possibilities to show a pdf file.

  1. Open a webView with:

    webView.loadUrl(\"https://docs.google.com/gview?embedded=true&url=\"+

3条回答
  •  春和景丽
    2021-02-03 12:40

    Android provides PDF API now with which it is easy to present pdf content inside application.

    you can find details here

    Below is the sample snippet to render from a pdf file in assets folder.

        private void openRenderer(Context context) throws IOException {
        // In this sample, we read a PDF from the assets directory.
        File file = new File(context.getCacheDir(), FILENAME);
        if (!file.exists()) {
            // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
            // the cache directory.
            InputStream asset = context.getAssets().open(FILENAME);
            FileOutputStream output = new FileOutputStream(file);
            final byte[] buffer = new byte[1024];
            int size;
            while ((size = asset.read(buffer)) != -1) {
                output.write(buffer, 0, size);
            }
            asset.close();
            output.close();
        }
        mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        // This is the PdfRenderer we use to render the PDF.
        if (mFileDescriptor != null) {
            mPdfRenderer = new PdfRenderer(mFileDescriptor);
        }
    }
    

    update: This snippet is from google developers provided samples.

提交回复
热议问题