Code Snippet To Set The Device Wallpaper In Delphi XE7 Firemonkey On Android


Developer Todd Frankson has posted a code snippet for setting the device wallpaper in Android with Delphi XE7 Firemonkey. The original post is on the Embarcadero Forum but I have duplicated it here. Android has a class called the WallpaperManager which allows you to set the system wallpaper and the code uses this class. Additionally the code uses the Android BitmapFactory class. Todd recommends using the Java2op tool to generate the necessary files but you can also access the Object Pascal wrapper code for WallpaperManager on Github as well (and BitmapFactory). The code is pretty straightforward. A TImageViewer is used to save a JPEG out to the filesystem. The JPEG is loaded into the BitmapFactory and then the bitmap is set via the WallpaperManager. This code should also work with Appmethod. Here is the code snippet:
Here’s a way to Set the wallpaper.don’t know how right or wrong it is).
Live wallpaper is not to much different, so this is a starting point
1. Use Java2op to generate the delphi bridge files of all the wallpaper classes.
2 New Fmx poject
3. Add the units to your uses clause along with:
4. place the following on the form:
Button1: TButton;
Image1: TImageViewer;
5. Load an image into Image1 in design time. and set Button1 onclick to below.
procedure TForm1.Button1Click(Sender: TObject);
Factoryoptions: JBitmapFactory_Options;
aScreenSize: TPoint;
Widthofscreen, Heightofscreen: Integer;
{Create a filename to save the image to}
{Save the image}
{Create  JBitmap options }
{Read up on these in the android API}
Factoryoptions.inJustDecodeBounds := true;
Factoryoptions.inJustDecodeBounds := false;
{Get the wallpaper manager instance}
(TJContext.JavaClass.WALLPAPER_SERVICE) as  ILocalObject).GetObjectID);
{Load the image we saved}
{Only change the wallpaper if the Bitmap loads}
if TheBitmaptoShow <>NIL then
{Set the Wallpaper}
Head over and see the post on the Embarcadero forum for Delphi XE7 Firemonkey on Android.

Comments are closed.