一尘不染

如何在flutter中使用rootBundle加载图像?

flutter

我使用图片插件( image:^ 2.0.4 ),以便可以在图片上写一些内容,然后将其保存为新图片到设备或通过邮件发送。我尝试使用“ new
File

加载图像,但Flutter出现错误。我询问并搜索,并得到一个提示,我可以使用rootBundle在Flutter中加载图像。我做到了,我得到以下错误。

[错误:topaz / lib / tonic / logging / dart_error.cc(16)]未处理的异常:无法加载资产:包/
myAppName /资产/图像/ReceiptRaw_1.jpg

当我创建一个简单的Dart控制台应用程序时,该插件可以工作,但无法使用Flutter加载。任何帮助,

这是Flutter代码:

    Future<bool> makeReceiptImage() async {

// UPDATE ****************************************

      // load the receipt jpeg
  var imageData = await rootBundle.load('packages/myAppName/dekonts/ReceiptRaw_1.jpg');
  print("imageData: $imageData"); // Prints as imageData: Instance of 
'_ByteDataView'

// UPDATE ****************************************

  Image _receiptImage = await decodeImage(new File(imageData).readAsBytesSync());

      drawString(_receiptImage, arial_48, 440, 30, “Customer Name”, color: 0xFF000000);

      // Write it to disk as a different jpeg 
      var new_jpeg = await encodeJpg(_receiptImage); 
      String newImagePath = await rootBundle.loadString('packages/myAppName/assets/images/ReceiptRaw_2.jpg'); 
      await new File(‘$newImagePath’).writeAsBytesSync(new_jpeg); 
    }

这是Dart控制台代码:

import 'dart:io';
import 'dart:convert';
import 'dart:async';
import 'package:image/image.dart';

void main() async {
  // load the receipt jpeg
  String mImagePath = 'images/ReceiptRaw_1.jpg';
  Image _receiptImage = decodeImage(new File(mImagePath).readAsBytesSync());
  drawString(_receiptImage, arial_48, 440, 30, “Customer Name”, color: 0xFF000000);

  // Write it to disk as a different jpeg
  var new_jpeg = encodeJpg(_receiptImage);
  new File('images/ReceiptRaw_1.jpg').writeAsBytesSync(new_jpeg);
}

更新1: 当我使用下面的代码时,我会得到以下错误:

在pubspec.yaml中检测到错误: 找不到资产的文件或变体:包/ myAppName /资产/图像/ReceiptRaw_1.jpg

 String imageData = await rootBundle.loadString('packages/myAppName/assets/images/ReceiptRaw_1.jpg');
  Image _receiptImage = await decodeImage(new File(imageData).readAsBytesSync());

更新2: 如果我使用rootBundle.load我得到以下错误。

错误:无法将类型为’dart.typed_data :: ByteData’的值分配给类型为’dart.core :: String’的变量。

var imageData = await rootBundle.load('packages/myAppName/assets/images/ReceiptRaw_1.jpg');
  Image _receiptImage = await decodeImage(new File(imageData).readAsBytesSync());

新更新:

步骤1: 移动到ReceiptRaw_1.jpglib/dekonts/

改成:

assets:
  - packages/myAppName/dekonts/ReceiptRaw_1.jpg

改成:

var imageData = await rootBundle.load('packages/myAppName/dekonts/ReceiptRaw_1.jpg');
print("imageData: $imageData");

结果: 打印为

imageData:“ _ ByteDataView”的实例

步骤2: 移至资料 /lib/assets/images/ReceiptRaw_1.jpg

改成:

assets:
  - packages/myAppName/lib/assets/images/ReceiptRaw_1.jpg

改成:

var imageData = await rootBundle.load('packages/myAppName/lib/assets/images/ReceiptRaw_1.jpg');
print("imageData: $imageData");

结果: 错误为:

解决依赖项…正在运行’gradlew
assembleDebug’…在pubspec.yaml中检测到错误:找不到资产的文件或变体:包/myAppName/lib/assets/images/ReceiptRaw_1.jpg

更新:

///要包含第一张图片,pubspec.yaml应用的图片应
///在资源部分中指定:
///
///资源:///-包/fancy_backgrounds/backgrounds/background1.png ///
/// lib/ 是隐含的,因此不应将其包含在资产路径中。


阅读 1433

收藏
2020-08-13

共1个答案

一尘不染

pub依赖项的文件不能作为文件使用。它们包含在存档文件中。

将图像添加到资产 pubspec.yaml

flutter:
  assets:
    - packages/myAppName/assets/images/ReceiptRaw_1.jpg

然后加载

var imageData = await rootBundle.load('packages/myAppName/assets/images/ReceiptRaw_1.jpg');

为此,ReceiptRaw_1.jpg需要将文件放入

myAppName/lib/assets/images/ReceiptRaw_1.jpg

如果该lib/部分是强制性的。

2020-08-13