Konuyu Oyla:
  • Derecelendirme: 0/5 - 0 oy
  • 1
  • 2
  • 3
  • 4
  • 5
Android Uygulama İkonu
#1
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?

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;
SRHT
Cevapla
#2
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;
Serhat YANALAK - FMX Software Developer
Cevapla
#3
Bende farklı bir yoldan çözüme ulaştım, teşekkür ederim.


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;
SRHT
Cevapla


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  Android SDK kajmerantime 1 318 28-10-2025, Saat: 14:53
Son Yorum: cinarbil
  Android El Terminali Barkod Okuyucu Verisi tuna 0 272 17-10-2025, Saat: 01:07
Son Yorum: tuna
  Apple Store'a Uygulama Yükleyebilen Arkadaşlarla Yardımlaşma... Jakarta2 23 3.502 04-10-2025, Saat: 16:51
Son Yorum: tavsanlili
  google play ve apple storede ücretli uygulama yayınlamak barissagir 4 678 15-09-2025, Saat: 15:03
Son Yorum: barissagir
  Uygulama üzerinden Wifi Bağlanma ? nurah 4 916 28-08-2025, Saat: 10:50
Son Yorum: nurah



Konuyu Okuyanlar: