2***@qq.com
2***@qq.com
  • 发布:2025-04-07 16:15
  • 更新:2025-07-08 16:18
  • 阅读:145

安卓11保存文件 拷贝文件路径问题

分类:HTML5+

报错提示:

保存到公共下载目录失败:targetSdkVersion设置>=29后在Android10 系统设备不支持当前路径。请更改为应用运行路径!

    plus.io.resolveLocalFileSystemURL(  
      filePath,  
      function (entry) {  
        const cachePath = plus.io.convertLocalFileSystemURL("_cache");  
        //安卓10+上无法保存到指定文件  
        console.log("_cache", entry.name, cachePath);  

        entry.copyTo(  
          cachePath,  
          entry.name,  
          function (newEntry) {  
            console.log("文件已保存到公共下载目录:" + newEntry.fullPath);  
          },  
          function (e) {  
            console.log("保存到公共下载目录失败:" + e.message);  
          }  
        );  
      },  
      function (e) {  
        console.log("解析文件路径失败:" + e.message);  
      }  
    );

外壳运行在自定义基座上,对了一下 cachePath 路里的包名没问题。但是还是报错了,为什么还是不能拷贝啊
为什么都使用的是私有应用路径了还是不行啊?应用运行路径是?

2025-04-07 16:15 负责人:无 分享
已邀请:
h***@qq.com

h***@qq.com

下面是测试成功的App.vue源码,包含了拷贝文件的功能,供参考:
<script>
export default {
onLaunch: async function() {
try {
await this.copyDatabase(); // 当doc目录下不存在数据库时,使用已有的库文件
} catch (e) {
console.log('Error: ', e)
}
},
onShow: function() {},
onHide: function() {},
methods: {
async copyDatabase() {
// 仅 Android/iOS 执行
// #ifdef APP-PLUS
const dbName = 'mira';

            // 检查是否已存在  
            const fileExists = await this.checkFileExists(dbName);  
            if (fileExists) return; // 如果已有库,则不进行复制  
            const docFolderEntry = await this.getDbFolder()  

            try {  
                // 从 static 复制到应用私有目录  
                const srcPath = `/static/db/amTool.db`;  

                plus.io.requestFileSystem(plus.io.PRIVATE_WWW, function(fs) {  
                    // fs.root是根目录操作对象DirectoryEntry  
                    fs.root.getFile(srcPath, {  
                        create: false  
                    }, function(fileEntry) {  
                        fileEntry.copyTo(docFolderEntry, dbName, function(entry) {  
                            console.log("数据库复制成功: " + entry.fullPath);  
                        }, function(e) {  
                            console.log("复制失败1: ", e);  
                        });  

                    });  
                });  

            } catch (e) {  
                console.error("复制失败2:", e);  
            }  
            // #endif  
        },  

        async checkFileExists(fileName) {  
            return new Promise((resolve) => {  
                plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {  
                    // fs.root是根目录操作对象DirectoryEntry  
                    fs.root.getFile(fileName, {  
                        create: false  
                    }, function(fileEntry) { // 文件存在  
                        resolve(true);  
                    }, function(e) { // 文件不存在  
                        resolve(false);  
                    });  
                });  
            });  
        },  

        async getDbFolder() { // 返回doc的应用私有目录对象,用于文件copy接口使用  
            return new Promise((resolve, reject) => {  
                plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {  
                    resolve(fs.root)  
                });  
            });  
        },  
    },  
}  

</script>

<style>
/每个页面公共css /
</style>

要回复问题请先登录注册