Loading a blob field into a ListBox

1k Views Asked by At

I had a question answered earlier in the week and I need to take things a stage further I am using XE7 and i have a problem loading a blob field (Image.jpg) from a table into a ListBox or even into a variable which I can later enter to the Listbox. My code which works fine for the string fields is below.

// draw address line and postcode to listbox
ListBox2.Clear;
ListBox2.BeginUpdate;
// Read From Database to Listbox
FDQueryUpdate.Close;
FDQueryUpdate.SQL.Text := 'SELECT Address1, Postcode, Photo FROM Address';
try
  FDQueryUpdate.Open;
  Item := TlistBoxItem.Create(ListBox2);
  while not FDQueryUpdate.Eof do
  begin
  // create and format listbox to show bottomdetail
    Item := TlistBoxItem.Create(ListBox2);
    Item.StyleLookup := 'listboxitembottomdetail';
    // draw address to text part and postcode to bottom detail of Listbox item
    Item.Text := (FDQueryUpdate.Fields[0].AsString);
    Item.ItemData.Detail := (FDQueryUpdate.Fields[1].AsString);

    TBlobField(FDQueryUpdate.FieldByName('Photo')).SaveToFile('c:\sample_2.jpg');
    Item.ItemData.Bitmap.LoadFromFile('c:\sample_2.jpg');

    ListBox2.AddObject( Item );
    FDQueryUpdate.Next;
  end;
finally
ListBox2.EndUpdate;
FDQueryUpdate.Close;
end;

Have been working on this and I can now get the program to work in Windows by saving the DBase Blob as an Image file on disc, then reloading it from disc, resulting in the sample_2.jpg picture being added to the Listbox.

However this will not work in Android as I cannot save the file anywhere, therefore I must find some other place to hold the image file

1

There are 1 best solutions below

2
On

Managed to crack my problem, see my code below using a blob stream:

var
  Stream: TStream;

// draw address line and postcode to listbox
ListBox2.Clear;
ListBox2.BeginUpdate;
// Read From Database to Listbox
FDQueryUpdate.Close;
FDQueryUpdate.SQL.Text := 'SELECT Address1, Postcode, Photo FROM Address';
try
  FDQueryUpdate.Open;
  Item := TlistBoxItem.Create(ListBox2);
  while not FDQueryUpdate.Eof do
  begin
    // create and format listbox to show bottomdetail
    Item := TlistBoxItem.Create(ListBox2);
    Item.StyleLookup := 'listboxitembottomdetail';
    // draw address to text part and postcode to bottom detail of Listbox item
    Item.Text := (FDQueryUpdate.Fields[0].AsString);
    Item.ItemData.Detail := (FDQueryUpdate.Fields[1].AsString);
    // prefer using blob stream this way
    Stream := FDQueryUpdate.CreateBlobStream(FDQueryUpdate.FieldByName('Photo'), bmRead);
    try
      Item.ItemData.Bitmap.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;

    ListBox2.AddObject(Item);
    FDQueryUpdate.Next;
  end;
finally
  ListBox2.EndUpdate;
  FDQueryUpdate.Close;
end;

However all this throws up another problem which I shall ask as a seperate question, just follow me to my next question.