文章目录
一、报错信息
二、问题分析
三、解决方案
一、报错信息
在 Flutter 中使用 image_picker 插件时 , 如 【Flutter】Flutter 拍照示例 ( 拍照源码示例 ) 中选择照片后 , 报如下错误 :
W/Binder (30392): Caught a RuntimeException from the binder stub implementation. W/Binder (30392): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:30392_2 W/Binder (30392): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:992) W/Binder (30392): at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:779) W/Binder (30392): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:144) W/Binder (30392): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:238) W/Binder (30392): at io.flutter.plugins.imagepicker.ImagePickerDelegate.finishWithSuccess(ImagePickerDelegate.java:540) W/Binder (30392): at io.flutter.plugins.imagepicker.ImagePickerDelegate.handleImageResult(ImagePickerDelegate.java:505) W/Binder (30392): at io.flutter.plugins.imagepicker.ImagePickerDelegate.access$000(ImagePickerDelegate.java:66) W/Binder (30392): at io.flutter.plugins.imagepicker.ImagePickerDelegate$4.onPathReady(ImagePickerDelegate.java:470) W/Binder (30392): at io.flutter.plugins.imagepicker.ImagePickerDelegate$3$1.onScanCompleted(ImagePickerDelegate.java:160) W/Binder (30392): at android.media.MediaScannerConnection$ClientProxy.onScanCompleted(MediaScannerConnection.java:204) W/Binder (30392): at android.media.MediaScannerConnection$1.scanCompleted(MediaScannerConnection.java:53) W/Binder (30392): at android.media.IMediaScannerListener$Stub.onTransact(IMediaScannerListener.java:61) W/Binder (30392): at android.os.Binder.execTransact(Binder.java:731)
二、问题分析
在 image_picker 的主页 , 已经对上述问题作出了说明 , 使用 image_picker 插件选择数据时 , 有时出现图像丢失的情况 , 此时使用 retrieveLostData 获取丢失的图像 ;
代码示例 :
Future<void> retrieveLostData() async { final LostData response = await picker.getLostData(); if (response.isEmpty) { return; } if (response.file != null) { setState(() { if (response.type == RetrieveType.video) { _handleVideo(response.file); } else { _handleImage(response.file); } }); } else { _handleError(response.exception); } }
还有一个问题就是可能使用了过时的 Flutter 插件 ;