1***@qq.com
1***@qq.com
  • 发布:2021-07-24 11:39
  • 更新:2023-10-12 14:37
  • 阅读:4992

uniapp 拍照后保存到本地自己指定的文件夹怎么做

分类:uni-app

因为是离线 有网络后需要上传照片 所以需要把照片保存到本地文件夹 但是用户不能自己选取照片 所以不能存到相册里

2021-07-24 11:39 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com

我给大家 解答下 直接 上 代码

主要的 是 将 照片 变为base64 字符串 然后 调用 bitmap.save方法 存储到指定路径 、
用了 imagetool这个插件(插件市场里面找)

第一步
takePhoto() {
// if (that.photoArr.length > that.photoArrCapacity) {
// that.tips('超出限制咯~');
// return 0;
// }
let _this = this; //作用域不同导致的
uni.chooseImage({
// count: that.photoArrCapacity - that.photoArr.length,
count: 6, //可以几张
success(res) {
console.log('res ==>', res);
res.tempFilePaths.forEach(item => {
//默认照片存储位置 是在(内部存储-》android-》data-》io.dcloud.HBuilder->apps->Hbuilder->doc->里面)
//根据路径将照片转换为base64 再将 base64 转换为 照片
console.log(item);
_this.imgUrlToBase64(item)

                });  
            }  
        });  
    },  

第2步
imgUrlToBase64(url){
console.log("开始转换");

     pathToBase64(url)  
       .then(base64 => {  
         console.log("base64是"+base64)  
         //将base64 转换为 照片 并保存在自定义路径  
      this.saveHeadImgFile(base64);  
       })  
       .catch(error => {  
         console.error(error)  
       })  

            // 小程可用 APP不可用  
            // uni.getFileSystemManager().readFile({          
            // filePath: url, //选择图片返回的相对路径       
            // encoding: 'base64', //编码格式  
            // success: res => { //成功的回调                      
            //      console.log(res,'返回结果');  
//          let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符,在页面无法显示的哦  
            //                      this.imgURL=base64  
            //                  },fail: (e) => {  
            //                      console.log("图片转换失败");  
            //                }  
            //           })   

      },  

第3步 我这里保存在 系统相册和 指定路径的文件夹

      //保存文件  
      saveHeadImgFile(base64) {  
          let _this = this; //作用域不同导致的  
const bitmap = new plus.nativeObj.Bitmap("test");  
bitmap.loadBase64Data(base64, function() {  
    // const url = "_doc/" +"测试/"+"测试"+ new Date().getTime() + ".png";  // url为时间戳命名方式  
     const url = "file://storage/emulated/0/离线地图/" + "老虎.png";   //保存为android 路径  

    console.log('saveHeadImgFile', url)  
    bitmap.save(url, {  
        overwrite: true,  // 是否覆盖  
        // quality: 'quality'  // 图片清晰度  
    }, (i) => {  

        //保存到系统相册  
        uni.saveImageToPhotosAlbum({  
            filePath: url,  
            success: function() {  
              _this.uni.showToast({  
                    title: '图片保存成功',  
                    icon: 'none'  
                })  
                alert("图片保存成功");  
                bitmap.clear()  
            }  
        });  
        //保存到 指定文件夹  

    }, (e) => {  
         _this.uni.showToast({  
            title: '图片保存失败',  
            icon: 'none'  
        })  
        alert("图片保存失败");  
        bitmap.clear()  
    });  
}, (e) => {  
    _this.uni.showToast({  
        title: '图片保存失败',  
        icon: 'none'  
    })  
        alert("图片保存失败");  
    bitmap.clear()  
});
1***@qq.com

1***@qq.com

同问

6***@qq.com

6***@qq.com

有示例,只有app、小程序能行

https://uniapp.dcloud.io/api/file/file?id=savefile

  • 1***@qq.com

    这个 您知道 如何 将照片

    保存到 android 端的 指定文件夹吗

    2022-01-24 08:58

  • 6***@qq.com

    回复 1***@qq.com: app可以使用 5+ api https://www.html5plus.org/doc/zh_cn/io.html

    2022-01-24 13:52

下一个转弯

下一个转弯

拍摄后图片会自动存入系统图片路径,用户又可见了,怎么才能不保存到系统图片路径呢(没有办法删除系统图片路径下的文件)?

1***@qq.com

1***@qq.com

最新的 方法 之前的 方法 不对 现在 应当使用 plus.io.moveto 方法
代码 如下
let that=this;

                     plus.io.requestFileSystem(  
                       plus.io.PUBLIC_DOCUMENTS,  // 文件系统中的根目录  
                       fs => {  
                         // 创建或打开文件, fs.root是根目录操作对象,直接fs表示当前操作对象  
                         //这里切记 这里不考虑 android的 路径形式了 直接写 /storage 就可以了 不用 file 加上  
                        const urln = "/storage/emulated/0/离线地图";      //android 的自定义路径  
                         fs.root.getDirectory(urln, {     //创建文件夹  
                           create: true  // 文件不存在则创建  
                         }, fileEntry => {  
                            //移动文件   resolveLocalFileSystemURL判断本地文件是否存在  
                            plus.io.resolveLocalFileSystemURL(url, function( entry ){  

                            // entry.getParent( function ( parent ) {  
                                // parent.getParent( function ( parent2 ) {  
                                    entry.moveTo(fileEntry, "/"+url.slice(url.length-15,url.length), function( file ){  
                                        console.log("移动成功"+file)  
                                    },function(e){  
                                        alert( e.message );  
                                    });  
                                // })  

                             // });  
                            }); 
  • 冤家

    你这个会报安卓11版本不允许

    2023-02-18 21:54

  • 2***@qq.com

    回复 冤家: 你好,请问后面你解决安卓11版本的问题了嘛

    2023-04-25 16:19

HelloHell

HelloHell

使用image-tools实现,下载地址
https://github.com/zhetengbiji/image-tools/blob/master/index.js


    import {  
        pathToBase64  
    } from '@/static/image-tools/index.js'  

                        /**  
             * 调用系统相机  
             */  
            openSystemCamera() {  
                //console.log("gotoAlbum");  
                uni.chooseImage({  
                    count: 1, //默认9  
                    sizeType: [  
                        'original'  
                    ], //可以指定是原图还是压缩图'compressed',默认二者都有   
                    sourceType: ['camera'], // 调用系统相机  
                    success: function(res) {  
                        // 存入系統相冊  
                        // uni.saveImageToPhotosAlbum({   
                        //  filePath: res.tempFilePaths[0],  
                        //  success: function() {  
                        //      console.log('save success');  
                        //  }  
                        // });  
                        // 存入指定目录,先转为base64然后使用plus.nativeObj.Bitmap在指定目录创建文件  
                        pathToBase64(res.tempFilePaths[0]).then(base64 => {  
                            //console.log("base64是" + base64)  
                            const bitmap = new plus.nativeObj.Bitmap("test");  
                            bitmap.loadBase64Data(base64, function() {  
                                const url = "/storage/emulated/0/" + "cyb/images/" + new Date().getTime() +".png"; // url为时间戳命名方式    
                                bitmap.save(url, {  
                                    overwrite: true, // 是否覆盖    
                                    // quality: 'quality'  // 图片清晰度    
                                }, (i) => {  
                                    bitmap.clear()  
                                })  
                            })  
                        })  
                    } // success  
                })  
            },

要回复问题请先登录注册