【问题标题】:How to convert asset image to File?如何将资产图像转换为文件?
【发布时间】:2019-08-13 04:52:12
【问题描述】:

有没有办法将资产图像用作文件。 我需要一个文件,以便可以使用 http 在 Internet 上对其进行测试。我尝试了 Stackoverflow.com (How to load images with image.file) 的一些答案,但收到错误“无法打开文件,(操作系统错误:没有这样的文件或目录,errno = 2)”。附加的代码行也给出了错误。

File f = File('images/myImage.jpg');

RaisedButton(
   onPressed: ()=> showDialog(
     context: context,
     builder: (_) => Container(child: Image.file(f),)),
   child: Text('Show Image'),)

使用 Image.memory 小部件(作品)

Future<Null> myGetByte() async {
    _byteData = await rootBundle.load('images/myImage.jpg');
  }

  /// called inside build function
  Future<File> fileByte = myGetByte();

 /// Show Image
 Container(child: fileByte != null
 ? Image.memory(_byteData.buffer.asUint8List(_byteData.offsetInBytes, _ 
 byteData.lengthInBytes))
 : Text('No Image File'))),

【问题讨论】:

  • 使用 AssetBundle 将其读入内存并存储到文件中。

标签: dart flutter


【解决方案1】:

您可以通过rootBundle 访问字节数据。然后,您可以将其保存到设备的临时目录中,该目录由path_provider 获取(需要添加为依赖项)。

import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';

Future<File> getImageFileFromAssets(String path) async {
  final byteData = await rootBundle.load('assets/$path');

  final file = File('${(await getTemporaryDirectory()).path}/$path');
  await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

  return file;
}

在你的例子中,你会这样调用这个函数:

File f = await getImageFileFromAssets('images/myImage.jpg');

有关写入字节数据的更多信息,check out this answer

您需要await Future 并为此创建函数async

RaisedButton(
   onPressed: () async => showDialog(
     context: context,
     builder: (_) => Container(child: Image.file(await getImageFileFromAssets('images/myImage.jpg')))),
   child: Text('Show Image'));

【讨论】:

  • 我测试它并得到一个错误:'参数类型 Future 不能分配给参数类型文件'。我在上面添加了测试代码。
  • @creativecreator 或许现在它真的有效。我遇到的问题是函数'getImageFileFromAssets'在getTemporaryDirectory()处添加了'images/myImage.jpg',而不仅仅是'/myImage.jpg'。终于有文件了!谢谢。
  • 嗨,我遵循解决方案,但我收到有关未处理异常的错误:FileSystemException:无法打开文件,路径 = '/data/user/0/com.example.myApp/cache/images/black .png'(操作系统错误:没有这样的文件或目录,errno = 2)
  • @creativecreatorormaybenot 我认为您应该将final file = File('${(await getTemporaryDirectory()).path}/$path'); 替换为final file = File('${(await getTemporaryDirectory()).path}/image.png'); 它将为未来的commers 节省时间
  • 我在创建文件后添加了await file.create(recursive: true);。因为如果您的路径包含尚不存在的目录,则写入该文件会失败,因为默认情况下 create 是递归 false。
【解决方案2】:

在不提供路径的情况下从资产中获取文件。

import 'package:path_provider/path_provider.dart';


Future<File> getImageFileFromAssets(Asset asset) async {
    final byteData = await asset.getByteData();

    final tempFile =
        File("${(await getTemporaryDirectory()).path}/${asset.name}");
    final file = await tempFile.writeAsBytes(
      byteData.buffer
          .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes),
    );

    return file;
  }

【讨论】:

  • 非常感谢,您在下一个link 旁边问我。对于 iOS 14.x 中 image_picker 有问题的人
  • 资产类在我能找到的任何地方都不存在。有什么见解吗?
【解决方案3】:

利用flutter_absolute_path包。

flutter_absolute_path: ^1.0.6

在 pubsec.yaml 中

从这种格式转换文件路径:

“content://media/external/images/media/5275”

"/storage/emulated/0/DCIM/Camera/IMG_00124.jpg”

======

List <File> fileImageArray = [];
assetArray.forEach((imageAsset) async {
final filePath = await FlutterAbsolutePath.getAbsolutePath(imageAsset.identifier);

File tempFile = File(filePath);
if (tempFile.existsSync()) {
    fileImageArray.add(tempFile);
}

【讨论】:

  • you forget ")" 在最后一行编辑它以方便其他人使用
猜你喜欢
  • 2017-09-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2012-06-29
  • 2014-07-17
  • 1970-01-01
相关资源
最近更新 更多