Link to home
Start Free TrialLog in
Avatar of Anna Kirova
Anna Kirova

asked on

How to Get Images From Server using App Tethering

I've 2 Apps Let's call Server And Client.

I'm using Delphi-xe8. App ->Multi-Device Application

In Both Side using: App tethering[tManager,tAProfile], SQLite Database.

In Server SQLite Database I've 6 images. I would like to View that images In Client Side.

In Client Side I've 6 [TImage].

When I Click Button 'Get Image List' I'm getting 6 images with the same view.

I would like 6 images view differently.->[Get From Server Database]

Client "Get Image List" button Code:

procedure TForm1.GetImgLstClick(Sender: TObject);
begin
  tAProfile.SendString(tManager.RemoteProfiles.First,'GetImages','');
end;

Server Received Code:

procedure TForm2.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    MS1:TMemorystream;
begin

     if AResource.Hint='GetImages' then
       begin
        MS1:=TMemorystream.Create;

         rQuery.Close;
         rQuery.SQL.Clear;
         rQuery.SQL.Add('select image from users');
         rQuery.Open;
         while not rQuery.Eof do
          begin
            tblobField(rQuery.FieldByName('image')).SaveToStream(MS1);
            Image1.Bitmap:=nil;
            rQuery.Next;
          end;
      tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImages',MS1);
       end;
end;

Client Received Code:

procedure TForm1.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
 var
  MS:TMemoryStream;
begin
 if AResource.Hint='SendImages' then
    begin
      Image1.Bitmap.LoadFromStream(AResource.Value.AsStream);
      Image2.Bitmap.LoadFromStream(AResource.Value.AsStream);
      Image3.Bitmap.LoadFromStream(AResource.Value.AsStream);
      Image4.Bitmap.LoadFromStream(AResource.Value.AsStream);
      Image5.Bitmap.LoadFromStream(AResource.Value.AsStream);
      Image6.Bitmap.LoadFromStream(AResource.Value.AsStream);
    end;
end;
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

so, send index of image too....
...
 tAProfile.SendString(tManager.RemoteProfiles.First,'GetImage_1','');
 tAProfile.SendString(tManager.RemoteProfiles.First,'GetImage_2','');
 tAProfile.SendString(tManager.RemoteProfiles.First,'GetImage_3','');
 tAProfile.SendString(tManager.RemoteProfiles.First,'GetImage_4','');
...

if AResource.Hint='GetImage_1' then
begin
....
   tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImage_1',MS1);
end;
if AResource.Hint='GetImage_2' then
begin
....
   tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImage_1',MS1);
end;

Open in new window


in client ..
...
  if AResource.Hint='SendImage_1' then
  begin
...
end;
...

Open in new window

Avatar of Anna Kirova
Anna Kirova

ASKER

what if There are 10000 images ???
This is not solution! I need Splite Stream . But I don't know How??? as an example http://stackoverflow.com/questions/42056070/how-to-split-string-by-splitstring-or-delimiter I now how to split String but I don't know Stream ;((((((((((
You don't need split stream... I put example for just showing my idea. So, why to load 1000 images at the same time.
Send image with index as I show... (little precise example...):
...
tAProfile.SendString(tManager.RemoteProfiles.First,'GetImage_1','');
...
tAProfile.SendString(tManager.RemoteProfiles.First,'GetImage_999','');
...

Open in new window


procedure TForm2.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    MS1:TMemorystream;
    sImageIdx: String;
    i: Integer;
begin
     sImageIdx:='';
      if Copy(AResource.Hint,1,Length('GetImage_'))='GetImage_' then 
      begin
         i := Pos('_',AResource.Hint);
         sImageIdx:= Copy(AResource.Hint, i+1, Length(AResource.Hint)-i);
      end;

     if Length(sImageIdx)>0 then
       begin
        MS1:=TMemorystream.Create;

         rQuery.Close;
         rQuery.SQL.Clear;
         rQuery.SQL.Add('select image from users where xxxx='+sImageIdx);
         rQuery.Open;
         while not rQuery.Eof do
          begin
            tblobField(rQuery.FieldByName('image')).SaveToStream(MS1);
            Image1.Bitmap:=nil;
            rQuery.Next;
          end;
      tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImage_'+sImageIdx,MS1);
       end;
end;

Open in new window

.... <continued>... so, please load only few images at the time, all other when they needed ... (when list scroll down... and user does not have image yet)
Ah.... you open another/same question ....
Instead you loose your energy on new question - you should try to understand whole idea ...

Client side code:
procedure TForm1.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
 var
  MS:TMemoryStream;
  sImageIdx: String;
   i: Integer; 
begin
sImageIdx:='';
      if Copy(AResource.Hint,1,Length('SendImage_'))='SendImage_' then 
      begin
         i := Pos('_',AResource.Hint);
         sImageIdx:= Copy(AResource.Hint, i+1, Length(AResource.Hint)-i);
      end;

     if Length(sImageIdx)>0 then //1 or 2 or ....
       begin
         if sImageIdx='1' then
           Image1.Bitmap.LoadFromStream(AResource.Value.AsStream);
         if sImageIdx='2' then
           Image2.Bitmap.LoadFromStream(AResource.Value.AsStream);
....
    end;
end; 

Open in new window


you can even implement one sending command ...
...
tAProfile.SendString(tManager.RemoteProfiles.First,'GetImages','');
...

Open in new window


...then you can translate this into several sending commands...
procedure TForm2.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    MS1:TMemorystream;
    sImageIdx: String;
    i: Integer;
begin
     sImageIdx:='';
      if Copy(AResource.Hint,1,Length('GetImage_'))='GetImage_' then 
      begin
         i := Pos('_',AResource.Hint);
         sImageIdx:= Copy(AResource.Hint, i+1, Length(AResource.Hint)-i);

        if Length(sImageIdx)>0 then
       begin
        MS1:=TMemorystream.Create;

         rQuery.Close;
         rQuery.SQL.Clear;
         rQuery.SQL.Add('select image from users where xxxx='+sImageIdx);
         rQuery.Open;
         while not rQuery.Eof do
          begin
            tblobField(rQuery.FieldByName('image')).SaveToStream(MS1);
            Image1.Bitmap:=nil;
            rQuery.Next;
          end;
      tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImage_'+sImageIdx,MS1);
       end;
      end
else  if AResource.Hint='GetImages' then 
      begin
        MS1:=TMemorystream.Create;

         rQuery.Close;
         rQuery.SQL.Clear;
         rQuery.SQL.Add('select image from users');
         rQuery.Open;
        i := 0; 
         while not rQuery.Eof do
          begin
            Inc(i);
            MS1.Clear;
            tblobField(rQuery.FieldByName('image')).SaveToStream(MS1);
            Image1.Bitmap:=nil;
            tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImage_'+IntToStr(i),MS1); //send each image in a  separate cmd ...

            rQuery.Next;
          end;      
       end;    
end;

Open in new window

Actually, I'm new to Programming. it seems difficult to me . 1 ? How to Get All Images From Server Database to Client Database Directly by one click?

IF I Click Client App Click Btn, I should get All images From Server SQLite Database to Client SQLite Database.
As I show in my example - when detect GetImages command - then go into loop in a resulting set (from DB) - this is mainly copy of your code - but I force to send for each row - new command - which is recognize on client part (upper modified code).
I need easiest way  plz!
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
@Sinisa Vuk Thank you very much!