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);
        },
      });
    },
相关推荐
手机不死我是天子1 小时前
《Android 核心组件深度系列 · 第 3 篇 BroadcastReceiver》
android·android studio
用户17345666963471 小时前
Android 日志库:高性能压缩加密日志系统
android
木易 士心1 小时前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
恋猫de小郭2 小时前
React 和 React Native 不再直接归属 Meta,React 基金会成立
android·前端·ios
bst@微胖子2 小时前
鸿蒙实现滴滴出行项目之侧边抽屉栏以及权限以及搜索定位功能
android·华为·harmonyos
zcz16071278212 小时前
Docker Compose 搭建 LNMP 环境并部署 WordPress 论坛
android·adb·docker
Arva .4 小时前
HTTP Client
网络协议·http·lua
Pika12 小时前
深入浅出 Compose 测量机制
android·android jetpack·composer
木易 士心17 小时前
MPAndroidChart 用法解析和性能优化 - Kotlin & Java 双版本
android·java·kotlin
消失的旧时光-194317 小时前
Kotlin Flow 与“天然背压”(完整示例)
android·开发语言·kotlin