简体   繁体   English

Flutter:当应用程序在后台时使用路径提供程序

[英]Flutter: Using path provider when app is in background

I am currently trying to implement FCM and local notifications into my Flutter app.我目前正在尝试在我的 Flutter 应用程序中实现 FCM 和本地通知。 I have successfully configured FCM and the Local notifications for normal notifications, But i also have a different type of notification that I would like to display with an Image, When my app is in the foreground the notification is displayed without error, However when I terminate the app / move it to background I get an exception when trying to save the image using path provider.我已经成功地为普通通知配置了 FCM 和本地通知,但我也有不同类型的通知,我想用图像显示,当我的应用程序在前台时,通知显示没有错误,但是当我终止时应用程序/将其移至后台尝试使用路径提供程序保存图像时出现异常。

The exception:例外:

MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)

I'm assuming this error is occurring because the path provider method channel is closed when the app is not in the foreground, Is there something I can do to fix this?我假设发生此错误是因为当应用程序不在前台时路径提供程序方法通道已关闭,我能做些什么来解决这个问题吗? Or if not the flutter_local_notifications plugin requires a filepath to a bitmap, Can I achieve saving the image and getting a path in a different way that will work in background (without path provider)?或者,如果不是flutter_local_notifications插件需要 bitmap 的文件路径,我可以实现保存图像并以不同的方式获取在后台工作的路径(没有路径提供程序)? (What I actually would like to display is an image from a link like this one: https://is1-ssl.mzstatic.com/image/thumb/WNUBiv2P6YSklHn9eA5nlg/1000x1000bb.jpeg ) (我实际上想要显示的是来自这样一个链接的图像: https://is1-ssl.mzstatic.com/image/thumb/WNUBiv2P6YSklHn9eA5nlg/1000x1000bb.jpeg

Saving the image:保存图像:

 static Future<String> saveImage(Image image) {
    final completer = Completer<String>();
    image.image.resolve(ImageConfiguration()).addListener(ImageStreamListener((imageInfo,_) async {
      final byteData = await imageInfo.image.toByteData(format: ImageByteFormat.png);
      final pngBytes = byteData.buffer.asUint8List();
      final fileName = pngBytes.hashCode;
      final directory = await getApplicationDocumentsDirectory();
      final filePath = '${directory.path}/$fileName';
      final file = File(filePath);
      await file.writeAsBytes(pngBytes);
      completer.complete(filePath);
    }));
    return completer.future;
  }

you need to register path provider in Application.java as well.您还需要在 Application.java 中注册路径提供程序。

import io.flutter.plugins.pathprovider.PathProviderPlugin;

...

    @Override
    public void registerWith(PluginRegistry registry) {
PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"));
    }

Now that flutter uses Kotlin as the default language for android side, here is the Kotlin code:现在 flutter 使用 Kotlin 作为 android 端的默认语言,这里是 Kotlin 代码:

override fun registerWith(registry: PluginRegistry) {
    io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"))
}

This error occured because you did not registered flutter pluging in android MainAcitivity.发生此错误是因为您没有在 android MainAcitivity 中注册 flutter 插件。

Your must change onCreate method of your MainActivity as below:您必须更改MainActivityonCreate方法,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this.getFlutterEngine());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM