【问题标题】:Can't download using flutter_downloader无法使用 flutter_downloader 下载
【发布时间】:2019-09-10 14:53:55
【问题描述】:

我正在尝试在我的颤振应用程序中使用颤振下载器。但是,当我触发下载操作时出现以下错误:

D/permissions_handler(16054): No permissions found in manifest for: $permission
D/permissions_handler(16054): No permissions found in manifest for: $permission
I/flutter (16054): decodePermissionRequestResult called with: value:[{14: 4}]
W/WM-WorkSpec(16054): Backoff delay duration less than minimum value
E/MethodChannel#vn.hunghd/downloader(16054): Failed to handle method call
E/MethodChannel#vn.hunghd/downloader(16054): java.lang.IllegalStateException: WorkManager is not initialized properly.  The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.
E/MethodChannel#vn.hunghd/downloader(16054):    at androidx.work.WorkManager.getInstance(WorkManager.java:141)
E/MethodChannel#vn.hunghd/downloader(16054):    at vn.hunghd.flutterdownloader.FlutterDownloaderPlugin.enqueue(FlutterDownloaderPlugin.java:175)
E/MethodChannel#vn.hunghd/downloader(16054):    at vn.hunghd.flutterdownloader.FlutterDownloaderPlugin.onMethodCall(FlutterDownloaderPlugin.java:73)
E/MethodChannel#vn.hunghd/downloader(16054):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:201)
E/MethodChannel#vn.hunghd/downloader(16054):    at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl.handleMessageFromDart(FlutterNativeView.java:188)
E/MethodChannel#vn.hunghd/downloader(16054):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:202)
E/MethodChannel#vn.hunghd/downloader(16054):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#vn.hunghd/downloader(16054):    at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#vn.hunghd/downloader(16054):    at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#vn.hunghd/downloader(16054):    at android.app.ActivityThread.main(ActivityThread.java:6669)
E/MethodChannel#vn.hunghd/downloader(16054):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#vn.hunghd/downloader(16054):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#vn.hunghd/downloader(16054):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/flutter (16054): Download task is failed with reason(WorkManager is not initialized properly.  The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.)
W/5gmail.playlis(16054): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (light greylist, linking)

这是我的下载管理器类。

import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class DownloadManager {
  Future<bool> checkPermission() async {
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.storage);
    if (permission != PermissionStatus.granted) {
      Map<PermissionGroup, PermissionStatus> permissions =
      await PermissionHandler()
          .requestPermissions([PermissionGroup.storage]);
      if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
        return true;
      }
    }
    return false;
  }

  void inti(String url) async {
    await checkPermission();
    final directory = await getExternalStorageDirectory();
    final taskId = await FlutterDownloader.enqueue(
      url: url,
      savedDir: directory.toString(),
      showNotification: true, // show download progress in status bar (for Android)
      openFileFromNotification: true, // click on notification to open downloaded file (for Android)
    );
    FlutterDownloader.registerCallback((id, status,progress) {
      print(
          'Download task ($id) is in status ($status) and process ($progress)');
    });
  }
}

我正在使用这个插件flutter_downloader

谁能帮忙?

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    尝试添加这个

     <uses-permission 
            android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    还有

     android:requestLegacyExternalStorage="true"
    

    在您的 AndroidManifest.xml 中将起作用

    【讨论】:

      【解决方案2】:

      我建议你使用 Dart 下载文件。

      您只需注册您的应用即可支持 Android 和 iOS 上的后台工作。

      flutter_workmanager插件的帮助下,这相对容易。
      您可以注册您的应用程序,以便在需要运行时在 Dart 代码中接收回调。

      例如:

      void callbackDispatcher() {
        Workmanager.executeTask((backgroundTask) {
          switch(backgroundTask) {
            case "downloadMyFile":
              print("You are now in a background Isolate");
              print("Download your file here using Dart");
              HttpClient client = new HttpClient();
              var _downloadData = List<int>();
              var fileSave = new File('./logo.png');
              client.getUrl(
                  Uri.parse("https://fluttermaster.com/wp-content/uploads/2018/08/fluttermaster.com-logo-web-header.png"))
                  .then((HttpClientRequest request) {
                return request.close();
              })
                  .then((HttpClientResponse response) {
                response.listen((d) => _downloadData.addAll(d),
                    onDone: () {
                      fileSave.writeAsBytes(_downloadData);
                    }
                );
              });
              break;
          }
          return Future.value(true);
        });
      }
      
      void main() {
        Workmanager.initialize(callbackDispatcher);
        Workmanager.registerOneOffTask("1", "downloadMyFile");
        runApp(MyApp());
      }
      

      【讨论】:

        【解决方案3】:

        尝试将&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; 添加到AndroidManifest.xml

        【讨论】:

          猜你喜欢
          • 2020-10-18
          • 2020-06-07
          • 2021-06-15
          • 2015-08-28
          • 2020-11-06
          • 2020-01-24
          • 2017-09-21
          • 2018-04-16
          • 1970-01-01
          相关资源
          最近更新 更多