我正在尝试在flutter应用程序中使用flutter_downloader。但是,触发下载操作时出现以下错误:
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。
有人可以帮忙吗?
答案 0 :(得分:2)
尝试添加这个
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
以及
android:requestLegacyExternalStorage="true"
在你的 AndroidManifest.xml 中可以使用
答案 1 :(得分:1)
我建议您使用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());
}
答案 2 :(得分:-1)
尝试将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
添加到AndroidManifest.xml