void plus.io.resolveLocalFileSystemURL( url, succesCB, errorCB );
这东西是异步的???
plus.gallery.pick(function(files) {
console.log('photo.js 110 files ' + files)
//临时图片数组
var tempFiles = new Array();
//原始路径
var **originalPath** = new Array();
//临时路径
var **tempPath** = new Array();
console.log(typeof files)
if(typeof files == 'string') {
tempFiles.push(files)
} else {
tempFiles = files.files
};
console.log('tempFiles: ' + JSON.stringify(tempFiles))
for(var i = 0; i < tempFiles.length; i++) {
var filePath = tempFiles[i];
plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
console.log('129 ??')
var path = entry.toLocalURL() + "?version=" + new Date().getTime(); //文件路径
var srcPath = entry.toLocalURL();
var dstPath = that.savePath + entry.name;
var compressOptions = {};
var thisFile = filePath;
entry.file(function(thisFile) {
//console.log("文件名称" + file.name + "文件大小" + file.size);
});
//如果需要压缩
if(that.compress === true) {
//初始化压缩配置对象
for(var i in that.compressCfg) {
compressOptions[i] = that.compressCfg[i];
}
compressOptions["src"] = srcPath;
compressOptions["dst"] = dstPath;
//默认压缩到的目录有同名文件进行覆盖更新
compressOptions["overwrite"] = true;
plus.zip.compressImage(compressOptions, function(event) {
//console.log("相册需要压缩");
//console.log("压缩成功,文件大小" + event.size + "宽:" + event.width + "高:" + event.height + "--" + dstPath);
// that.callback(dstPath, path, that.options);
}, function(error) {
//console.log("压缩图片失败");
});
};
console.log('159 dstPath: ' + JSON.stringify(dstPath))
console.log('160 path: ' + JSON.stringify(path))
//把临时与原始路径放到相应数组中
tempPath.push(dstPath);
originalPath.push(path)
}, function(e) {
console.log("读取相册文件错误:" + e.message);
}) //end resolveLocalFileSystemURL
}; //end for
console.log('171 tempPath: ' + JSON.stringify(tempPath))
console.log('172 originalPath: ' + JSON.stringify(originalPath))
//如果是单选,取数组第一个
if(!that.multiple) {
tempPath = tempPath[0];
originalPath = originalPath[0]
};
that.callback(tempPath, originalPath, that.options)
}, function(e) {
// console.log("取消选择图片");
}, {
filter: that.filter,
multiple: that.multiple
})//end pick
console.log('171 tempPath: ' + JSON.stringify(tempPath)) 返回的是空
console.log('172 originalPath: ' + JSON.stringify(originalPath)) 返回的是空
5 个回复
cttren
看函数名也知道是检查本地文件的
2***@qq.com - 嗯嗯嗯,撂了吧,我写bug呢。
void plus.io.resolveLocalFileSystemURL( url, succesCB, errorCB );
这东西是异步的???
console.log('171 tempPath: ' + JSON.stringify(tempPath)) 返回的是空
console.log('172 originalPath: ' + JSON.stringify(originalPath)) 返回的是空
2***@qq.com - 嗯嗯嗯,撂了吧,我写bug呢。
没好法了,解决了,加了个定时,代码就不上了。
z***@163.com
如何解决的,能上下代码吗?
2018-04-04 17:53
2***@qq.com
我也是在别人的代码下改的,一个本地照片选择类。
/*
*/
(function(win) {
//判断组件库是否第一次载入,如果是初始化组件库
win.Plugin = win.Plugin || {};
//Photo类构造函数
function Photo(cfgs) {
var configs = cfgs || {};
var buttons = [{
title: "拍照"
}, {
title: "从手机相册选择"
}];
//初始化数据
this.savePath = configs.savePath || '_doc/tempImage/';
this.title = configs.title || "修改用户头像";
this.cancel = configs.cancel || "取消";
this.buttons = configs.buttons || buttons; //不推荐修改
this.options = configs.options || {};
this.callback = configs.callback || function() {};
this.compress = configs.compress || false; //默认不压缩
this.compressCfg = configs.compressCfg || {}; //默认压缩配置
this.filter = configs.filter || 'image'; //相册选择时的过滤类型默认(image图片),其他 video(仅可选择视频文件) none(不过滤,可选择图片或视频文件)
this.multiple = configs.multiple || false; //true是否多选,默认false(单选)
this.maximum = configs.maximum || 10; //最大选择图片的数量,默认是10张
}
//对象方法定义
Photo.prototype = {
init: function() {
//this.showSheet();
},
showSheet: function() { //点击选择
var that = this;
var actionSheetObj = {
title: that.title,
cancel: that.cancel,
buttons: that.buttons
};
plus.nativeUI.actionSheet(actionSheetObj, function(b) { /actionSheet 按钮点击事件/
switch(b.index) {
case 0:
break;
case 1:
that.appendByCamera(that); /拍照/
break;
case 2:
that.appendByGallery(that); /打开相册/
break;
default:
break;
}
})
},
//拍照
appendByCamera: function(that) {
// var path = entry.toLocalURL() + "?version=" + new Date().getTime(); //文件路径
var path = entry.toLocalURL();
console.log('photo.js 142 path: ' + path)
var srcPath = entry.toLocalURL();
var dstPath = that.savePath + entry.name;
var compressOptions = {};
})(window);
2018-04-11 08:34
2***@qq.com
这是使用方法:
//拍照或从相册选择图片
var Photo = new Plugin.Photo({
callback: function(files, path, options) {
详细的参数可以看photo.js最上面的参数合并部分,不能保证完美可能会有bug
2018-04-11 08:42
1***@qq.com
获取本地地址 ,无法转base64
1***@qq.com
这个本地只是格式,根据这个本地路劲根本找不到文件