Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show PDF file in App

Tags:

android

pdf

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="+uri);

  2. 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.

like image 916
Laire Avatar asked Aug 06 '14 21:08

Laire


People also ask

How do I get PDFs to open in app instead of browser?

Select the Programs tab. Click Manage Add-Ons and choose Acrobat Reader in the list of add-ons. Click Disable to ensure PDFs won't be opened in a browser.

How do I set an app to open PDF?

Right-click the PDF, choose Open With > Choose default program or another app in. 2. Choose Adobe Acrobat Reader DC or Adobe Acrobat DC in the list of programs, and then do one of the following: (Windows 10) Select Always use this app to open .


1 Answers

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.

like image 136
GvSharma Avatar answered Sep 23 '22 02:09

GvSharma