/**
* android获取一个用于打开PDF文件的intent
*
* @param path 要打开的文件的绝对路径
* @return
*/
public Intent getPdfFileIntent(String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);//Intent.ACTION_VIEW = "android.intent.action.VIEW"
intent.addCategory(Intent.CATEGORY_DEFAULT);//Intent.CATEGORY_DEFAULT = "android.intent.category.DEFAULT"
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(MainActivity.this, "pdf.test.jxl.base64topdf.fileProvider", new File(path));
} else {
uri = Uri.fromFile(new File(path));
}
intent.setDataAndType(uri, "application/pdf");
return intent;
}
网上找了很多的第三方应用打开pdf文件,但是都没有反应;
忽略了android 7.0的一个权限问题,7.0后打开文件或者路径需要使用FileProvider,
首先在中加入
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="pdf.test.jxl.base64topdf.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
注意;如果FileProvider找不到那么,在build.gride中加入
implementation 'com.android.support:support-v4:26.1.0'
然后再res下面创建xml文件夹,再创建file_paths.xml,
<?xml version="1.0" encoding="utf-8"?>
<paths>
//代表的目录即为:Environment.getExternalStorageDirectory()/Android/data/包名/
<external-path
name="files_root"
path="Android/data/包名/" />
//代表的目录即为:Environment.getExternalStorageDirectory()
<external-path
name="external_storage_root"
path="." />
//代表的目录即为:Environment.getExternalStorageDirectory()/pics
<external-path
name="external"
path="pics" />
</paths>