uni-app中uni.openDocument在安卓端使用报fail

uni-app中uni.openDocument在安卓端使用报fail

在小程序和h5都是没问题的
但是在安卓真机和模拟器 就一直进了uni.openDocument的fail
不管是使用官方的示例文档 还是下面这段代码 都是fail
我在手机放了一个pdf 手机可以直接打开这份pdf(通过 WPS-PDF查看器)
.xlsx 也不行
我的需求是:图2 用户选择了手机中的文件,然后可以查看预览(这时的文件 只是调用了uni.downloadFile的临时路径,并没有真正上传到服务器中),点击提交 才会上传到服务器

uni.downloadFile({  
  url: item.path || item.tempFilePaths,  
  header: {  
    Authorization: getAccessToken() ? 'Bearer ' + getAccessToken() : ''  
  },  
  success: function (res) {  
    console.log('1111', res)  
    uni.openDocument({  
      filePath: res.tempFilePath,  
      success: function (res) {  
        console.log('打开文档成功');  
      },  
      fail: function (err) {  
        uni.showToast({  
          title: '打开文档失败,请先安装支持该文档的应用',  
          icon: 'none',  
          duration: 2000  
        })  
        console.log('打开文档失败', err);  
      },  
    });  
  },  
  fail: function (err) {  
    console.log('2222', err);  
  },  
});


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

1 回复

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


uni-app 中使用 uni.openDocument 打开文档时,如果在安卓端报错,可能是由于以下几个原因导致的。你可以根据以下步骤进行排查和解决:

1. 文件路径问题

  • uni.openDocument 需要传入文件的本地路径或临时文件路径。如果你传入的是一个网络 URL,需要先通过 uni.downloadFile 下载文件,然后再使用下载后的临时文件路径。
  • 确保文件路径是正确的,并且文件确实存在。
uni.downloadFile({
    url: 'https://example.com/document.pdf',
    success: (res) => {
        if (res.statusCode === 200) {
            uni.openDocument({
                filePath: res.tempFilePath,
                fileType: 'pdf',
                success: function (res) {
                    console.log('打开文档成功');
                },
                fail: function (err) {
                    console.log('打开文档失败', err);
                }
            });
        }
    },
    fail: (err) => {
        console.log('下载文件失败', err);
    }
});

2. 文件类型不支持

  • uni.openDocument 支持的文档类型包括:doc, xls, ppt, pdf, txt 等。确保你传入的文件类型是支持的。
  • 如果文件类型不正确,可能会导致打开失败。
uni.openDocument({
    filePath: 'path/to/document.pdf',
    fileType: 'pdf', // 确保文件类型正确
    success: function (res) {
        console.log('打开文档成功');
    },
    fail: function (err) {
        console.log('打开文档失败', err);
    }
});

3. 权限问题

  • 在安卓设备上,应用需要具备读取存储的权限。如果权限不足,可能会导致打开文件失败。
  • 确保在 manifest.json 中配置了相应的权限:
{
    "app-plus": {
        "distribute": {
            "android": {
                "permissions": [
                    "<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"/>",
                    "<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"/>"
                ]
            }
        }
    }
}
  • 如果权限不足,可以在运行时动态请求权限:
uni.authorize({
    scope: 'scope.writePhotosAlbum',
    success() {
        // 权限获取成功
    },
    fail() {
        // 权限获取失败
    }
});

4. 文件路径格式问题

  • 在安卓设备上,文件路径需要使用正确的格式。确保路径是以 file:// 开头的绝对路径,或者是通过 uni.downloadFile 下载后的临时文件路径。
uni.openDocument({
    filePath: 'file:///storage/emulated/0/Download/document.pdf',
    fileType: 'pdf',
    success: function (res) {
        console.log('打开文档成功');
    },
    fail: function (err) {
        console.log('打开文档失败', err);
    }
});

5. 系统或第三方应用问题

  • uni.openDocument 依赖于系统或第三方应用来打开文档。如果系统中没有合适的应用来处理该文件类型,可能会导致打开失败。
  • 确保设备上安装了能够处理该文件类型的应用(如 PDF 阅读器、Word 等)。

6. 调试日志

  • 如果以上方法都无法解决问题,可以通过 console.log 打印更多调试信息,查看具体的错误原因。
uni.openDocument({
    filePath: 'path/to/document.pdf',
    fileType: 'pdf',
    success: function (res) {
        console.log('打开文档成功');
    },
    fail: function (err) {
        console.log('打开文档失败', err);
    }
});
回到顶部