uniapp 安卓app图片回显,默认不支持http图片地址,上传图片和回显图片

复制代码
<view class="uni-form-item uni-column">
          <view class="title"><text class="corlorRed">*</text> 现场照片</view>
          <view class="photoStyle">
            <view class="photoItem">
              <view>故障全貌</view>
              <view class="btn" @click="uploadOption('1')">点击上传</view>
              <image
                v-if="img1"
                :src="img1"
                style="width: 25px; height: 25px; margin-left: 5px"
                @longpress="previewImage(img1)"
              ></image>
              <text
                v-else
                style="width: 25px; height: 25px; margin-left: 5px"
              ></text>
            </view>
            <view class="photoItem">
              <view>故障详情</view>
              <view class="btn" @click="uploadOption('2')">点击上传</view>
              <image
                v-if="img2"
                :src="img2"
                style="width: 25px; height: 25px; margin-left: 5px"
                @longpress="previewImage(img2)"
              ></image>
              <text
                v-else
                style="width: 25px; height: 25px; margin-left: 5px"
              ></text>
            </view>
            <view class="photoItem">
              <view>维修详情</view>
              <view class="btn" @click="uploadOption('3')">点击上传</view>
              <image
                v-if="img3"
                :src="img3"
                style="width: 25px; height: 25px; margin-left: 5px"
                @longpress="previewImage(img3)"
              ></image>
              <text
                v-else
                style="width: 25px; height: 25px; margin-left: 5px"
              ></text>
            </view>
          </view>
        </view>

压缩图片需要安装插件 const ImageCompressor = require("js-image-compressor");

复制代码
// 预览图片
    previewImage(url) {
      uni.previewImage({
        current: url, // 当前显示图片的 URL(可选,默认显示第一张)
        urls: [url], // 需预览的图片列表(单张需用数组)
        success: () => {}, // 可选回调
        fail: (err) => console.log("预览失败:", err),
      });
    },
    //转为文件
    base64ToFile(urlData, fileName) {
      let arr = urlData.split(",");
      let mime = arr[0].match(/:(.*?);/)[1];
      let bytes = atob(arr[1]);
      let n = bytes.length;
      let ia = new Uint8Array(n);
      while (n--) {
        ia[n] = bytes.charCodeAt(n);
      }
      return new File([ia], fileName, { type: mime });
    },
    getImgUrl(imgUrl, index) {
      uni.request({
        url: imgUrl, // 替换为你的图片URL
        responseType: "arraybuffer",
        success: (res) => {
          let base64 = wx.arrayBufferToBase64(res.data);
          let imageSrc = "data:image/jpg;base64," + base64;
          // 将图片URL设置给图片元素的src属性
          if (index == 1) {
            this.img1 = imageSrc;
          } else if (index == 2) {
            this.img2 = imageSrc;
          } else if (index == 3) {
            this.img3 = imageSrc;
          }
        },
      });
    },
    // 压缩图片
    compressionImage(file, index) {
      let _this = this;
      let content = null;
      let readfile = new FileReader();
      return new Promise((resolve, reject) => {
        // eslint-disable-next-line no-new
        new ImageCompressor({
          file: file,
          quality: 0.8,
          maxWidth: 88,
          maxHeight: 88,
          redressOrientation: false,
          // 压缩前回调
          beforeCompress: function (result) {
            // console.log('压缩之前图片尺寸大小: ', result.size)
            // console.log('mime 类型: ', result.type)
          },

          success: function (result) {
            // console.log("压缩之后图片尺寸大小: ", result.size, result);
            // console.log("mime 类型: ", result.type);
            // console.log(
            //   "实际压缩率: ",
            //   (((file.size - result.size) / file.size) * 100).toFixed(2) + "%"
            // );

            content = readfile.readAsDataURL(result, "UTF-8");
            readfile.onload = function (event) {
              content = event.target.result;
              let blod = _this.base64ToFile(
                content,
                new Date().getTime() + ".png"
              );
              // 调用后端上传接口
              uni.uploadFile({
                url: _this.$baseUrl + "/order/ftp/5g/uploadFile",
                filePath: file.path, // 选择的图片路径
                name: "file", // 后端接收文件的字段
                header: {
                  Authorization: _this.userInfo.token,
                },
                success: (res) => {
                  let data = JSON.parse(res.data).data;
                  uni.showToast({
                    title: "上传成功",
                    icon: "success",
                  });
                  _this.form.images[index] = [
                    {
                      fileName: data.fileName,
                      fileUrl: data.fileUrl,
                      type: index,
                    },
                  ];
                  if (index == 1) {
                    // _this.img1 = _this.$baseUrlImg + data.fileUrl;
                    // #ifdef H5
                    _this.img1 = _this.$baseUrlImg + data.fileUrl;
                    // #endif

                    // #ifdef APP-PLUS
                    let imgUrl = _this.$baseUrlImg + data.fileUrl;
                    _this.getImgUrl(imgUrl, 1);
                    // #endif
                  } else if (index == 2) {
                    // _this.img2 = _this.$baseUrlImg + data.fileUrl;
                    // #ifdef H5
                    _this.img2 = _this.$baseUrlImg + data.fileUrl;
                    // #endif

                    // #ifdef APP-PLUS
                    let imgUrl = _this.$baseUrlImg + data.fileUrl;
                    _this.getImgUrl(imgUrl, 2);
                    // #endif
                  } else if (index == 3) {
                    // _this.img3 = _this.$baseUrlImg + data.fileUrl;
                    // #ifdef H5
                    _this.img3 = _this.$baseUrlImg + data.fileUrl;
                    // #endif

                    // #ifdef APP-PLUS
                    let imgUrl = _this.$baseUrlImg + data.fileUrl;
                    _this.getImgUrl(imgUrl, 3);
                    // #endif
                  }
                },
                fail: (err) => {
                  console.error("上传失败", err);
                  uni.showToast({
                    title: "上传失败,请重试",
                    icon: "none",
                  });
                },
              });
            };
            resolve(result);
          },
          error(e) {
            reject(e);
          },
        });
      });
    },
    uploadOption(index) {
      let _this = this;
      uni.chooseImage({
        count: 1, // 一次选择一张图片
        sizeType: ["original", "compressed"], // 可选择原图或压缩图
        sourceType: ["album", "camera"], // 可从相册或相机选取图片
        success: function (e) {
          // console.log(11111111, e);
          _this.compressionImage(e.tempFiles[0], index);
        },
        fail: function (err) {
          console.log(err);
        },
      });
    },
相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo3 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077003 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
XUEYUAN52123 天前
ASN 自治系统号风控:平台如何通过 IP 所属自治域批量识别代理流量
python·网络协议·http·网络安全·socks5
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
码兄科技3 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
AFinalStone3 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui