Home > Software engineering >  Flutter - android: Exception when convert large file (more than 1GB) to byte
Flutter - android: Exception when convert large file (more than 1GB) to byte

Time:08-04

I'm using dart:io to convert file to bytes. with image and short video it's work. but with large file (more than 1GB) i got error

E/flutter (16030): [ERROR:flutter/shell/common/shell.cc(93)] Dart Error: NewExternalTypedData expects argument 'length' to be in the range [0..1073741823].

my code

Uint8List bytes;
          try {
            bytes = imageFile.readAsBytesSync();
            print('bytes $bytes');
            lengthInBytes = bytes.buffer.asByteData().lengthInBytes;
          } catch (e) {
            print('e $e');
          }

it's look like limit of memory. any way to solve this?

CodePudding user response:

Try uploading in chunks of the file size is bigger. You can try some package like https://pub.dev/packages/chunked_uploader

ChunkedUploader chunkedUploader = ChunkedUploader(Dio(BaseOptions(
    baseUrl: 'https://example.com/api',
    headers: {'Authorization': 'Bearer'})));
try {
  Response? response = await chunkedUploader.upload(
      filePath: '/path/to/file',
      maxChunkSize: 500000,
      path: '/file',
      onUploadProgress: (progress) => print(progress));
  print(response);
} on DioError catch (e) {
  print(e);
}

It will automatically convert into chunks and upload

CodePudding user response:

i found a solution to get file size

imageFile.lengthSync()

it's solve my problem. readAsBytesSync make full memory

  • Related