Try this code
This worked for me.
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = "https://www.rkiinstruments.com/pdf/71-0136RK.pdf";
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();
}
}
}
add library in build.gradle
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
create activity_custom_pdf.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".activity.PdfViewer">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/txtPdf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</RelativeLayout>