如何在 Flutter 中将图像转换为文件?

Posted

技术标签:

【中文标题】如何在 Flutter 中将图像转换为文件?【英文标题】:How to convert Image to File in Flutter? 【发布时间】:2020-10-03 17:22:06 【问题描述】:

我正在编写这个颤振代码,其中我在图像小部件中有图像,我想将其转换为文件,以便我可以将其上传到 Firebase 存储。

Image _image = Image.asset('assets\images\profile.png');
File _fileImage = convertToFile(_image);
//upload _fileImage to Firebase Storage code

我需要File convertToFile(Image img) 函数。

【问题讨论】:

@Uni 我不是从照片或相机中挑选图像。它存在于资产文件夹中。 【参考方案1】:

特别感谢sami kanafani。

这是我的解决方案(非常适合我):

import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

class ImageUtils 
  static Future<File> imageToFile(String imageName, String ext) async 
    var bytes = await rootBundle.load('assets/$imageName.$ext');
    String tempPath = (await getTemporaryDirectory()).path;
    File file = File('$tempPath/profile.png');
    await file.writeAsBytes(
        bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
    return file;
  



class AnotherClass 
    File imagePlaceHolder;
    _setPlaceHolder() async 
         this.imagePlaceHolder = await ImageUtils.imageToFile(
         imageName: "photo_placeholder", ext: "jpg");
    

    ...
    Image.file(this.imagePlaceHolder),

【讨论】:

这部分代码在做什么? String tempPath = (await getTemporaryDirectory()).path; File file = File('$tempPath/profile.png'); await file.writeAsBytes( bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes)); 是放置文件 (profile.png) 的路径,而 bytes.buffer.asUint8List(.. 负责将“内存中的文件”写入设备。【参考方案2】:

我遇到了完全相同的问题,并使用了本指南: https://medium.com/@mrgulshanyadav/convert-image-url-to-file-format-in-flutter-10421bccfd18(作者 Gulshan Yadav)

使用这些导入:

import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:math'; 

还有这个功能:

Future<File> urlToFile(String imageUrl) async 
// generate random number.
var rng = new Random();
// get temporary directory of device.
Directory tempDir = await getTemporaryDirectory();
// get temporary path from temporary directory.
String tempPath = tempDir.path;
// create a new file in temporary path with random file name.
File file = new File('$tempPath'+ (rng.nextInt(100)).toString() +'.png');
// call http.get method and pass imageUrl into it to get response.
http.Response response = await http.get(imageUrl);
// write bodyBytes received in response to file.
await file.writeAsBytes(response.bodyBytes);
// now return the file which is created with random name in 
// temporary directory and image bytes from response is written to // that file.
return file;

【讨论】:

【参考方案3】:

您需要转换资产而不是图像

你需要安装path_provider插件


  var bytes = await rootBundle.load('assets\images\profile.png');
  String tempPath = (await getTemporaryDirectory()).path;
  File file = File('$tempPath/profile.png');
  await file.writeAsBytes(bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));

  return file;

【讨论】:

不起作用。抛出此错误。 [错误:flutter/lib/ui/ui_dart_state.cc(157)]未处理的异常:FileSystemException:无法打开文件,路径='/data/user/0/com.example.xyz/cache/images/profile.png'(操作系统错误:没有这样的文件或目录,errno = 2) 尝试将 \ 替换为 / 收到此错误。它返回空值。构建 UserSignUpScreen(dirty, state: _UserSignUpScreenState#e96f8): 'package:flutter/src/painting/image_provider.dart': 失败的断言: line 826 pos 14: 'file != null': is not true. 发现问题,从文件构造函数中删除 /image,我编辑了答案 如何将png转为jpg,请指教。谢谢。

以上是关于如何在 Flutter 中将图像转换为文件?的主要内容,如果未能解决你的问题,请参考以下文章

在 Flutter 错误中将 base64 转换为图像

如何在c#.net中将图像文件(如扫描副本)转换为文本文件[重复]

如何在 Flutter 中将 TextEditingController 转换为 String?

如何在 PHP 中将图像从 base 64 转换为它们的文件类型?

如何在 Flutter 中将 JSON 转换为对象? [关闭]

如何在 Flutter 中将 GIF 转换为 mp4 视频?