Flutter高仿微信-第14篇-朋友圈-相册选择图片或小视频

本文介绍了一个Flutter应用如何实现微信风格的功能,特别聚焦于处理相册权限、选择图片和视频的过程。文章详细展示了如何请求相册权限、选择图片及视频,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

详情请查看

效果图:

实现代码:

//打开相册权限
void _openAblumPermission(int type) async {
  mediaType = type;
  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: <Widget>[
        BasicDialogAction(
          title: Text("知道了"),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        BasicDialogAction(
          title: Text("去设置"),
          onPressed: () {
            // 跳转到系统设置页
            AppSettings.openAppSettings();
          },
        ),
      ],
    ),
  );
}

void _openAblum() async{
  LogUtils.d("打开相册 ${mediaType}");
  List<AssetEntity> selectedAssets = [];
  List<AssetEntity>? imageList;
  if(mediaType == MEDIA_TYPE_PICTURE){
    imageList = await AssetPicker.pickAssets(context,pickerConfig: AssetPickerConfig(maxAssets: 9,selectedAssets: selectedAssets,requestType: RequestType.image,),);
  } else if(mediaType == MEDIA_TYPE_VIDEO){
    imageList = await AssetPicker.pickAssets(context,pickerConfig: AssetPickerConfig(maxAssets: 1,selectedAssets: selectedAssets,requestType: RequestType.video,),);
  } else {
    return;
  }
  LogUtils.d("打开相册 ${imageList}");
  if(imageList == null){
    return;
  }
  if(mediaType == MEDIA_TYPE_PICTURE){
    List<String> imageResultList = [];
    for(int i = 0; i < imageList.length; i++){
      AssetEntity ae = imageList[i];
      File? file = await ae.file;
      String resultFilePath = file?.path??"";
      imageResultList.add(resultFilePath);
    }
    LogUtils.d("打开相册 _processPicture ");
    _processPicture(imageResultList);
  } else if(mediaType == MEDIA_TYPE_VIDEO){
    //只有一个小视频
    AssetEntity ae = imageList[0];
    File? file = await ae.file;
    String resultFilePath = file?.path??"";
    _processVideo(resultFilePath);
  }

}

//处理图片
void _processPicture(List<String> imageResultList) async {
  LogUtils.d("处理图片选择file图片ae返回:${imageResultList}");
  if(imageResultList == null){
    return;
  }

  List<String> compressImageResultList = [];
  for(int i = 0; i < imageResultList.length; i++){
    String resultFilePath = imageResultList[i];
    LogUtils.d("压缩前图片:${resultFilePath}");
    //压缩完成再发送
    String compressImagePath = await CompressImageUtils.compressFile(fileName: resultFilePath);
    LogUtils.d("压缩后图片:${compressImagePath}");
    compressImageResultList.add(compressImagePath);
  }

  String videoFilePath = "";
  var result = await Navigator.push(context, MaterialPageRoute(builder: (context) => MomentsPublishWidget(type: CommonUtils.MOMENTS_TYPE_PICTURE, imageList: compressImageResultList, videoFilePath: videoFilePath,)));
  LogUtils.d("发布返回:${result}");
  if(result != null && result){
    _getData();
  }
}

//处理小视频(相册、拍照)
void _processVideo(String resultFilePath) async {
  LogUtils.d("选择file图片ae返回:${resultFilePath}");
  if(resultFilePath == null || "" == resultFilePath){
    return;
  }

  //最大100M的视频, 不是1024*1024*500 , 使用1000*100*500
  int maxSize = 100000000;
  int fileSize = File(resultFilePath).lengthSync();
  LogUtils.d("视频大小:${fileSize}");
  if(fileSize > maxSize){
    CommonToast.show(context, "上传视频大小不能超过100M", duration: 3);
    return ;
  }
  Navigator.push(context, MaterialPageRoute(builder: (context) => MomentsPublishWidget(type: CommonUtils.MOMENTS_TYPE_VIDEO, imageList: [], videoFilePath: resultFilePath,)));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android-Flutter

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值