如何在android中使用Flatter中的资产图像设置壁纸

如何在android中使用Flatter中的资产图像设置壁纸,android,flutter,dart,Android,Flutter,Dart,我无法在android中找到将资产图像设置为墙纸的解决方案,而我正在按照官方颤振文档ion中的记录做所有事情,在下面的图像中,设置为墙纸按钮使用方法通道并在java活动中使用本机代码,但无法将此图像设置为java活动中的墙纸。请导游。 此图像是从Flatter中的本地资源文件夹加载的 您可以使用软件包 您可以在主屏幕或锁屏中设置壁纸 墙纸可以从文件或资产 代码片段 Future<void> setWallpaperFromAsset() async { setState(()

我无法在android中找到将资产图像设置为墙纸的解决方案,而我正在按照官方颤振文档ion中的记录做所有事情,在下面的图像中,设置为墙纸按钮使用方法通道并在java活动中使用本机代码,但无法将此图像设置为java活动中的墙纸。请导游。 此图像是从Flatter中的本地资源文件夹加载的

您可以使用软件包
您可以在主屏幕或锁屏中设置壁纸
墙纸可以从
文件
资产

代码片段

Future<void> setWallpaperFromAsset() async {
    setState(() {
      _wallpaperAsset = "Loading";
    });
    String result;
    String assetPath = "assets/tmp1.jpg";
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      result = await WallpaperManager.setWallpaperFromAsset(
          assetPath, WallpaperManager.HOME_SCREEN);
    } on PlatformException {
      result = 'Failed to get wallpaper.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _wallpaperAsset = result;
    });
  }
Future setwallperfromset()异步{
设置状态(){
_“加载”;
});
字符串结果;
字符串assetPath=“assets/tmp1.jpg”;
//平台消息可能会失败,因此我们使用try/catch PlatformException。
试一试{
结果=等待WallpaperManager.SetWallperFromAsset(
assetPath、壁纸管理器、主屏幕);
}平台异常{
结果='获取墙纸失败';
}
//如果在异步平台运行时从树中删除了小部件
//消息正在传输中,我们希望放弃回复而不是呼叫
//设置state以更新我们不存在的外观。
如果(!已安装)返回;
设置状态(){
_资产=结果;
});
}
工作演示

完整代码

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:wallpaper_manager/wallpaper_manager.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _wallpaperFile = 'Unknown';
  String _wallpaperAsset = 'Unknown';

  @override
  void initState() {
    super.initState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await WallpaperManager.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> setWallpaperFromFile() async {
    setState(() {
      _wallpaperFile = "Loading";
    });
    String result;
    var file = await DefaultCacheManager().getSingleFile(
        'https://images.unsplash.com/photo-1542435503-956c469947f6');
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      result = await WallpaperManager.setWallpaperFromFile(
          file.path, WallpaperManager.HOME_SCREEN);
    } on PlatformException {
      result = 'Failed to get wallpaper.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _wallpaperFile = result;
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> setWallpaperFromAsset() async {
    setState(() {
      _wallpaperAsset = "Loading";
    });
    String result;
    String assetPath = "assets/tmp1.jpg";
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      result = await WallpaperManager.setWallpaperFromAsset(
          assetPath, WallpaperManager.HOME_SCREEN);
    } on PlatformException {
      result = 'Failed to get wallpaper.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _wallpaperAsset = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Column(
            children: <Widget>[
              RaisedButton(
                child: Text("Platform Version"),
                onPressed: initPlatformState,
              ),
              Center(
                child: Text('Running on: $_platformVersion\n'),
              ),
              RaisedButton(
                child: Text("Set wallpaper from file"),
                onPressed: setWallpaperFromFile,
              ),
              Center(
                child: Text('Wallpaper status: $_wallpaperFile\n'),
              ),
              RaisedButton(
                child: Text("Set wallpaper from asset"),
                onPressed: setWallpaperFromAsset,
              ),
              Center(
                child: Text('Wallpaper status: $_wallpaperAsset\n'),
              ),
            ],
          )),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“dart:async”;
导入“包:flifter/services.dart”;
导入“包:flatter_cache_manager/flatter_cache_manager.dart”;
导入“package:wallpaper_manager/wallpaper_manager.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
字符串_platformVersion='未知';
字符串_文件='未知';
字符串_=未知;
@凌驾
void initState(){
super.initState();
}
//平台消息是异步的,所以我们用异步方法初始化。
Future initPlatformState()异步{
字符串平台版本;
//平台消息可能会失败,因此我们使用try/catch PlatformException。
试一试{
platformVersion=等待壁纸管理器。platformVersion;
}平台异常{
platformVersion='获取平台版本失败';
}
//如果在异步平台运行时从树中删除了小部件
//消息正在传输中,我们希望放弃回复而不是呼叫
//设置state以更新我们不存在的外观。
如果(!已安装)返回;
设置状态(){
_平台版本=平台版本;
});
}
//平台消息是异步的,所以我们用异步方法初始化。
Future setwallperfromfile()异步{
设置状态(){
_壁纸文件=“正在加载”;
});
字符串结果;
var file=await DefaultCacheManager().getSingleFile(
'https://images.unsplash.com/photo-1542435503-956c469947f6');
//平台消息可能会失败,因此我们使用try/catch PlatformException。
试一试{
结果=wait WallpaperManager.setwallperfromfile(
文件路径、壁纸管理器、主屏幕);
}平台异常{
结果='获取墙纸失败';
}
//如果在异步平台运行时从树中删除了小部件
//消息正在传输中,我们希望放弃回复而不是呼叫
//设置state以更新我们不存在的外观。
如果(!已安装)返回;
设置状态(){
_文件=结果;
});
}
//平台消息是异步的,所以我们用异步方法初始化。
Future SetWallperFromAsset()异步{
设置状态(){
_“加载”;
});
字符串结果;
字符串assetPath=“assets/tmp1.jpg”;
//平台消息可能会失败,因此我们使用try/catch PlatformException。
试一试{
结果=等待WallpaperManager.SetWallperFromAsset(
assetPath、壁纸管理器、主屏幕);
}平台异常{
结果='获取墙纸失败';
}
//如果在异步平台运行时从树中删除了小部件
//消息正在传输中,我们希望放弃回复而不是呼叫
//设置state以更新我们不存在的外观。
如果(!已安装)返回;
设置状态(){
_资产=结果;
});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:const Text(“插件示例应用程序”),
),
正文:专栏(
儿童:[
升起的按钮(
子:文本(“平台版本”),
onPressed:initPlatformState,
),
居中(
子项:文本('运行于:$\u platformVersion\n'),
),
升起的按钮(
子项:文本(“从文件设置壁纸”),
onPressed:setwallperfromfile,
),
居中(
子项:文本('墙纸状态:$\u墙纸文件\n'),
),
升起的按钮(
子项:文本(“从资源设置墙纸”),
onPressed:SetWallperFromAsset,
),
居中(
子项:文本('Wallpaper status:$\u wallparasset\n'),
),
],
)),
);
}
}

谢谢chunhunghan,您的回答对我所说的问题非常有帮助,从您的代码中可以看出,我在setwallperfromsete()异步中出错,无法加载资产映像,同时可以完美地处理网络映像。