How to open pdf files in webview in pdfactivity?
BINGO!! My issue resolved can open pdf directly using BufferedInputStream
I created two activity, First Activity to load all pdfs in webview. When user clicks the link of pdf, the pdfurl
fetches the url as strings, passes strings to next activity.
PdfActivity.java
final String pdfurl = view.getHitTestResult().getExtra();
Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
intent.putExtra("PDFURL",pdfurl);
startActivity(intent);
Second Activity is PdfViewer, bateksc pdfviewer to load pdf from url, edited as above
PdfViewer.java
package ak.wp.meto.activity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ak.wp.meto.R;
public class PdfViewer extends Activity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
String value = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("PDFURL");
}
System.out.println("PRINT PDFVIEWER DATA" +value);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = value;
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}
}
Can not open PDF file using WebView
There some issue while loading pdf with webView. You can use the following code to solve your problem. This method takes the help of drive to view pdf in webView.Finally let me know if your problem solved or not
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
Load PDF file in webview
Android does not work like iOS in this respect. The WebView widget cannot display PDF documents on Android.
You will have to:
Use a third party library (unfortunately, most open source pdf libs are GPL)
Open a pdf viewer app via an Intent
Use Google docs in a webview
Open PDF in a WebView
(1) Google Docs Viewer, You can open it in android Browser like,
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+ webUrl);
Update:
(2) Check this library, in build.gradle(app module) add this dependency,
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
Load sdcard pdf files in Webview in android
Webview cant load local(sdcard) pdf file,
If you want to load sdcard pdf file you can use third party lib for it
https://github.com/barteksc/AndroidPdfViewer
or
You can use this method if you want
https://stackoverflow.com/a/43087727/4951663
Related Topics
Project Has No Default.Properties File! Edit the Project Properties to Set One
How to Pass a File Path Which Is in Assets Folder to File(String Path)
How to Put an Image in an Alertdialog? Android
Speed Control of Mediaplayer in Android
Multiple Count Down Timers in Recyclerview Flickering When Scrolled
Android: Backspace in Webview/Baseinputconnection
Notifications Fail to Display in Android Oreo (API 26)
Android Viewpager Setcurrentitem Not Working After Onresume
Android Background Service Is Restarting When Application Is Killed
Custom Converter for Retrofit 2
Display All Unicode Chars in Textview
Fragment-Fragment Communication in Android
Fragment Replacing in Recyclerview Item
How to Draw Outside the Bounds of an Android Canvas
Put a Progressbar on Actionbar
Missing Contentdescription Attribute on Image' in Xml
How to Add -Xlint:Unchecked to My Android Gradle Based Project