if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { String fileName = Environment.getExternalStorageDirectory() + "/cxy/" + "resume.pdf"; file = new File(fileName); // 目录不存在创建目录 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(response);
mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); // This is the PdfRenderer we use to render the PDF. if (mFileDescriptor != null) { mPdfRenderer = new PdfRenderer(mFileDescriptor); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void closeRenderer() throws IOException { if (null != mCurrentPage) { mCurrentPage.close(); } if (null != mPdfRenderer) mPdfRenderer.close(); if (null != mFileDescriptor) mFileDescriptor.close(); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void showPage(int index) { if (mPdfRenderer.getPageCount() <= index) { return; } // Make sure to close the current page before opening another one. if (null != mCurrentPage) { mCurrentPage.close(); } // Use `openPage` to open a specific page in PDF. mCurrentPage = mPdfRenderer.openPage(index); // Important: the destination bitmap must be ARGB (not RGB). Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(), Bitmap.Config.ARGB_8888); // Here, we render the page onto the Bitmap. // To render a portion of the page, use the second and third parameter. Pass nulls to get // the default result. // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter. mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); // We are ready to show the Bitmap to user. mImageView.setImageBitmap(bitmap); updateUi(); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void updateUi() { int index = mCurrentPage.getIndex(); int pageCount = mPdfRenderer.getPageCount(); mButtonPrevious.setEnabled(0 != index); mButtonNext.setEnabled(index + 1 < pageCount); if (pageCount <= 1) { ll_1.setVisibility(View.GONE); } else { ll_1.setVisibility(View.VISIBLE); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View view) { switch (view.getId()) { case R.id.previous: { // Move to the previous page showPage(mCurrentPage.getIndex() - 1); break; } case R.id.next: { // Move to the next page showPage(mCurrentPage.getIndex() + 1); break; } } } }