【问题标题】:Downloading image to file from firebase in flutter在颤动中从firebase下载图像到文件
【发布时间】:2018-10-22 01:48:26
【问题描述】:

我正在尝试将图像从 firebase 存储存储到手机,以便它们可以离线使用(下载)。我知道这个堆栈溢出问题Download an image from Firebase to Flutter -- 但是这个解决方案似乎已经过时了,因为“getData()”方法现在需要一个 maxSize--(我只选择了一个像 9999999 这样的大数字)。

我目前的尝试是这样的。我的想法是,通过下载数据,我可以将其写入本地文件。我正在使用 path_provider 酒吧。

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

当我运行 downloadImage 时,我一遍又一遍地收到以下错误,使我的控制台溢出。

由于以下错误消息,我无法判断错误是否是由于身份验证引起的

(E/StorageUtil(15931):获取令牌时出错 java.util.concurrent.ExecutionException:com.google.firebase.internal.api.FirebaseNoSignedInUserException:请先登录,然后再尝试获取令牌。)

或者可能与“maxSize”有关,因为这个错误消息

java.lang.IndexOutOfBoundsException: 超出了允许的最大缓冲区大小。

或者两者兼而有之!但是我的读写规则设置为真,所以我什至看不出身份验证与它有什么关系。我以前不必登录。

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.

【问题讨论】:

    标签: firebase firebase-authentication local-storage firebase-storage flutter


    【解决方案1】:

    也许您选择的大数字不足以下载您想要下载的内容。 1mb 是 1024*1024 = 1048576,高于您设置的最大限制。 而且您的数据可能大于 1mb。

    检查您的数据并相应地设置最大限制。

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 2019-10-29
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2017-11-01
      • 2018-05-22
      • 2019-11-25
      • 2020-12-13
      相关资源
      最近更新 更多