使用plus.io 在app进行图片文件读写处理,项目需要将cnavas绘画导出的图片保存到app里面,但是在使用uni的图片文件上传api进行测试读写逻辑的时候发现不管怎么样,在安卓模拟器,打开写入图片路径发现写入的图片都是已损坏。。。
页面使用代码
下面是权限配置
下面是代码
export async function getFile(tempFilePath) {
console.log(tempFilePath);
// readerFile2(tempFilePath)
const type = plus.io.PUBLIC_DOWNLOADS
const { fileName, fileData } = await readerFile2(tempFilePath)
const ada = await writeFile(type, fileName, fileData)
console.log(ada);
}
function readerFile(url) {
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(url, function (fs) {
console.log("文件对象获取成功");
console.log(fs.name);
console.log(fs);
fs.file((file) => {
console.log("文件file对象打开成功")
console.log(file);
let reader = new plus.io.FileReader();
reader.onloadend = function (e) {
console.log("Read success");
};
reader.onload = function (e) {
console.log("onload");
console.log(e);
resolve({ fileName: fs.name, fileData: e.target.result })
};
reader.onerror = function (e) {
console.log("Read error");
reject(e)
};
reader.readAsText(file);
}, (err3) => {
console.log("文件file对象打开失败")
console.log(err3);
})
}, (err1) => {
console.log("文件对象获取失败")
console.log(err1);
})
})
}
function writeFile(type, fileName, fileData) {
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(type,
(fs) => {
console.log("文件对象获取成功");
console.log(fs.name);
fs.root.getFile(fileName, { create: true },
(fileEntry) => {
console.log("文件对象打开成功")
console.log(fileEntry);
fileEntry.createWriter(
(writer) => {
console.log("文件写入成功")
console.log(writer);
writer.onwrite = (e) => {
console.log("文件写入成功")
console.log(e);
resolve(e)
}
writer.onerror = (e) => {
console.log("文件写入失败")
reject(e)
}
logger.log(fileData)
writer.write(fileData)
}
)
},
(err2) => {
console.log(err2);
}
)
},
(err1) => {
console.log("文件对象获取失败")
console.log(err1);
}
)
})
}