Show PDF file in App

I found this two possibilities to show a pdf file.

  • Open a webView with:

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

  • Open the pdf File with a extern App:

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(outFile),"application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent);

  • Both of them work. But my problem is that the pdf is for internal use only and in both examples the user could download it or save it in another folder.

    I know frameworks for this in iOS dev, I looking for a solution that works with Android.


    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.


    Many libraries are available for showing pdfs within your own app.

  • Android PDF Viewer
  • VuDroid
  • APDFViewer
  • droidreader
  • android-pdf
  • mupdf
  • android-pdfView
  • For a working example using android-pdfView , see this blog post. It demonstrates the basic usage of the library to display pdf onto the view with vertical and horizontal swipe.

    pdfView = (PDFView) findViewById(R.id.pdfView);
    pdfView.fromFile(new File("/storage/sdcard0/Download/pdf.pdf")).defaultPage(1).enableSwipe(true).onPageChange(this).load();
    

    You can show the PDF in your own viewer

    Tier are few open source pdf viewers you should look at: http://androiddeveloperspot.blogspot.com/2013/05/android-pdf-reader-open-source-code.html

    You can encrypt the pdf and make sure that your viewer only will be able to decrypt it.

    链接地址: http://www.djcxy.com/p/67508.html

    上一篇: 无法将图像保存在内部存储器android棒棒糖上

    下一篇: 在App中显示PDF文件