
If you’re using the FireDAC components that comes with Delphi XE5 Firemonkey and you want to save a bitmap or some other type of data to a blog field on your embedded database (either SQLite, IBlite, or Interbase To Go) it can be a bit tricky. For saving it is pretty standard fair with the TFDDataSet.LoadFromStream() which will load a stream like a memory stream into a field. The important piece to reading the blob back out is is the TFDDataSet.CreateBlobStream() function. I have included two functions below which show an example of how to read and write a string and bitmap data from an image using a FireDAC TFDQuery component. This method of reading and writing a blog works on Android and IOS as well as Windows and OSX.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
procedure SaveToFireDACBlob; var MemStream: TMemoryStream; begin FireDAC.Connected := True; MemStream := TMemoryStream.Create; try Image1.Bitmap.SaveToStream(MemStream); MemStream.Seek(0,0); FDQueryInsert.ParamByName('Media').LoadFromStream(MemStream,ftBlob); FDQueryInsert.ParamByName('MType').AsString := '0'; FDQueryInsert.ExecSQL(); except on e: Exception do begin //ShowMessage(e.Message); end; end; MemStream.Free; FireDAC.Connected := False; end; procedure LoadFromFireDACBlob; var BlobStream: TStream; begin FireDAC.Connected := True; try FDQuerySelect.Open; FDQuerySelect.First; while(not FDQuerySelect.EOF)do begin // access a stream from a blob like this BlobStream := FDQuerySelect.CreateBlobStream(FDQuerySelect.FieldByName('Media'),TBlobStreamMode.bmRead); // access a string from a field like this if (FDQuerySelect.FieldByName('MType').AsString='0') then begin // load your blob stream data into a control ImageViewer.Bitmap.LoadFromStream(BlobStream); end; BlobStream.Free; FDQuerySelect.Next; end; except on e: Exception do begin //ShowMessage(e.Message); end; end; FireDAC.Connected := False; end; |
Hi.
Great info.
But what about using ParamByName.AsBlob (for single param) or AsBlobs[X] for multiple params in ie. ArrayDML?
Regards
Bjarne
If you want to provide a demo or sample which shows the ParamByName.AsBlob way I will post it here 🙂
I cobbled this code together from different StackOverflow answers from probably previous versions of Firemonkey.
Hi.
Best to show the documentation 🙂
http://docs.embarcadero.com/products/rad_studio/firedac/frames.html?frmname=topic&frmfile=%21%21NAVID_Array_DML_ST%283%29_SEC_Example_2.html
and with that modify to use it with blob statements.
But so far I haven’t had much success in using AsBlobs[X] with something that is binary(ie. jpg picture).
I got an answer from Dmitry 🙂
// here “i” is array index
Params[…].LoadFromFile(‘image.jpg’, ftBlob, i);
Similar with LoadFromStream, where last parameter is also array index.
thread is here:
https://forums.embarcadero.com/thread.jspa?messageID=642145#642145
Hope this can help others.
Nice! I need to retrieve an image (blob field) stored on a mysql database, and put each image for each item on a ListBox, on Delphi XE6. When i run the executable, it shows an error message : “Loading bitmap failed”.
My code :
BlobStream := myQyery.CreateBlobStream(myQuery.FieldByName(‘photo’),TBlobStreamMode.bmRead);
ListBoxItem.ItemData.Bitmap.LoadFromStream(BlobStream);
BlobStream.Free;
Best regards from Brasil!
Hello,
Is it possible open blob (.pdf, .xls) in fmx android aplication? My restfull return blob from database!