Loading the app’s icon into a TImage on Android

Working with Delphi for Android, I wanted to load my app’s icon into a TImage. Here’s the code I came up with:

uses
  AndroidApi.JniBridge, AndroidApi.Jni.App, AndroidApi.Jni.GraphicsContentViewText,
  FMX.Helpers.Android, FMX.Surfaces;

function GetAppIcon(Dest: TBitmap): Boolean;
var
  Activity: JActivity;
  Drawable: JDrawable;
  Bitmap: JBitmap;
  Surface: TBitmapSurface;
begin
  Result := False;
  Activity := SharedActivity;
  Drawable := Activity.getPackageManager.getApplicationIcon(Activity.getApplicationInfo);
  Bitmap := TJBitmapDrawable.Wrap((Drawable as ILocalObject).GetObjectID).getBitmap;
  Surface := TBitmapSurface.Create;
  try
    if not JBitmapToSurface(Bitmap, Surface) then
      Exit;
    Dest.Assign(Surface);
  finally
    Surface.Free;
  end;
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GetAppIcon(Image1.MultiResBitmap[0].Bitmap);
end;

The slightly awkward manner of casting from a JDrawable to a JBitmapDrawable in the middle is because Java type casts are not supported directly by the Delphi to Java bridge. As such, we have to get a handle to the raw Java object and re-wrap it to the desired derived class… but that’s too much of a faff all told, and the syntax will be familiar with anyone who has used the Delphi to Objective-C bridge before.

Native Android controls for XE5

Just a quick post to say Babak Yaghoobi – he of the open source native iOS control set – is coming up with the goods for Android too. The project – ‘D.P.F Delphi Android Native Components’ – is on SourceForge (http://sourceforge.net/projects/dpfdelphiandroid/). While it doesn’t look quite finished yet, he has made decent progress, so if you have XE5, it’s well worth a look.

A few XE5-related bits

With XE5 now out, I’ve updated my FMX TClipboard and Mac PDF writer code to compile with it, and in the case of the latter, also made an unrelated fix suggested by Sebastian Zierer. With respect to TClipboard, I must add the caveat that the Android implementation merely delegates to the standard, frankly half-arsed IFMXClipboardService, and as such, only supports text. This is because my investigations into the Android API suggest there is no standard way to exchange pictures over the Android clipboard – if anyone knows something to the contrary, by all means let me know in the comments though. Anyhow, if you want to browse the code, check it out here: http://code.google.com/p/delphi-foundations/source/browse/#svn%2Ftrunk%2FFMX%20Utilities Alternatively, the SVN link is http://delphi-foundations.googlecode.com/svn/trunk/FMX%20Utilities/ I have also just posted to the same place a TCustomIniFile descendant that delegates to the Android SharedPreferences API, which I have unimaginatively called TAndroidPreferencesIniFile (the unit is CCR.PrefsIniFile.Android.pas). This class corresponds to the TMacPreferencesIniFile TApplePreferencesIniFile class I had written previously, which sits on top of the CFPreferences API on OS X and iOS. The idea in both cases is to have classes that roughly correspond to TRegistryIniFile on Windows, providing a consistent interface over whatever is the native preferences store for the platform. Be warned that unlike TIniFile (but like TApplePreferencesIniFile), TAndroidPreferencesIniFile is case sensitive with respect to section and key names. Further, there’s also a slightly annoying issue in which each time an app is deloyed by the IDE, existing preference data gets wiped in the process. Given when you run an Android app from the IDE you see a message saying it is uninstalling the previous version of the app, I imagine the preference wiping is just a function of that. Anyhow, accompanying the main unit is a small (and very crude!) demo designed for a 7″ tablet (a Nook HD in my case): TAndroidPreferencesIniFile demo The demo works best if you write a series of values first, given the third edit box doubles as both the entry field for values to write and default values to read.

Update (17/9/13): originally the unit was called CCR.Android.PrefsIniFile.pas; it’s now CCR.PrefsIniFile.Android.pas for consistency with the naming pattern used by my TClipboard units. I’ve also now added a ‘mobile’ version of the demo which uses TAndroidPreferencesIniFile when targeting Android, TApplePreferencesIniFile when targeting iOS, and TRegistryIniFile when targeting Windows (Delphi provides the Windows option in a mobile project to allow ‘quick and dirty’ debugging).

Update (25/6/15): current version now at http://github.com/chrisrolliston/CCR.PrefsIniFile