Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何在Dart/FLATTER中读取本地图像文件的字节?_Image_Dart_Base64_Byte_Flutter - Fatal编程技术网

Image 如何在Dart/FLATTER中读取本地图像文件的字节?

Image 如何在Dart/FLATTER中读取本地图像文件的字节?,image,dart,base64,byte,flutter,Image,Dart,Base64,Byte,Flutter,我想使用一个占位符图像文件,该文件已作为“assets/placeholder.png”添加到我的代码中,但我收到一个文件未找到错误。这就是我在dartlang文档中所做的 var bytes = await new File('assets/placeholder.png').readAsBytes(); String base64 = CryptoUtils.bytesToBase64(bytes); 字节变量每次都会出错。如何保存本地保存的图像文件的字节?使用flift环境,如果要访问资

我想使用一个占位符图像文件,该文件已作为
“assets/placeholder.png”
添加到我的代码中,但我收到一个文件未找到错误。这就是我在dartlang文档中所做的

var bytes = await new File('assets/placeholder.png').readAsBytes();
String base64 = CryptoUtils.bytesToBase64(bytes);

字节
变量每次都会出错。如何保存本地保存的图像文件的字节?

使用flift环境,如果要访问资产(),必须使用
AssetBundle


在dart UINT8中,列表等于字节[]

  • 创建一个函数并传递文件路径,它将返回字节

    Future<Uint8List> _readFileByte(String filePath) async {
        Uri myUri = Uri.parse(filePath);
        File audioFile = new File.fromUri(myUri);
        Uint8List bytes;
        await audioFile.readAsBytes().then((value) {
        bytes = Uint8List.fromList(value); 
        print('reading of bytes is completed');
      }).catchError((onError) {
          print('Exception Error while reading audio from path:' +
          onError.toString());
      });
      return bytes;
    }
    
  • 如果需要base64String,请使用以下代码:

    String audioString=base64.encode(audioByte)

  • 对于base64导入“dart:convert”


    我希望这会有帮助

    下面是一个逐字节读取文件的示例:

    import 'dart:io';
    
    void main(List<String> arguments) {
      readFileByteByByte().then((done) {
        print('done');
      });
      print('waiting...');
      print('do something else while waiting...');
    }
    
    Future<bool> readFileByteByByte() async {
      //final fileName = 'C:\\code\\test\\file_test\\bin\\main.dart'; // use your image file name here
      final fileName = Platform.script.toFilePath(); //this will read this text file as an example
      final script = File(fileName);
      final file = await script.open(mode: FileMode.read);
    
      var byte;
      while (byte != -1) {
        byte = await file.readByte();
        if (byte == ';'.codeUnitAt(0)) { //check if byte is semicolon
          print(byte);
        }
      }
      await file.close();
      return (true);
    }
    
    导入'dart:io';
    void main(列出参数){
    ReadFileByteByByByByte()。然后((完成){
    打印(“完成”);
    });
    打印('等待…');
    打印('等待时做其他事情…');
    }
    未来的ReadFileByteByByByte()异步{
    //最终文件名='C:\\code\\test\\file\u test\\bin\\main.dart';//在此处使用图像文件名
    final fileName=Platform.script.toFilePath();//这将读取此文本文件作为示例
    最终脚本=文件(文件名);
    final file=wait script.open(模式:FileMode.read);
    变量字节;
    while(字节!=-1){
    byte=wait file.readByte();
    if(byte==';'.codeunit(0)){//检查byte是否为分号
    打印(字节);
    }
    }
    等待文件。关闭();
    返回(真);
    }
    
    如果需要读取一些字节,请使用此

    Future<Uint8List> readFileBytes(String path, int len) async {
        final File file = File(path);
        RandomAccessFile fileOpen = await file.open(mode: FileMode.read);
    
        int count = 0;
        List<int> bytes = [];
        int byte;
    
        while (byte != -1 && count < len) {
          byte = fileOpen.readByteSync();
          bytes.add(byte);
          count++;
        }
    
        await fileOpen.close();
        return Uint8List.fromList(bytes);
      }
    
    Future readFileBytes(字符串路径,int len)异步{
    最终文件=文件(路径);
    RandomAccessFile fileOpen=wait file.open(模式:FileMode.read);
    整数计数=0;
    列表字节=[];
    整数字节;
    while(字节!=-1&&count
    您可能想使用`Image.asset(“assets/placeholder.png”),而不是使用`Image.asset”(“assets/placeholder.png”),您知道如何将字节转换为BASE64字符串吗?我上面的CryptoUtils扩展无法工作。是的,很抱歉,您应该使用“dart:convert”中的BASE64,如果文件来自本地存储,请怎么办?正如@X09所述,此实现不适用于从本地存储加载图像。我使用了这个,首先它可以工作,但是如果你从路径上有空间的文件夹加载图像,它就不能工作。因此,请小心否决,因为建议的解决方案只会给问题(
    文件(“…”).readAsBytes()
    )中基本上已经正确陈述的内容添加噪音,而添加的内容与问题无关。对于那些以这个建议答案为例的人来说,要读取和映射(然后)来自未来的数据,您可以只分配等待的结果。。。然后(…),因此您不需要在回调范围之外声明变量,然后将其赋值。我在回调范围之外声明变量,因为如果我们以后可以在catch block之前使用它。futures可以返回错误,这可以用
    myFunc()处理。然后(processValue).catchError(handleError)
    ,请参阅:-)
    import 'dart:io';
    
    void main(List<String> arguments) {
      readFileByteByByte().then((done) {
        print('done');
      });
      print('waiting...');
      print('do something else while waiting...');
    }
    
    Future<bool> readFileByteByByte() async {
      //final fileName = 'C:\\code\\test\\file_test\\bin\\main.dart'; // use your image file name here
      final fileName = Platform.script.toFilePath(); //this will read this text file as an example
      final script = File(fileName);
      final file = await script.open(mode: FileMode.read);
    
      var byte;
      while (byte != -1) {
        byte = await file.readByte();
        if (byte == ';'.codeUnitAt(0)) { //check if byte is semicolon
          print(byte);
        }
      }
      await file.close();
      return (true);
    }
    
    Future<Uint8List> readFileBytes(String path, int len) async {
        final File file = File(path);
        RandomAccessFile fileOpen = await file.open(mode: FileMode.read);
    
        int count = 0;
        List<int> bytes = [];
        int byte;
    
        while (byte != -1 && count < len) {
          byte = fileOpen.readByteSync();
          bytes.add(byte);
          count++;
        }
    
        await fileOpen.close();
        return Uint8List.fromList(bytes);
      }