Create Wallpaper App In Flutter(Share, Download & Set Wallpaper)

Published by Abhay Rastogi on

In this blog, we will focus on creating an abstract of wallpaper and modules required for developing it. this blog covers sharing an URL image, downloading URL image and setting the image as wallpaper of the phone.

Add dependencies in pubspec.yaml

  • image_picker_saver:
  • http:
  • esys_flutter_share:

Share Image in Flutter

  void _onImageShareButtonPressed() async {

var response = await http.get('https://codesearchonline.com/wp-content/uploads/2020/01/social-media-management-8.png');
    filePath = await ImagePickerSaver.saveFile(fileData: response.bodyBytes);
    print(filePath);
    
  BASE64_IMAGE = filePath;
   
    final ByteData bytes = await rootBundle.load(BASE64_IMAGE);
    await EsysFlutterShare.shareImage('myImageTest.png', bytes, 'my image title');
    }

Download Url Image in Flutter

void _onImagDownloadButtonPressed() async {

var response = await http.get('https://codesearchonline.com/wp-content/uploads/2020/01/social-media-management-8.png');
     filePath = await ImagePickerSaver.saveFile(fileData: response.bodyBytes);

 }

Set the URL image as wallpaper

  void _onImageWallpapButtonPressed() async {
   
     var response = await http.get('https://codesearchonline.com/wp-content/uploads/2020/01/social-media-management-8.png');
     filePath = await ImagePickerSaver.saveFile(fileData: response.bodyBytes);

 _getWallpaper();
 }
static const platform = const MethodChannel('wallpaper');
  Future<void> _getWallpaper() async {
    try {
      final int result =  await platform.invokeMethod('getWallpaper',{"text":filePath});
       _onLoading(false);
    } on PlatformException catch (e) {
      Navigator.pop(context);

    }
}

MainActivity.java

new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {
                        // TODO
                        if (call.method.equals("getWallpaper")) {
                            file = call.argument("text");
                            int setWallpaper = RandomFunction(file);
                            if (setWallpaper != -1) {
                                result.success(setWallpaper);
                            } else {
                                result.error("UNAVAILABLE", "setwallpaper", null);
                            }
                        } else {
                            result.notImplemented();
                        }
                        }
                });




  }

    int RandomFunction(String f){
        Display metrics = getWindowManager().getDefaultDisplay();
        int height = metrics.getHeight();
        int width = metrics.getWidth();
        try {
            WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(getApplicationContext());
            Bitmap mybitmap = BitmapFactory.decodeFile(f);
            Bitmap bitmap = Bitmap.createScaledBitmap(mybitmap, width, height, true);
            myWallpaperManager.setBitmap(bitmap);
            return 1;
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
            }
    }


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.