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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With