gpt4 book ai didi

dart - 如何在 flutter 中将图像设置为墙纸?

转载 作者:IT王子 更新时间:2023-10-29 06:44:13 26 4
gpt4 key购买 nike

我对如何在 flutter 中将图像设置为墙纸感到困惑,我尝试使用下面引用自 here 的代码!

我的 flutter 代码:在此代码中,我尝试从 URL 制作文件

//get URL image and save it and create a file
_setWallp(String imageUrl) async {


try {
var httpClient = http.Client();
var list = await httpClient.readBytes(imageUrl);


final tempDir = await getTemporaryDirectory();
final file = await new Io.File('${tempDir.path}/image.jpg').create();
file.writeAsBytes(list);

final channel = const MethodChannel('setwallpaper');
channel.invokeMethod('shareFile', 'image.jpg');
} catch (e) {
print('Share Error :$e');
}
}

我的java代码:在此 java 代码中,我尝试将位图设置为我在 flutter 中创建的文件路径中的墙纸

//call method channel from flutter
private static final String SHARE_CHANNEL = "setwallpaper";

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

new MethodChannel(this.getFlutterView(), SHARE_CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
public final void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("shareFile")) {
shareFile((String) methodCall.arguments);
}
}
});
}


private void shareFile(String path) {
File imgFile = new File(this.getApplicationContext().getCacheDir(), path);


// set bitmap to wallpaper
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
WallpaperManager wm = WallpaperManager.getInstance(this);
try{
wm.setBitmap(bitmap);
}catch (IOException e){
Log.e(TAG, "shareFile: cannot set image as wallpaper",e );
}
}
}

调试控制台消息:

//message from console
E/flutter (10376): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (10376): PlatformException(error, Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference, null)
E/flutter (10376): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:547:7)
E/flutter (10376): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter (10376): <asynchronous suspension>
E/flutter (10376): #2 FullScreenState._setWallp (file:///D:/fltr/cat_wallpapers/lib/fullscreen.dart:111:15)
E/flutter (10376): <asynchronous suspension>
E/flutter (10376): #3 FullScreenState.build.<anonymous closure> (file:///D:/fltr/cat_wallpapers/lib/fullscreen.dart:71:31)
E/flutter (10376): #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
E/flutter (10376): #5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:549:30)
E/flutter (10376): #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (10376): #7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter (10376): #8 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
E/flutter (10376): #9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter (10376): #10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (10376): #11 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (10376): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter (10376): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (10376): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (10376): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (10376): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (10376): #17 _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter (10376): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
D/EGL_emulation(10376): eglMakeCurrent: 0xb429e760: ver 2 0
I/FlutterActivityDelegate(10376): onResume setting current activity to this
D/EGL_emulation(10376): eglCreateContext: 0xb429e820: maj 2 min 0 rcv 2
D/EGL_emulation(10376): eglMakeCurrent: 0xb429e820: ver 2 0
D/EGL_emulation(10376): eglMakeCurrent: 0xb429e760: ver 2 0
D/EGL_emulation(10376): eglMakeCurrent: 0xb429e820: ver 2 0
I/flutter (10376): Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'Future<File>' //didn't understand about this

需要帮助..谢谢

最佳答案

使用 Dio 库...确保首先下载图像并在下载后设置为墙纸

Future<Null> setWallpaper() async {
Dio dio = Dio();
try {
var dir = await getTemporaryDirectory();

await dio.download(widget.img, "${dir.path}/myimage.jpeg",
onProgress: (rec, total) {
setState(() {
downloading = true;
progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
print(progressString);
if (progressString == "100%") {
_setWallpaer();
}
});
});
} catch (e) {}

setState(() {
downloading = false;
progressString = "Completed";
});}

Future<Null> _setWallpaer() async {
String setWallpaper;
try {
final int result =
await platform.invokeMethod('setWallpaper', 'myimage.jpeg');
setWallpaper = 'Wallpaer Updated....';
} on PlatformException catch (e) {
setWallpaper = "Failed to Set Wallpaer: '${e.message}'.";
}
setState(() {
_setWallpaper = setWallpaper;
});}

如果有帮助,您可以查看完整的源代码......

https://github.com/pratiktimer/flutterwallpaper

关于dart - 如何在 flutter 中将图像设置为墙纸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com