Merhaba,
Delphi FMX Android ile uygulamanın "application icon"'u nasıl alırım.
Aşağıda örnek bir kod buldum, ancak uygulama çöküyor.
Aşağıdaki örnekte eksik ya da yanlış olan nedir?
Kod:
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;
Merhaba,
Delphi 13 üzerinde test ettim. çalışıyor. Bir deneyin isterseniz;
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
FMX.Types,
FMX.Controls,
FMX.Forms,
FMX.Graphics,
FMX.Dialogs,
FMX.Surfaces,
Androidapi.Helpers,
Androidapi.JNI.App,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Util,
Androidapi.JNI.OS,
Androidapi.JNI.Provider,
Androidapi.JNIBridge,
FMX.Controls.Presentation,
FMX.StdCtrls,
FMX.Objects;
function JBitmapToTBitmap(JBM: JBitmap): TBitmap;
var
OutputStream: JByteArrayOutputStream;
Bytes: TJavaArray<Byte>;
Stream: TMemoryStream;
begin
Result := TBitmap.Create;
OutputStream := TJByteArrayOutputStream.Create;
JBM.compress(TJBitmap_CompressFormat.JavaClass.PNG, 100, OutputStream);
Bytes := OutputStream.toByteArray;
Stream := TMemoryStream.Create;
try
Stream.WriteBuffer(Bytes.Data^, Bytes.Length);
Stream.Position := 0;
Result.LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
function GetAppIcon: TBitmap;
var
Activity: JActivity;
PM: JPackageManager;
AppInfo: JApplicationInfo;
IconDrawable: JDrawable;
BitmapDrawable: JBitmapDrawable;
JBM: JBitmap;
begin
Activity := TAndroidHelper.Activity;
PM := Activity.getPackageManager;
AppInfo := Activity.getApplicationInfo;
IconDrawable := PM.getApplicationIcon(AppInfo);
if IconDrawable = nil then
begin
Result := nil;
Exit;
end;
// Drawable → JBitmap
if (IconDrawable.getClass.getName.equals(StringToJString('android.graphics.drawable.BitmapDrawable'))) then
begin
BitmapDrawable := TJBitmapDrawable.Wrap((IconDrawable as ILocalObject).GetObjectID);
JBM := BitmapDrawable.getBitmap;
end
else
begin
JBM := TJBitmap.JavaClass.createBitmap(
IconDrawable.getIntrinsicWidth,
IconDrawable.getIntrinsicHeight,
TJBitmap_Config.JavaClass.ARGB_8888
);
IconDrawable.setBounds(0, 0, JBM.getWidth, JBM.getHeight);
IconDrawable.draw(TJCanvas.JavaClass.init(JBM));
end;
Result := JBitmapToTBitmap(JBM);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Bitmap := GetAppIcon;
end;
Bende farklı bir yoldan çözüme ulaştım, teşekkür ederim.
Kod:
function GetAppIcon(Dest: TBitmap): Boolean;
var
Activity: JActivity;
PackageManager: JPackageManager;
ApplicationInfo: JApplicationInfo;
Drawable: JDrawable;
Bitmap: JBitmap;
Canvas: JCanvas;
Surface: TBitmapSurface;
Width, Height: Integer;
begin
Result := False;
try
Activity := SharedActivity;
if Activity = nil then Exit;
PackageManager := Activity.getPackageManager;
ApplicationInfo := Activity.getApplicationInfo;
Drawable := PackageManager.getApplicationIcon(ApplicationInfo);
if Drawable = nil then Exit;
Width := Drawable.getIntrinsicWidth;
Height := Drawable.getIntrinsicHeight;
if (Width <= 0) or (Height <= 0) then
begin
Width := 192; // Varsayılan adaptive ikon boyutu (dp)
Height := 192;
end;
Bitmap := TJBitmap.JavaClass.createBitmap(Width, Height, TJBitmap_Config.JavaClass.ARGB_8888);
Canvas := TJCanvas.JavaClass.init(Bitmap);
Drawable.setBounds(0, 0, Canvas.getWidth, Canvas.getHeight);
Drawable.draw(Canvas);
Surface := TBitmapSurface.Create;
try
if not JBitmapToSurface(Bitmap, Surface) then
Exit;
Dest.Assign(Surface);
finally
Surface.Free;
end;
Bitmap.recycle;
Result := True;
except
end;
end;