uni-app uni.openDocument 安卓端报错

uni-app uni.openDocument 安卓端报错

操作步骤:

  • 调用uni.downloadFile下载文件之后用uni.openDocument 打开临时目录

预期结果:

  • 正常打开预览

实际结果:

  • 报错

bug描述:

下载文件后调用 uni.openDocument 报错,异常如下:

W/System.err: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject  
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)  
    at org.json.JSONObject.<init>(JSONObject.java:163)  
    at org.json.JSONObject.<init>(JSONObject.java:176)  
    at io.dcloud.feature.pdr.RuntimeFeatureImpl.execute(SourceFile:420)  
    at io.dcloud.common.adapter.util.MessageHandler$1.handleMessage(SourceFile:3)  
    at android.os.Handler.dispatchMessage(Handler.java:108)  
    at android.os.Looper.loop(Looper.java:166)  
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7529)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)  
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference  
W/System.err:     at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:605)  
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:579)  
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)  
    at io.dcloud.common.util.LoadAppUtils.getDataAndTypeIntent(SourceFile:6)  
    at io.dcloud.common.adapter.util.PlatformUtil.openFileBySystem(SourceFile:17)  
W/System.err:     at io.dcloud.feature.pdr.RuntimeFeatureImpl.execute(SourceFile:427)  
    at io.dcloud.common.adapter.util.MessageHandler$1.handleMessage(SourceFile:3)  
    at android.os.Handler.dispatchMessage(Handler.java:108)  
    at android.os.Looper.loop(Looper.java:166)  
    at android.app.ActivityThread.main(ActivityThread.java:7529)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)  

更多关于uni-app uni.openDocument 安卓端报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

可能是DCloud_FileProvider没配置 具体看下离线app文档https://nativesupport.dcloud.net.cn/AppDocs/FAQ/android

更多关于uni-app uni.openDocument 安卓端报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


解决了,没有替换${apk.applicationId}为自己的应用id

这是一个典型的Android文件路径权限配置问题。错误堆栈显示FileProvider.getUriForFile()方法抛出了空指针异常,原因是系统无法正确解析文件提供者(FileProvider)的配置。

核心问题: 在Android 7.0及以上版本,需要通过FileProvider来共享文件URI。你的应用可能缺少正确的FileProvider配置,或者配置的路径与uni.openDocument使用的临时文件路径不匹配。

解决方案:

  1. 检查AndroidManifest.xml配置 确保在manifestapplication节点下正确配置了FileProvider:
<provider
    android:name="io.dcloud.common.util.DCloudFileProvider"
    android:authorities="${apk.applicationId}.dc.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/dcloud_file_provider" />
</provider>
  1. 验证资源文件 检查res/xml/dcloud_file_provider.xml文件是否存在且内容正确:
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path name="root" path="." />
    <files-path name="files" path="." />
    <cache-path name="cache" path="." />
    <external-path name="external" path="." />
    <external-files-path name="external_files" path="." />
    <external-cache-path name="external_cache" path="." />
</paths>
  1. 检查代码调用方式 确保下载完成后正确获取文件路径:
uni.downloadFile({
    url: 'https://example.com/file.pdf',
    success: (res) => {
        if (res.statusCode === 200) {
            uni.openDocument({
                filePath: res.tempFilePath,
                success: function() {
                    console.log('打开文档成功');
                }
            });
        }
    }
});
  1. 临时解决方案 如果问题紧急,可以尝试将文件保存到应用外部存储:
// 下载后复制到外部存储
const savePath = plus.io.convertLocalFileSystemURL('_downloads/') + 'filename.pdf';
plus.io.resolveLocalFileSystemURL(res.tempFilePath, (entry) => {
    entry.copyTo(null, savePath, (newEntry) => {
        uni.openDocument({
            filePath: newEntry.fullPath
        });
    });
});
回到顶部