- 发布:2020-12-11 18:51
- 更新:2023-02-23 20:20
- 阅读:3362
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: 2.9.11.20201121-alpha
HBuilderX类型: Alpha
HBuilderX版本号: 2.9.11
手机系统: 全部
手机厂商: 华为
页面类型: vue
打包方式: 云端
项目创建方式: HBuilderX
测试过的手机:
示例代码:
test() {
var pic =
"iVBORw0KGgoAAAANSUhEUgAAAG8AAABvAQAAAADKvqPNAAAACXBIWXMAAAsTAAALEwEAmpwYAAABbElEQVQ4jbXVsa3EIAwAUEcUdMkCSKxBx0phgeNugWQlOtZAYoGko0DxN9y/L/3GpPj/FOnyIqHYxiaAv37wh6wA+oVptmkF8CN61E+bloAn1iFXqUDiYZQL6g6XKC6jz3t8mLzH5G7QowLIGJX7DpIjgHKxXZ/0GVL5KliN5aewDOsKmv7oyUE3Iy6hzgYvmV+lvZclblZfEqaAGAUOSCGlKeaLEgl6TCkumUDqC3pxOOImM2JazScqjtUFmCLudGHGAfGkkCxFJbBFxbPOlK8B6pZNtt3nOYV8Bn3Y/DRtLU8XxGVhQeV7JVnS7lQfwRXx6kGypLXU3pQszJCHnAoNI2UtDtN2geeDyoLijPowLUieq6W+EliUL2M+JI1MKyP0ZmDZZmEK1OQt6xHbmbPHPr/vjmXZ5lemvnbMduZYgBZV8jdIDUBNuPXZH9Ih+EAHoMYR6ZykVa7Qi8akM2eJeqfZATHiv30mvgDDTWzT1ca62QAAAABJRU5ErkJggg==";
var path = plus.io.PRIVATE_DOC + new Date().getTime() + '.png';//这里存到doc目录
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
fs.root.getFile(path, {
create: true
}, function(entry) {
var fullPath = entry.fullPath;
if (plus.os.name == 'Android') {
var Base64 = plus.android.importClass("android.util.Base64");
var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
try {
var out = new FileOutputStream(fullPath);
var bytes = Base64.decode(pic, Base64.DEFAULT);
out.write(bytes);
out.close();
console.log("Android 成功", entry.toURL());
} catch (e) {
console.log(e.message);
}
} else if (plus.os.name == 'iOS') {
var NSData = plus.ios.importClass('NSData');
var nsData = new NSData();
nsData = nsData.initWithBase64EncodedStringoptions(pic, 0);
if (nsData) {
nsData.plusCallMethod({
writeToFile: fullPath,
atomically: true
});
plus.ios.deleteObject(nsData);
}
console.log("iOS 成功", entry.toURL());
}
})
})
console.log(plus.io.convertLocalFileSystemURL(path));//这里显示成www目录,而且www目录没有文件!!!
},
}
test() {
var pic =
"iVBORw0KGgoAAAANSUhEUgAAAG8AAABvAQAAAADKvqPNAAAACXBIWXMAAAsTAAALEwEAmpwYAAABbElEQVQ4jbXVsa3EIAwAUEcUdMkCSKxBx0phgeNugWQlOtZAYoGko0DxN9y/L/3GpPj/FOnyIqHYxiaAv37wh6wA+oVptmkF8CN61E+bloAn1iFXqUDiYZQL6g6XKC6jz3t8mLzH5G7QowLIGJX7DpIjgHKxXZ/0GVL5KliN5aewDOsKmv7oyUE3Iy6hzgYvmV+lvZclblZfEqaAGAUOSCGlKeaLEgl6TCkumUDqC3pxOOImM2JazScqjtUFmCLudGHGAfGkkCxFJbBFxbPOlK8B6pZNtt3nOYV8Bn3Y/DRtLU8XxGVhQeV7JVnS7lQfwRXx6kGypLXU3pQszJCHnAoNI2UtDtN2geeDyoLijPowLUieq6W+EliUL2M+JI1MKyP0ZmDZZmEK1OQt6xHbmbPHPr/vjmXZ5lemvnbMduZYgBZV8jdIDUBNuPXZH9Ih+EAHoMYR6ZykVa7Qi8akM2eJeqfZATHiv30mvgDDTWzT1ca62QAAAABJRU5ErkJggg==";
var path = plus.io.PRIVATE_DOC + new Date().getTime() + '.png';//这里存到doc目录
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
fs.root.getFile(path, {
create: true
}, function(entry) {
var fullPath = entry.fullPath;
if (plus.os.name == 'Android') {
var Base64 = plus.android.importClass("android.util.Base64");
var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
try {
var out = new FileOutputStream(fullPath);
var bytes = Base64.decode(pic, Base64.DEFAULT);
out.write(bytes);
out.close();
console.log("Android 成功", entry.toURL());
} catch (e) {
console.log(e.message);
}
} else if (plus.os.name == 'iOS') {
var NSData = plus.ios.importClass('NSData');
var nsData = new NSData();
nsData = nsData.initWithBase64EncodedStringoptions(pic, 0);
if (nsData) {
nsData.plusCallMethod({
writeToFile: fullPath,
atomically: true
});
plus.ios.deleteObject(nsData);
}
console.log("iOS 成功", entry.toURL());
}
})
})
console.log(plus.io.convertLocalFileSystemURL(path));//这里显示成www目录,而且www目录没有文件!!!
},
}
操作步骤:
把这个贴出来,一方面希望官方修复bug,一方面提醒后来者,不要在掉坑里了!
把这个贴出来,一方面希望官方修复bug,一方面提醒后来者,不要在掉坑里了!
预期结果:
我只能用以下代码强制把www结果变成doc下!实际上文件就在doc下!
var real_path = ('file://' + plus.io.convertLocalFileSystemURL(path) ).replace('/www/','/doc/');
我只能用以下代码强制把www结果变成doc下!实际上文件就在doc下!
var real_path = ('file://' + plus.io.convertLocalFileSystemURL(path) ).replace('/www/','/doc/');
实际结果:
18:46:51.322 /storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/www/1607683611517.png at pages/index/index.vue:56
18:46:51.510 Android 成功, _doc/1607683611517.png at pages/index/index.vue:36
18:46:51.322 /storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/www/1607683611517.png at pages/index/index.vue:56
18:46:51.510 Android 成功, _doc/1607683611517.png at pages/index/index.vue:36
bug描述:
安卓下必定发生!ios下偶发!
我要存一个base64的图片到手机本地,再分享。期间有bug总是分享文件为空,起初怀疑权限,研究了好几天,最终证明是h5+的bug!
以下这个convertLocalFileSystemURL 有时候返回_www,有时候返回_doc,比如文件在doc下,但是它返回在www下!
plus.io.convertLocalFileSystemURL(plus.io.PRIVATE_DOC)
还有种偶发情况,调试没错,但是云打包之后,真机上运行就出现分享文件为空,肯定也是这个convertLocalFileSystemURL的锅!