• Flutter高仿微信-第33篇-单聊-图片


    Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。

     详情请查看

    效果图:

    详情请参考 Flutter高仿微信-第29篇-单聊 , 这里只是提取图片实现的部分代码。

    实现代码:

    //打开相册权限
    void _openAblumPermission() async {
      bool isPhotosGranted = await Permission.photos.isGranted;
      bool isPhotosDenied = await Permission.photos.isDenied;
      if(isPhotosGranted){
        _openAblum();
      } else {
        if(isPhotosDenied){
          _openAblum();
        } else {
          //跳转到设置页面提示
          _showPhotosConfirmationAlert(context);
        }
      }
    }

    // 为正常拍摄,请前往设备中的【设置】> 【隐私】> 【相机】中允许无他相机使用
    _showPhotosConfirmationAlert(BuildContext context) {
      showPlatformDialog(
        context: context,
        builder: (_) => BasicDialogAlert(
          title: Text("无法使用相册"),
          content: Text("为编辑照片,请前往设备中的【设置】> 【隐私】> 【照片】中允许${AppManager.getInstance().appName}使用"),
          actions: [
            BasicDialogAction(
              title: Text("知道了"),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            BasicDialogAction(
              title: Text("去设置"),
              onPressed: () {
                // 跳转到系统设置页
                AppSettings.openAppSettings();
              },
            ),
          ],
        ),
      );
    }

    //打开相册
    void _openAblum() {
      List selectedAssets = [];
      AssetPicker.pickAssets(
        context,
        pickerConfig: AssetPickerConfig(
          maxAssets: 1,
          selectedAssets: selectedAssets,
        ),
      ).then((imageList) {
        if(imageList == null){
          return;
        }
        imageList as List;
        for(int i = 0; i < imageList.length; i++){
          AssetEntity ae = imageList[i];
          ae.file.then((file) async {
            String resultFilePath = file?.path??"";
            _processVideoAndPicture(resultFilePath);
          });
        }
      });
    }

    //处理图片和小视频(相册、拍照)
    void _processVideoAndPicture(String resultFilePath) async {
    
      if(resultFilePath == null || "" == resultFilePath){
        return;
      }
    
      String messageId = UUID.getUUID();
    
      if(CommonUtils.isImage(resultFilePath)){
        //压缩图片完成再发送
        String compressImagePath = await CompressImageUtils.compressFile(fileName: resultFilePath);
        widget.sendMedialCallback(CommonUtils.CHAT_CONTENT_TYPE_IMG, compressImagePath,0 ,messageId);
        widget.refreshMediaCallback(CommonUtils.CHAT_CONTENT_TYPE_IMG, compressImagePath, "",0, messageId);
      } 
    

    }

    接收到消息:

    String serverImagePath = CommonUtils.BASE_URL_UPLOAD + content;
    String localImagePath = await FileUtils.getBaseFile("${DateUtil.getNowDateMs()}.jpg");
    //先下载图片
    await DownloadUtils.getInstance().downloadFile(serverImagePath, localImagePath);
    chatBean.imgPath = serverImagePath;
    chatBean.imgPathLocal = localImagePath;
    //通知栏提示
    NotificationUtils.getInstance().showNotification(chatBean);
    //插入本地数据库
    ChatRepository.getInstance().insertChat(chatBean);
    //EventBus刷新页面
    eventBus.emit(chatBean);

  • 相关阅读:
    给出一组正整数arr,你从第0个数向最后一个数,每个数的值表示你从这个位置可以向右跳跃的最大长度,计算如何以最少的跳跃次数跳到最后一个数。
    高精度与高精度的乘法---基础算法
    Vue组件间传值
    【前沿技术RPA】 RPA开发人员的日常
    vue3在路由route.js中获取不到仓库pinia中store里面的值
    是时候使用 YAML 来做配置或数据文件了
    面对6G时代 适合通信专业的 毕业设计题目
    Linux | 网络服务管理相关不完全总结
    【C++入门】C语言的不足之处
    JSON+<boost/property_tree/json_parser.hpp>+<boost/property_tree/ptree.hpp>
  • 原文地址:https://blog.csdn.net/maoning20080808/article/details/128013065