tina_xia
tina_xia
  • 发布:2016-03-19 19:41
  • 更新:2018-06-19 11:18
  • 阅读:19149

读取文件resolveLocalFileSystemURL

分类:HBuilder

void plus.io.resolveLocalFileSystemURL( url, succesCB, errorCB );
这个接口的uri只能是本地吗?用网路地址,就报执行错误

2016-03-19 19:41 负责人:无 分享
已邀请:
cttren

cttren

看函数名也知道是检查本地文件的

2***@qq.com

2***@qq.com - 嗯嗯嗯,撂了吧,我写bug呢。

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)) 返回的是空

2***@qq.com

2***@qq.com - 嗯嗯嗯,撂了吧,我写bug呢。

没好法了,解决了,加了个定时,代码就不上了。

  • z***@163.com

    如何解决的,能上下代码吗?

    2018-04-04 17:53

  • 2***@qq.com

    我也是在别人的代码下改的,一个本地照片选择类。

    /*



    • 照片选择类

    • @constructor Photo

    • @param {String} cfgs.title 修改原生标题

    • @param {String} cfgs.cancel 修改取消按钮文字描述

    • @param {Array} cfgs.buttons 修改按钮文字描述

    • @param {Object} cfgs.options 作为回调函数第三个形参传入

    • @param {Function(file,path,options)} cfgs.callback 获取图片成功以后的回调函数,file:文件,path:文件路径,options:初始传入的对象


    • */

      (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) {


          plus.camera.getCamera().captureImage(function(file) {  

      console.log('photo.js 67 file: ' + JSON.stringify(file))

      plus.io.resolveLocalFileSystemURL(file, function(entry) {

      var path = entry.toLocalURL();
      var srcPath = entry.toLocalURL();
      var dstPath = that.savePath + entry.name;
      console.log('photo.js 74 path: ' + JSON.stringify(path))
      console.log('photo.js 75 srcPath: ' + JSON.stringify(srcPath))
      console.log('photo.js 76 dstPath: ' + JSON.stringify(dstPath))
      var compressOptions = {};

      entry.file(function(file) {
      //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);
      that.callback('file://' + plus.io.convertLocalFileSystemURL(dstPath), path, that.options);
      }, function(error) {
      //console.log("压缩图片失败");
      });
      } else { //如果不需要压缩
      //console.log("拍照不需要压缩");
      //执行回调函数
      that.callback(file, path, that.options)
      }
      }, function(e) {
      console.log("读取拍照文件错误:" + e.message)
      })// end resolveLocalFileSystemURL

      })//captureImage

      },
      //打开相册
      appendByGallery: function(that) {

      plus.gallery.pick(function(files) {
      console.log('photo.js 110 files ' + files)
      //临时图片数组
      var tempFiles = [];
      //原始路径
      var originalPath = [];
      //临时路径
      var tempPath = [];
      //resolveLocalFileSystemURL 是异步读取,所以要做个标记,执行定时任务,当标记为真时再读取originalPath与tempPath并回调
      var flag = false;

      if(typeof files == 'string') {
      tempFiles.push(files)
      } else {
      tempFiles = files.files
      };

      console.log('photo.js 127 tempFiles: ' + JSON.stringify(tempFiles))

      for(var i = 0; i < tempFiles.length; i++) {
      //异步读取
      plus.io.resolveLocalFileSystemURL(tempFiles[i], function(entry) {



    // 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 = {};


                        var thisFile = tempFiles[i];  
    entry.file(function(thisFile) {
    console.log("原始文件名称" + thisFile.name + "文件大小" + thisFile.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(JSON.stringify(event))
    //console.log("相册需要压缩");
    console.log("压缩成功,文件大小" + event.size + "宽:" + event.width + "高:" + event.height + "--" + dstPath);
    // that.callback(dstPath, path, that.options);
    }, function(error) {
    //console.log("压缩图片失败");
    });
    };
    console.log('photo.js 161 dstPath: ' + JSON.stringify(dstPath))
    console.log('photo.js 162 path: ' + JSON.stringify(path))
    //把临时与原始路径放到相应数组中
    tempPath.push('file://' + plus.io.convertLocalFileSystemURL(dstPath));
    originalPath.push(path)

    }, function(e) {
    console.log("读取相册文件错误:" + e.message);
    }); //end resolveLocalFileSystemURL

    //如果是最后一个图片读完,标记为真
    if(i == tempFiles.length - 1) {
    flag = true;
    }
    }; //end for

    //定时任务-实时获取标记为真才能回调
    var timer = setInterval(function(){
    if(flag) {
    //如果 flag 为真,表示所有图片都读完,就不在执行定时任务
    clearInterval(timer);
    console.log('photo.js 182 tempPath: ' + JSON.stringify(tempPath))
    console.log('photo.js 183 originalPath: ' + JSON.stringify(originalPath))
    //如果是单选,取数组第一个
    if(!that.multiple) {
    tempPath = tempPath[0];
    originalPath = originalPath[0]
    };
    that.callback(tempPath, originalPath, that.options)
    }
    },500)
    }, function(e) {
    // console.log("取消选择图片");
    }, {
    filter: that.filter,
    multiple: that.multiple,
    maximum: that.maximum
    }) //end pick
    }

    }

    //将Photo类暴露在外部
    win.Plugin.Photo = Photo;

    })(window);

    2018-04-11 08:34

  • 2***@qq.com

    这是使用方法:

    //拍照或从相册选择图片

    var Photo = new Plugin.Photo({

    callback: function(files, path, options) {


                            },  
    options: {},
    title: "修改用户头像",
    compress: true,
    compressCfg: {
    //压缩转换后的图片格式
    format: 'jpg',
    //压缩图片的质量,1-100默认50
    quality: 40,
    width: '50%'
    }
    });
    Photo.showSheet()

    详细的参数可以看photo.js最上面的参数合并部分,不能保证完美可能会有bug

    2018-04-11 08:42

1***@qq.com

1***@qq.com

获取本地地址 ,无法转base64

1***@qq.com

1***@qq.com

这个本地只是格式,根据这个本地路劲根本找不到文件

该问题目前已经被锁定, 无法添加新回复