开发环境:Win10,1909; Android Studio 3.5.3 , 华为Mate10 Pro,Android10.0
1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.pdfviewershow"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:requestLegacyExternalStorage="true"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
android:requestLegacyExternalStorage="true",这句非常重要,参考https://www.cnblogs.com/endv/p/12164605.html
如果不加上,读取文件一直是Permission denied
2.添加PDFView
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
添加完,点sync
3.activity_main.xml,这里什么功能都没添加,很简单的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfview" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.barteksc.pdfviewer.PDFView> </LinearLayout>
4. MainActivity.java,这里也很简单,网上都能找的到
package com.example.pdfviewershow; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.widget.Toast; import com.github.barteksc.pdfviewer.PDFView; import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener; import java.io.File; public class MainActivity extends AppCompatActivity { private static final int REQUEST_EXTERNAL_STORAGE = 1; private PDFView pdfView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSDPath(); getPermission(); pdfView = findViewById(R.id.pdfview); // /storage/sdcard0/Documents/CMCC.pdf File fPdfPath = new File("/sdcard/Download/CMCC.pdf"); if(!fPdfPath.exists()) { System.out.println("FILE *****************************"); } pdfView.fromFile(fPdfPath) .enableSwipe(true) .swipeHorizontal(true) .enableDoubletap(false) .defaultPage(0) .onLoad(new OnLoadCompleteListener() { @Override public void loadComplete(int nbPages) { Toast.makeText(getBaseContext(),"loadComplete",Toast.LENGTH_LONG); } }) .enableAnnotationRendering(false) .password(null) .scrollHandle(null) .enableAntialiasing(true) .spacing(0) .load(); } public void getSDPath(){ File sdDir = null; File sdDir1 = null; File sdDir2 = null; boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 if (sdCardExist) { sdDir = Environment.getExternalStorageDirectory();//获取跟目录 sdDir1 = Environment.getDataDirectory(); sdDir2 =Environment.getRootDirectory(); } System.out.println("getExternalStorageDirectory(): "+sdDir.toString()); System.out.println("getDataDirectory(): "+sdDir1.toString()); System.out.println("getRootDirectory(): "+sdDir2.toString()); } private void getPermission() { //验证是否许可权限 // requestPermissions(new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE},0x123); requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0x456); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 0x123) { if (grantResults != null && grantResults[0] == PackageManager.PERMISSION_GRANTED ) { Toast.makeText(this,"已授权READ_EXTERNAL_STORAGE",Toast.LENGTH_LONG); } } if (requestCode == 0x456) { if (grantResults != null && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this,"已授权WRITE_EXTERNAL_STORAGE",Toast.LENGTH_LONG); } } } }
6,效果如下