3

I have generated a pdf using itext library and it gets stored in sdcard. I have to open the pdf in webview , but when I searched for the solution I came to know I can open it using online google doc service ,but my pdf is stored in a sdcard. Is there a way to open a pdf from external storage in a webview?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
user2699728
  • 377
  • 2
  • 8
  • 25

2 Answers2

3

To open pdf in Webview , it better to show pdf via google doc service,

WebView webView = (WebView) context.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf");

this may help you

Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
-1

No, you can`t use WebView to show a PDF file from a sdcard. Only HTML file maybe shown.

The most simple solution of this task is to use an external PDF reader. Such approach it is convenient for users enough because a "hand made" PDF reader you create from a github open source code can be slow for displaying large pdf files.

public class PDFReader {
public void read(Activity context,  String fileName){
    File appFolder = new File( Environment.getExternalStorageDirectory(), 
        context.getBaseContext().getPackageName() );
    File file = new File(appFolder, fileName);

    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(path, "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    try {
        context.startActivity(intent);
    } 
    catch (ActivityNotFoundException e) {
        Toast.makeText(context, "На устройстве не найдено доступного приложения для отображения PDF!", 
            Toast.LENGTH_SHORT).show();
    }
}

}
DenisMath
  • 547
  • 4
  • 8