Link to home
Start Free TrialLog in
Avatar of Grant1842
Grant1842Flag for United States of America

asked on

How to load an image(Blob) from a sQlite into a timage, and a listview.

This is an Delphi MultiDevice app. Using Firedac Query and sQlite database
I am trying to load an image from a blob in an sqlite database into a ListView.
The database DDL
CREATE TABLE [Pictures](
    [PicId] INT, 
    [UsersImage] BLOB);

Open in new window


Here is my code
procedure LoadFromBlob;
var
  BlobStream: TStream;
begin
  // FireDAC.Connected := True;
  try
    AccessCameraAppForm.FDQuerySelect.Open;
    AccessCameraAppForm.FDQuerySelect.First;
    while (not AccessCameraAppForm.viewimgquery.EOF) do
    begin
      // access a stream from a blob like this
      BlobStream := AccessCameraAppForm.viewimgquery.CreateBlobStream
        (AccessCameraAppForm.viewimgquery.FieldByName('UsersImage'),
        TBlobStreamMode.bmRead);
      // access a string from a field like this
      if (AccessCameraAppForm.viewimgquery.FieldByName('PicId')
        .AsInteger = AccessCameraAppForm.FDQuery1.FieldByName('ID').AsInteger)
      then
      begin
        // load your blob stream data into a control
        AccessCameraAppForm.viewimage.Bitmap.LoadFromStream(BlobStream);
         AccessCameraAppForm.ListView2.items.Add();
        BlobStream.Free;
        AccessCameraAppForm.viewimgquery.Next;
      end;
      end;
    except
      on e: Exception do
      begin
        // ShowMessage(e.Message);
      end;
    end;
    // FireDAC.Connected := False;
  end;

Open in new window

I am useing the live bindings to bind the querys image to the listviews item.image2
The code compiles but can not load the image from the sqlite database into the listview or a timage.
Thanks for any help .
This seems to be a problem for me.
I have attached the sorcecode.
Project4.zip
Here is the database
flatrate.db3


I am useing differnt sql here.
Differnt-Sql-Project4.zip
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Grant1842

ASKER

THanks for your help.  
What does  Itm equal
itm := form4.ListView1.items.Add();
This is for accessing newly created ListViewItem. With them you can:

- change label:
itm.Text := ...

- set image index:
itm.ImageIndex := ...

and so on...
why are you mixing procedural and object oriented code ?
that's just making your life difficult

if you create a second instance of TForm4 named formXYZ like:
formXYZ := TForm4.create(application);

Open in new window


that procedure won't work.
but ... correctly :
  private
    { Private declarations }
     procedure LoadFromBlob;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.fmx}
 procedure TForm4.LoadFromBlob;

Open in new window


and then you don't need to use Form4, which possibly doesn't exist:
sample:
      if viewimgquery.FieldByName('PicId').AsInteger = FDQuery1.FieldByName('ID').AsInteger then 

Open in new window

that was just to say something about the wrong code
and yes ... confusing, sinisa got that right :)