Flutter 图片保存到本地

本文介绍了如何在Flutter应用中使用image_gallery_saver插件将图片保存到本地。首先,在pubspec.yaml文件中引入插件,然后配置Android和iOS的权限。在Android的AndroidManifest.xml文件中进行相应设置。接着,通过调用插件的方法,可以轻松将图片保存到手机相册。此外,还提供了相关讨论群和资源链接以获取更多信息。

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

引入image_gallery_saver插件

pubspec.yaml文件中引入:

dependencies:
  image_gallery_saver: ^1.1.0

权限设置

  1. Android配置
    • 文件地址:android/app/src/profile/AndroidManifest.xml
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.zetc.zetc_app">
       <!-- Flutter needs it to communicate with the running application
           to allow setting breakpoints, to provide hot reload, etc.
       -->
       <uses-permission android:name="android.permission.INTERNET"/>
       <!--开启读写storage权限-->
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
      </manifest>
    • 文件地址:android/app/src/main/AndroidManifest.xml
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zetc.zetc_app">
      <uses-permission android:name="android.permission.INTERNET"/>
      <!--开启读写storage权限-->
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
      <!-- io.flutter.app.FlutterApplication is an android.app.Application that
          calls FlutterMain.startInitialization(this); in its onCreate method.
          In most cases you can leave this as-is, but you if you want to provide
          additional functionality it is fine to subclass or reimplement
          FlutterApplication and put your custom class here. -->
      <application
         android:name="io.flutter.app.FlutterApplication"
         android:label="zetc_app"
         android:icon="@mipmap/ic_launcher">
         <activity
             android:name=".MainActivity"
             android:launchMode="singleTop"
             android:theme="@style/LaunchTheme"
             android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
             android:hardwareAccelerated="true"
             android:windowSoftInputMode="adjustResize">
             <!-- This keeps the window background of the activity showing
                  until Flutter renders its first frame. It can be removed if
                  there is no splash screen (such as the default splash screen
                  defined in @style/LaunchTheme). -->
             <meta-data
                 android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                 android:value="true" />
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
         </activity>
      </application>
      </manifest>
  2. IOS配置

作用及使用

保存图片到手机

  1. 参考网址:image_gallery_saver
  2. 使用方式:

     import 'package:image_gallery_saver/image_gallery_saver.dart';
     import 'package:flutter/services.dart' show rootBundle;
     import 'dart:typed_data';
    
     _saveImg () async {
     // print(qrcodeData);
       ByteData bytes = await rootBundle.load("assets/images/weixin.jpg"); 
       final result = await ImageGallerySaver.save(bytes.buffer.asUint8List()); //这个是核心的保存图片的插件
       if(result){
         ToastUtil.showMsg('保存成功');
       }else{
         ToastUtil.showMsg('保存失败');
       }
     }

欢迎加群讨论更多flutter相关问题(7天有效)如果失效,可加个人微信拉群

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值