1

I am trying to store images from firebase storage to the phone so that they can be available offline (downloaded). I am aware of this Stack Overflow question Download an image from Firebase to Flutter -- but that solution seems outdated as the "getData()" method now requires a maxSize-- (I just pick a high number like 9999999).

My current attempt looks like this. My thought process is that by downloading the data, I can then write it to a local file. I am using the path_provider pub.

 static Future<List<int>> downloadImage(String title, String route) async{

    List<int> data = await FirebaseStorage.instance.ref().child("English/routes/" + route + "/" + title +".jpg").getData(1000000);
    var text = new String.fromCharCodes(data);
    print("data= "+data.toString());
    print("text= " +text);
    return data;

  }

  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();
    return directory.path;
  }

  Future<File> localFile(String fileName) async {
    final path = await _localPath;
    return new File('$path/'+fileName+'.txt');
  }

  Future<File> writeImage(List<int> bytes, File file) async {
    // Write the file
    return file.writeAsBytes(bytes);
  }

When I run downloadImage, I get the following errors over and over again, overflowing my console.

I can't tell whether the error is due to authentication, because of the following error message

(E/StorageUtil(15931): error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.)

Or maybe something to do with the "maxSize", because of this error message

java.lang.IndexOutOfBoundsException: the maximum allowed buffer size was exceeded.

Or maybe both! But my rules are set to true for reading and writing so I don't even see how authentication has anything to do with it. I've not had to sign in before.

E/StorageException(15931): StorageException has occurred.
E/StorageException(15931): An unknown error occurred, please check the HTTP result code and inner exception for server response.
E/StorageException(15931):  Code: -13000 HttpResult: 200
E/StorageException(15931): the maximum allowed buffer size was exceeded.
E/StorageException(15931): java.lang.IndexOutOfBoundsException: the maximum allowed buffer size was exceeded.
E/StorageException(15931):  at com.google.firebase.storage.zzi.doInBackground(Unknown Source)
E/StorageException(15931):  at com.google.firebase.storage.StreamDownloadTask.run(Unknown Source)
E/StorageException(15931):  at com.google.firebase.storage.StorageTask.zzl(Unknown Source)
E/StorageException(15931):  at com.google.firebase.storage.zzq.run(Unknown Source)
E/StorageException(15931):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
E/StorageException(15931):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
E/StorageException(15931):  at java.lang.Thread.run(Thread.java:762)
E/StorageUtil(15931): error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
E/flutter (15931): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (15931): PlatformException(download_error, An unknown error occurred, please check the HTTP result code and inner exception for server response., null)
E/flutter (15931): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:547:7)
E/flutter (15931): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter (15931): <asynchronous suspension>
E/flutter (15931): #2      StorageReference.getData (package:firebase_storage/firebase_storage.dart:207:42)
E/flutter (15931): <asynchronous suspension>
E/flutter (15931): #3      Utils.downloadImage (package:flutter_alight/utils/utils.dart:88:108)
E/flutter (15931): <asynchronous suspension>
E/flutter (15931): #4      BusRouteItemState.downloadPois.<anonymous closure> (package:flutter_alight/UI/busRouteCards.dart:56:23)
E/flutter (15931): #5      _RootZone.runUnaryGuarded (dart:async/zone.dart:1316:10)
E/flutter (15931): #6      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:330:11)
E/flutter (15931): #7      _DelayedData.perform (dart:async/stream_impl.dart:578:14)
E/flutter (15931): #8      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:694:11)
E/flutter (15931): #9      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:654:7)
E/flutter (15931): #10     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (15931): #11     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
W/NetworkRequest(15931): no auth token for request
I/System.out(15931): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(15931): (HTTPLog)-Static: isSBSettingEnabled false
E/StorageReference(15931): the maximum allowed buffer size was exceeded.
W/StreamDownloadTask(15931): Exception occurred calling doInBackground.
W/StreamDownloadTask(15931): java.lang.IndexOutOfBoundsException: the maximum allowed buffer size was exceeded.
Andrew Takao
  • 105
  • 1
  • 13

1 Answers1

0

Maybe the high number you picked isn't high enough for what you want to download. 1mb is 1024*1024 = 1048576, which is higher than what you set as max limit. And your data could be larger than 1mb.

Check your data and set the max limit accordingly.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
Maria
  • 15
  • 1
  • 6