Opening PDFs with Google Docs is a bad idea in terms of user experience. It is really slow and indifferent.
Solution after API 21
Starting with api 21, we have a PdfRenderer that helps convert PDF to bitmap. I have never used this, but it seems easy enough.
Solution for any level of API
Another solution is to download the PDF file and transfer it using Intent to a special PDF application that will display it. A quick and enjoyable user experience, especially if this feature is not central to your application.
Use this code to download and open PDF
public class PdfOpenHelper { public static void openPdfFromUrl(final String pdfUrl, final Activity activity){ Observable.fromCallable(new Callable<File>() { @Override public File call() throws Exception { try{ URL url = new URL(pdfUrl); URLConnection connection = url.openConnection(); connection.connect();
}
For Intent to work, you need to create a FileProvider to grant permission to the receiving application to open the file.
Here's how you implement it: in your manifest:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
Finally, create file_paths.xml in the resource folder
<?xml version="1.0" encoding="utf-8"?> <paths> <files-path name="shared_pdf" path="shared_pdf"/> </paths>
Hope this helps =)
JDenais Aug 09 '17 at 15:25 2017-08-09 15:25
source share