赞
踩
首先需要项目里面有pdf文件,或者手机本地有pdf文件,如果只是测试可以直接引入pdf文件到项目的assets目录下,如果是服务器上的pdf需要先下载到手机本地然后使用文件路径
废话不多说,上方法
1.引入build.gradle中引入需要的包
implementation 'com.github.chrisbanes:PhotoView:2.0.0'
2.声明获取权限
1)AndrooidManifest.xml中声明权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果是服务器文件需要下载,还需声明网络权限
<uses-permission android:name="android.permission.INTERNET" />
2)Android 6.0新特性,一些保护权限,除了要在AndroidManifest中声明权限,还需要在activity中使用代码动态获取权限
- if (Build.VERSION.SDK_INT >= 23) {
-
- String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.INTERNET};
- //验证是否许可权限
- for (String str : permissions) {
- if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
- //申请权限
- this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
- }
- }
- }
3.布局文件使用ViewPager
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <androidx.viewpager.widget.ViewPager
- android:id="@+id/vp_pdf"
- android:layout_width="match_parent"
- android:layout_height="match_parent"></androidx.viewpager.widget.ViewPager>
-
- </LinearLayout>
4.打开pdf文件读取
- public void init() {
- String FILE_NAME = getIntent().getStringExtra("path");//文件路径,根据项目需求修改
- final File file = new File(FILE_NAME);
-
- if (!file.exists()){
- Toast.makeText(PdfActivity.this,"文件不存在",Toast.LENGTH_SHORT).show();
- }
- mInflater = LayoutInflater.from(this);
- vpPdf =findViewById(R.id.vp_pdf);
-
- try {
- openRender();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- private void openRender() throws IOException {
-
- File file = new File(FILE_NAME);
- if (!file.exists()) {
- //复制文件到本地存储
- InputStream asset = getAssets().open(FILE_NAME);
- FileOutputStream fos = new FileOutputStream(file);
- byte[] buffer = new byte[1024];
-
- int size;
- while ((size = asset.read(buffer)) != -1) {
- fos.write(buffer, 0, size);
- }
-
- asset.close();
- fos.close();
- }
-
- //初始化PdfRender
- mDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
- if (mDescriptor != null) {
- mRenderer = new PdfRenderer(mDescriptor);
- }
-
- //初始化ViewPager的适配器并绑定
- MyAdapter adapter = new MyAdapter();
- vpPdf.setAdapter(adapter);
- }
-
- class MyAdapter extends PagerAdapter {
-
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- @Override
- public int getCount() {
- return mRenderer.getPageCount();
- }
-
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view==object;
- }
-
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- View view = mInflater.inflate(R.layout.item_pdf, null);
-
- PhotoView pvPdf = view.findViewById(R.id.iv_pdf);
- pvPdf.setEnabled(true);
-
- if (getCount() <= position) {
- return view;
- }
-
- PdfRenderer.Page currentPage = mRenderer.openPage(position);
- Bitmap bitmap = Bitmap.createBitmap(1080, 1760, Bitmap.Config
- .ARGB_8888);
- currentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
- pvPdf.setImageBitmap(bitmap);
- //关闭当前Page对象
- currentPage.close();
-
- container.addView(view);
- return view;
- }
-
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- //销毁需要销毁的视图
- container.removeView((View) object);
- }
- }
5.关闭pdf文件读取,一般activity销毁时需要使用
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- private void closeRenderer() throws IOException {
- if (mRenderer != null){
- mRenderer.close();
- }
- if (mDescriptor != null){
- mDescriptor.close();
- }
-
-
- }
以上就是pdf文件读取的全流程
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。