Tuesday, March 19, 2013

Android - open PDF after WebView downloaded it.

In the previous post, I handled WebView to download of PDF file from server to an Android device. To open the PDF by creating a new instant was not too difficult.
(p.s. This is not open within the WebView, I didn't see a single successful case on the web that is able to render the PDF within WebView; however, people do the tick by using google Doc Viewer, but this is not necessary for me)  

By adding a function in the Activity as below, and this will be called right after the download is completed.


public void displayPdf(String filename)
    {
        try
        {          
            File file = new file(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS ) + "/myPDF/" + filename);
           
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }
        catch (Exception e)
        {
            Log.i("TAG",e.getMessage());
        }
    }

   

No comments: