How to Read Bytes of a Local Image File In Flutter ?

· 3 min read
How to Read Bytes of a Local Image File In Flutter
How to Read Bytes of a Local Image File In Flutter

Sometimes when users make use of a local placeholder image file and run a flutter pub get he/she started getting File not Found. So in this article, we will go through How to Read Bytes of a Local Image File in Flutter?

How to Read Bytes of a Local Image File In Flutter?

With the Flutter Environment, you have to use AssetBundle if you want to access your assets.

import 'package:flutter/services.dart' show rootBundle;
ByteData bytes = await rootBundle.load('assets/placeholder.png');
  • In Dart, Uint8List is equal to byte[].

Create one function and pass file path, It will return Bytes.

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;
}

Now call the function is to get bytes of the file.

try{
  Uint8List audioByte;
  String myPath= 'MyPath/abc.png';
  _readFileByte(myPath).then((bytesData) {
    audioByte = bytesData;
  //do your task here 
  });
} catch (e) {
   // if path invalid or not able to read
  print(e);
}
  • If you want base64String then use the below code:
    String audioString = base64.encode(audioByte);

    for base64 import ‘dart:convert‘;

  • Users can also give try to below code snippet.
    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);
    }

Conclusion:

Thanks for being with us on a Flutter Journey.

Keep Learning !!! Keep Fluttering !!!

Still, need support for Flutter Development? Do contact us.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply