- Note: Displaying PDF files in this way will increase the APK size by about 3-4m. It is recommended to use x5 WebView to load PDF files (extensible).
1. Add this dependency
Implementation ‘com. Making. Barteksc: android – PDF – viewer: 3.2.0 – beta. 1’
2. Brief introduction
This article is mainly to download PDF files to this SD directory, then converted to file, to show pdfView, the specific display OF PDF files can be viewed in pdfView source code
https://github.com/barteksc/AndroidPdfViewer
Copy the code
3. Start operations
public class PDF2Activity extends AppCompatActivity { private PDFView pdfView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_p_d_f2); pdfView = findViewById(R.id.pdfView); download("xxx.pdf"); } private void download(String url) { DownloadUtil.download(url, getCacheDir() + "/temp.pdf", new DownloadUtil.OnDownloadListener() { @Override public void onDownloadSuccess(final String path) { Log.d("MainActivity", "onDownloadSuccess: " + path); runOnUiThread(new Runnable() { @Override public void run() { preView(path); }}); } @Override public void onDownloading(int progress) { Log.d("MainActivity", "onDownloading: " + progress); } @Override public void onDownloadFailed(String msg) { Log.d("MainActivity", "onDownloadFailed: " + msg); }}); } private void preView(String path) { File file = new File(path); Pdfview.fromfile (file). EnableSwipe (true) // allows to block changing pages using swipe .swipeHorizontal(false) .enableDoubletap(true) .defaultPage(0) // allows to draw something on the current page, usually visible in the middle of the screen .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms) .password(null) .scrollHandle(null) .enableAntialiasing(true) // improve rendering a little bit on low-res screens // spacing between pages in dp. To define spacing color, set view background .spacing(0) .load(); }Copy the code
3.1 okhttp
Implementation (” com. Squareup. Okhttp3: okhttp: 4.6.0 “)
4.DownLoadUtils
public class DownloadUtil { public static void download(final String url, final String saveFile, final OnDownloadListener listener) { Request request = new Request.Builder().url(url).build(); new OkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { listener.onDownloadFailed(e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len; FileOutputStream fos = null; try { is = response.body().byteStream(); long total = response.body().contentLength(); File file = new File(saveFile); fos = new FileOutputStream(file); long sum = 0; while ((len = is.read(buf)) ! = -1) { fos.write(buf, 0, len); sum += len; Int progress = (int) (sum * 1.0f/total * 100); listener.onDownloading(progress); } fos.flush(); listener.onDownloadSuccess(file.getAbsolutePath()); } catch (Exception e) { listener.onDownloadFailed(e.getMessage()); } finally { try { if (is ! = null) is.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fos ! = null) fos.close(); } catch (IOException e) { e.printStackTrace(); }}}}); } public interface OnDownloadListener { void onDownloadSuccess(String path); void onDownloading(int progress); void onDownloadFailed(String msg); }}Copy the code