I am aware that this question has been asked many times, but I still cannot get it to work. I have a pdf in the raw folder and I am trying to open it in a webview using "https://drive.google.com/viewerng/viewer?embedded=true&url=" to open it in the webview.
I think my problem is I can't get the correct path for the pdf file. I've tried it three different ways, and either the webview says I get an error (the url doesn't exist), or it "opens" the pdf, but the webview displays this text in the middle, "No preview available".
These are the three ways I've tried to get the path of the file.
- "android.resource://com.an.example/raw/filename"
- "android.resource://" + getPackageName() + "/" + R.raw.filename;
- Environment.getExternalStorageDirectory() + "/filename.pdf"
For methods 1 and 3 I have tried "filename" and "filename.pdf"
None of these are allowing the pdf to load in the webview. Below is the code I use to load the pdf. (Note: this method does work if I use a web url and not a local file)
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
String pdf = "one_of_the_above_methods";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
What am I doing wrong? Is there a better way to load a local pdf inside my app without having it take up the entire activity page?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…