我愿你知道
我愿你知道
  • 发布:2023-03-10 09:31
  • 更新:2024-01-19 16:49
  • 阅读:459

Android10+,targetSdkVersion>=29之后,怎么将图片转成base64字符串

分类:uni-app

Android10+,targetSdkVersion>=29之后,安卓端限制了应用访问其他应用的文件。详情请看https://ask.dcloud.net.cn/article/36199

而使用uni.chooseImage从相册选择原图,返回的路径为系统公共目录,无法通过FileReader.readAsDataURL将其转化成base64字符串了,请问目前有替代方案吗?

2023-03-10 09:31 负责人:无 分享
已邀请:
我愿你知道

我愿你知道 (作者)

手机系统是鸿蒙3

小权

小权

同问

小权

小权

到底有没有办法啊,现在是死路一条了吗
@Android_TRY

喜欢技术的前端

喜欢技术的前端 - QQ---445849201

App端吗,有个依赖库 image-tools


uni.chooseImage({  
                    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有  
                    sourceType: ['album'], //从相册选择  
                    success: function(res) {  
                        console.log(res)  

                                                pathToBase64(res.tempFilePaths[0]).then(base64 => {  
                                  console.log(base64)  
                                })  
                                .catch(error => {  
                                     console.error(error)  
                                })  
                    }  
                });  

import {  
        pathToBase64,  
        base64ToPath  
    } from 'image-tools'
  • 小权

    你没搞懂问题,问题是安卓10+且targetSdkVersion>=29之后,由于安卓的分区存储机制,FileReader.readAsDataURL无法访问系统公共目录。看这个image-tools插件的源码在plus环境下也是用的FileReader.readAsDataURL

    2024-01-19 09:37

ccccxxll

ccccxxll

有哪位大佬知道app端视频怎么转blob对象吗

小权

小权

临时解决方案:
获取到相册原图后,通过uni.saveFile将图片复制一份到沙盒目录,比如"_doc"等。然后再通过FileReader.readAsDataURL读取新的路径。
不过我认为这肯定不应该是正常的解决方案,每次都要复制一份,那不是浪费空间和资源?

uni.chooseImage({  
                    count: 6, //默认9  
                    success: function (res) {  
                        var t_path=res.tempFilePaths[0]  

                        uni.saveFile({  
                            tempFilePath:t_path,  
                            success:function(re2){  
                                plus.io.resolveLocalFileSystemURL(re2.savedFilePath, function(entry) {  
                                    entry.file(function(file) {  
                                        var reader = new plus.io.FileReader();  
                                        reader.onloadend = function(e) {  
                                            console.log('read success',e.target.result)  
                                        }  
                                        reader.readAsDataURL(file);  
                                    })  
                                })  
                            }  
                        })  

                    }  
                });
  • 我愿你知道 (作者)

    可以在最后将复制的文件删了

    2024-01-20 15:35

  • 小权

    回复 我愿你知道: 是的,获得base64后,可以remove掉,但是这始终不优雅,想想要是很大的文件,每次都得有个复制的过程。。

    2024-01-22 09:13

要回复问题请先登录注册