Android 11下FileOpener打开PdfMake生成的PDF文件失败问题
问题:Android 11上PdfMake生成PDF后无法通过FileOpener打开,报错Activity not found
问题描述
使用PdfMake生成PDF文件,网页端功能正常,但在Android 11设备上调用FileOpener.open()时直接进入catch块,提示找不到处理PDF的Activity。
生成PDF的代码
if (platform != "web") { pdfFile.getBase64((data) => { try { Filesystem.writeFile({ path: nameFile, data: data, directory: Directory.Documents, //encoding: Encoding.UTF8, recursive: true, }).then(() => { Filesystem.getUri({ directory: Directory.Documents, path: nameFile, }).then( (getUriResult) => { const path = getUriResult.uri; console.log("path : " + path); FileOpener.open(path, "application/pdf") .then(() => console.log("File is opened")) .catch( (error) => console.log("Error openening file", error), (error) => console.dir(error, { depth: null }) ); }, (error) => { console.log(error); } ); }); } catch (error) { console.error("Unable to write file", error); } }); } else { pdfFile.download(`${nameFile}`); }
报错信息
{ status: 9, message: "Activity not found: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.company.appname.fileOpener2.provider/external/Documents/XX-1663759802589.pdf typ=application/pdf flg=0x3 }" }
AndroidManifest.xml配置
<?xml version="1.0" encoding="utf-8"?> <!-- Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" /> <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"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:name="com.company.appname.MainActivity" android:exported="true" android:label="@string/title_activity_main" android:theme="@style/AppTheme.NoActionBarLaunch" android:launchMode="singleTask" android:screenOrientation="userLandscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider> </application>
解决方案
1. 修正FileProvider Authority不匹配问题
报错中URI的Authority为com.company.appname.fileOpener2.provider,但你在Manifest中配置的FileProvider Authority是${applicationId}.fileprovider,两者不匹配导致系统无法识别该URI的合法来源。
修改AndroidManifest中的FileProvider配置,将authorities改为与报错中一致的值:
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.company.appname.fileOpener2.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider>
2. 确保file_paths.xml路径配置正确
在res/xml/file_paths.xml中添加对Documents目录的访问权限,确保FileProvider可以定位到目标文件:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <documents-path name="documents" path="."/> <external-files-path name="external_files" path="."/> <external-path name="external" path="."/> </paths>
3. 检查设备是否安装PDF查看应用
Android 11设备若未安装任何支持PDF格式的应用,也会触发该错误。可在代码中提前检查:
import { FileOpener } from '@capacitor/file-opener'; async function checkPdfViewerAvailability(path) { try { const result = await FileOpener.canOpen({ path: path, contentType: 'application/pdf' }); if (!result.value) { console.log('设备未安装PDF查看应用'); // 可在此处提示用户安装阅读器 } } catch (err) { console.error('检查PDF应用失败:', err); } }
4. 尝试切换文件存储目录(可选)
若上述方法无效,可将文件写入应用私有目录(如Directory.Data),规避Android 11作用域存储的限制:
Filesystem.writeFile({ path: nameFile, data: data, directory: Directory.Data, recursive: true, })
内容的提问来源于stack exchange,提问作者Péah
相关产品推荐
相关产品推荐

