1

Using Delphi Tokyo 10.2.3 and targeting Android, I'm creating multiple threads that download PNG images and decode them in the background.

Using TBitmap is not an option due to stability reasons explained here: Getting multi-threaded safe RGBA values from a decoded PNG image running Android

I believe PNG files can be decoded using the JBitmap/JBitmapFactory classes, but I can't find any documentation on how to do this.

My current code downloads the PNG images into a TMemoryStream. I am hoping to find sample code that will take in a TMemoryStream and output a JBitmap.

Something like this:

function DecodeBitmapFromStream(mStream : TMemoryStream) : JBitmap;
bLight
  • 803
  • 7
  • 23

1 Answers1

4

I believe PNG files can be decoded using the JBitmap/JBitmapFactory classes, but I can't find any documentation on how to do this.

In Embarcadero's JNIBridge framework, types being with J and TJ are interface/class wrappers for native Android Java types. So you need to read Google's documentation, in this case the Bitmap and BitmapFactory class references, and then adapt the property and method calls to Embarcadero's syntax as needed.

My current code downloads the PNG images into a TMemoryStream. I am hoping to find sample code that will take in a TMemoryStream and output a JBitmap.

Try something like this:

function DecodeBitmapFromStream(mStream : TMemoryStream) : JBitmap;
var
  data: TJavaArray<Byte>;
  size: Integer;
begin
  size := mStream.Size;
  data := TJavaArray<Byte>.Create(size);
  Move(mStream.Memory^, data.Data^, size);
  Result := TJBitmapFactory.JavaClass.decodeByteArray(data, 0, size, nil);
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770