uni-app 启动后首次调用uni.chooseImage返回压缩图片有效期只有5s左右

uni-app 启动后首次调用uni.chooseImage返回压缩图片有效期只有5s左右

开发环境 版本号 项目创建方式
Windows 10 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Windows

PC开发环境操作系统版本号:Windows 10 家庭中文版 版本号:20H2 内部版本:19042.1110

HBuilderX类型:正式

HBuilderX版本号:3.4.9

手机系统:Android

手机系统版本号:Android 10

手机厂商:华为

手机机型:荣耀V20

页面类型:vue

vue版本:vue2

打包方式:云端

项目创建方式:HBuilderX


示例代码:

let [err, res] = await uni.chooseImage({  
    sourceType: ['album'],  
    sizeType: ['compressed']  
});

不限制sizeType也可以,选择的时候不勾选【原图】一样可以复现。

完整测试Demo代码:

<template>  
    <view class="content">  
        <image class="logo" src="/static/logo.png"></image>  
        <view class="text-area">  
            <text class="title">{{title}}</text>  
        </view>  
        <button @click="selImgs">选择图片</button>  
    </view>  
</template>  

<script>  
    let timer = null;  
    export default {  
        data() {  
            return {  
                title: 'Hello'  
            }  
        },  
        onLoad() {  

        },  
        methods: {  
            async selImgs(){  
                clearInterval(timer);  
                let [err, res] = await uni.chooseImage({  
                    sourceType: ['album'],  
                    sizeType: ['compressed']  
                });  
                if(err){  
                    console.error('choose error:', err);  
                    return;  
                }  
                let initStart = new Date() / 1;  
                timer = setInterval(async () => {  
                    let succNum = await this.checkImgInfos(res.tempFiles);  
                    let cost = new Date() / 1 - initStart;  
                    this.title = '有效图片:' + succNum + ', valid for ' + cost + ' ms';  
                    if(!succNum){  
                        clearInterval(timer);  
                    }  
                }, 100);  
            },  
            async checkImgInfos(images){  
                let succNum = 0;  
                for(let img of images){  
                    let [err, res] = await uni.getFileInfo({filePath: img.path});  
                    if(!err && res.size){  
                        succNum += 1;  
                    }  
                }  
                return succNum;  
            }  
        }  
    }  
</script>  
2 回复

已复现


在使用 uni-app 开发时,如果你发现通过 uni.chooseImage 选择的图片在返回后,其临时路径的有效期只有 5 秒左右,这通常是因为 uni.chooseImage 返回的图片路径是临时路径,这些路径在短时间内可能会失效。

解决方案

  1. 立即处理图片: 如果你需要在应用中使用这些图片,建议在获取到临时路径后立即进行处理,比如上传到服务器或者保存到本地存储中。

    uni.chooseImage({
        count: 1,
        success: function (res) {
            const tempFilePaths = res.tempFilePaths;
            // 立即处理图片,比如上传到服务器
            uploadImage(tempFilePaths[0]);
        }
    });
    
    function uploadImage(tempFilePath) {
        uni.uploadFile({
            url: 'https://example.com/upload',
            filePath: tempFilePath,
            name: 'file',
            success: function (uploadRes) {
                console.log('上传成功', uploadRes.data);
            },
            fail: function (err) {
                console.error('上传失败', err);
            }
        });
    }
  2. 保存到本地: 如果你需要长期使用这些图片,可以将图片保存到本地存储中,这样即使临时路径失效,你仍然可以通过本地路径访问图片。

    uni.chooseImage({
        count: 1,
        success: function (res) {
            const tempFilePaths = res.tempFilePaths;
            // 保存到本地
            uni.saveFile({
                tempFilePath: tempFilePaths[0],
                success: function (saveRes) {
                    const savedFilePath = saveRes.savedFilePath;
                    console.log('保存成功', savedFilePath);
                },
                fail: function (err) {
                    console.error('保存失败', err);
                }
            });
        }
    });
  3. 使用 uni.getFileSystemManager: 如果你需要更灵活地处理文件,可以使用 uni.getFileSystemManager API 来管理文件系统,比如读取文件内容、复制文件等。

    uni.chooseImage({
        count: 1,
        success: function (res) {
            const tempFilePaths = res.tempFilePaths;
            const fs = uni.getFileSystemManager();
            fs.readFile({
                filePath: tempFilePaths[0],
                encoding: 'base64',
                success: function (readRes) {
                    console.log('文件内容', readRes.data);
                },
                fail: function (err) {
                    console.error('读取文件失败', err);
                }
            });
        }
    });
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!