Work on a project and I want to save the images I downloadin a blob field. I use idHTTP with success but UI freezes.
I create a procedure to download images from URL using ImageLoader method (Image Loader page). Everything is OK with the images but I cannot save them in the specific field in DB.
How can I do it?
Here is my code :
procedure TForm1.LoadImages;
var
i: integer;
IMLO: TImageLoader;
IM: TImage;
Stream: TMemoryStream;
imgAddress: string;
begin
Table1.First;
for i := 0 to Table1.RecordCount-1 do
begin
imgAddress := Table1.FieldByName('URL').AsString;
Stream := TMemoryStream.Create;
IM:= TImage.Create(self);
IM.Name := 'IM'+inttostr(i);
IM.Parent := HorzScrollBox1;
IM.Position.X := i * 320;
IM.Align:= TAlignLayout.alLeft;
IM.Width := 300;
IMLO.LoadImage(IM, imgAddress);
{ Table1.Edit;
TBlobField(Table1.FieldByName('image')).LoadFromStream(Stream);
Table1.Post;
Stream.Free; }
Table1.Next;
end;
end;
The code works perfectly if I want just to show images. How can I use stream to save them?
Problem
The problem is that the TImageLoader.LoadImage loads the data from the net asynchronously.
Because of that the data is not yet available at the time when you're loading the data.
Simple solution
In order to stop TImageLoader from doing that you can add an extra method to it and call that one instead.
The other problem is that you are loading the Blob with the Stream data, but you never load the stream with anything.
Luckily you don't need the stream.
function TImageLoader.LoadImageNow(const AImage: TImage; const AImageURL: string): boolean;
const
success = true;
failure = false;
var
MS : TMemoryStream;
Image: TImage;
begin
Result:= success;
MS := TMemoryStream.Create;
Image:= TImage.Create;
try
IdHTTP1.get(AImageURL,MS);
Ms.Seek(0,soFromBeginning);
Image.LoadFromStream(MS);
AImage.Picture.Assign(Image);
except
Result:= failure;
end;
FreeAndNil(Image);
FreeAndNil(MS);
end;
Now the loader will only return when the image is loaded.
This also explains why you get an exception upon freeing IM.
You're freeing IM, but the loader is still using it in the background.
That's the problem with using multi-threaded applications, you need to keep track of what the other threads are doing.
The code for your loop will now look like this:
procedure TForm1.LoadImages;
var
i: integer;
IMLO: TImageLoader;
IM: TImage;
imgAddress: string;
FieldImage: TGraphicField;
FieldUrl: TField;
begin
FieldImage:= TGraphicField(Table1.FieldByName('image'));
FieldUrl:= Table1.FieldByName('URL');
Table1.First;
for i := 0 to Table1.RecordCount-1 do try
imgAddress := FieldUrl.AsString;
IM:= TImage.Create(self);
IM.Name := 'IM'+inttostr(i);
IM.Parent := HorzScrollBox1;
IM.Position.X := i * 320;
IM.Align:= TAlignLayout.alLeft;
IM.Width := 300;
if IMLO.LoadImageNow(IM, imgAddress) then begin
Table1.Edit;
FieldImage.Assign(IM.Picture);
Table1.Post;
end; {if}
finally
IM.Free;
Stream.Free;
Table1.Next;
end; {for .. try}
end;
Because FieldByName is slow it is best to pull it out of a loop (it probably will not matter because of the slowness of LoadImageNow).
See: http://docwiki.embarcadero.com/Libraries/XE5/en/Data.DB.TBlobField.Assign
And: http://docwiki.embarcadero.com/CodeExamples/XE3/en/HTTP_Get_%28Delphi%29
Other option
Add an event to the thread that you can hook into every time a image finishes loading.
In the event handler you can then display the image and put it into the database.
In fact the async loader may already have an option for this, but I do not know that codebase well enough to answer that question.
Related
First of all, I know a little bit about restarting an app. but that's when it is for windows. In this case, I need to make this for an app that is from android. I couldn't find a solution for it that works in Delphi. Just found this from #Mihai Limbășan I quote:
Perhaps you should think outside the box. Instead of futzing with the
mutex/instance logic, you could simply create another executable that
waits for your app to close then starts it again. As an added bonus,
you can later use this mechanism to, for example, update some of your
main app's binaries. It's also much easier to run it elevated instead
of maintaining different integrity levels inside the same app, etc.
But have no idea how this works or even where to start...
Every tip, code sample, or maybe other solution to restart an app will be appreciated.
EDIT
after some questions here are some pieces of code from the procedure.
First.
after you choose for example the language 'English and push the button save this happens
Inifile := TIniFile.Create(fPath);
try
Inifile.WriteString('Instelling','ip',edit5.text);
Inifile.WriteString('Instelling','user',edit6.text);
Inifile.WriteString('Instelling','pixels',edit3.text);
Inifile.WriteInteger('Instelling','language',Combobox2.ItemIndex);
fGebruiker := Edit6.Text;
fFotoformaat := StrToInt(edit3.Text);
finally
FDConnection1.Params.Values['server']:=edit5.Text;
FDConnection1.Connected := True;
inifile.free;
End;
with this code, I fill an inifile with data as you see also the item index of the Combobox for the language.
on this point i restart the app manually so the right language is chosen by this code:
procedure TfmMain.FormShow(Sender: TObject);
VAR
param : string;
inifile : tInifile;
begin
if (System.SysUtils.fileexists(fPath)) then
Begin
begin
Inifile := TIniFile.Create(fPath);
try
if not (Inifile.ReadString('Instelling','ip','default')='default') and not (Inifile.ReadString('Instelling','Gebruiker','default')='default')then
try
edit5.text := Inifile.ReadString('Instelling','ip','default');
edit6.text := Inifile.ReadString('Instelling','user','default');
Edit3.text := Inifile.ReadString('Instelling','pixel','400');
combobox2.ItemIndex := IniFile.ReadInteger('Instelling','language',1);
fpixel:= StrToInt(edit3.Text);
fuser:=edit6.text;
FDConnection1.Params.Values['server']:=edit5.Text;
taal := 'NL';
//find language settings
if combobox2.ItemIndex=0 then
begin
language:= 'NL'
end;
if combobox2.ItemIndex=1 then
begin
language:= 'ENG';
end;
if language='ENG' then
begin
vertalerENG.vertaler('ENG');
end;
end;
end;
end;
end;
the VertalerENG is a function that is fired if the language parameter is ENG and change all of the captions to English.
the problem is that nothing is changed till i restart the app.
If you want restart app programmatically,
This code work fine for me and you can set time elapse before restart app
uses
Androidapi.Helpers,Androidapi.JNI.GraphicsContentViewText,Androidapi.JNI.App,
System.DateUtils;
...
procedure RestartApp;
{$IFDEF ANDROID}
var LPM : JPackageManager;
LIntent_Start : JIntent;
LPendingIntent : JPendingIntent;
LMS : Int64;
{$ENDIF}
begin
{$IFDEF ANDROID}
LPM := TAndroidHelper.Context.getPackageManager();
LIntent_Start := LPM.getLaunchIntentForPackage(
TAndroidHelper.Context.getPackageName()
);
LIntent_Start.addFlags( TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP );
LPendingIntent := TJPendingIntent.JavaClass.getActivity(
TAndroidHelper.Context,
223344 {RequestCode},
LIntent_Start,
TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
);
LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000;
TAndroidHelper.AlarmManager.&set(
TJAlarmManager.JavaClass.RTC,
LMS + 10000,
LPendingIntent
);
// TAndroidHelper.Activity.finish();
Application.Terminate;
{$ENDIF }
end;
If changing the language is your only concern then i would suggest changing the locale of the application. You only need to restart the activity if you're using all the strings correctly from strings.xml
You can see it here how to change the locale of application programatically.
Change app language programmatically in Android
I'm trying to iterate though a TClientDataSet and refresh a TLabel and a TProgressBar in Android, but I get this error. How can I fix it?
This is the execute procedure. is the first time i'm working wuth multithreads in Delphi and i would like to know about that.
I hope you can help me.
procedure TThreadCatalogos.Execute;
var i : Integer;
AppPath : string;
begin
AppPath := System.IOUtils.TPath.GetPublicPath;
ProgressBar.Min := 0;
for i := round(ProgressBar.Min) to round(ProgressBar.Max) do begin
// check if Self(thread) is terminated, if so exit
if Terminated then
Exit;
Position := i;
{*******************************************}
Conexion.Open;
//CLIENTES
dsClientes.Open;
//mtClientes.EmptyDataSet;
dsClientes.First;
ProgressBar.Max := dsClientes.RecordCount;
while not dsClientes.Eof do
begin
if not mtClientes.Locate('nombre',dsClientes.FieldByName('nombre').AsString,[]) then
begin
Synchronize(procedure()
begin
mtClientes.Insert;
mtClientes.Fields[0].Value := dsClientes.FieldByName('cliente_id').Asinteger;
mtClientes.Fields[1].Value := dsClientes.FieldByName('nombre').AsString;
mtClientes.Fields[2].Value := dsClientes.FieldByName('tipo').AsString;
mtClientes.Post;
mtClientes.SaveToFile(System.IOUtils.TPath.combine(AppPath,'CLIENTES.bin'),sfBinary);
lbl.Text := 'Cliente '+floattostr(ProgressBar.Value)+' de '+floattostr(ProgressBar.Max);
ProgressBar.Value := ProgressBar.Value + 1;
end);
dsClientes.Next;
end;
//mtClientes.SaveToFile(System.IOUtils.TPath.combine(AppPath,'CLIENTES.xml'),sfXML);
mtClientes.First;
end); Exit;
end;
{************************************************}
end;
end;
To update a progress bar in a main thread from a child thread one approach is to:
Use atomically updateable global variable(s), like a 32 bit integer that you update in the child thread.
Use a TTimer event on the form that updates the progress bar based off the values in the global variable(s).
This prevents updating the progress bar too often and lets the thread do a very quick updates to progress.
I've created a simple FireMonkey screenshot capture that works fine on Android and Windows..:
procedure Capture;
var
B : TBitmap;
begin
try
B := Layout1.MakeScreenshot;
try
B.SaveToFile( ..somefilepath.png );
finally
B.Free;
end;
except
on E:Exception do
ShowMessage( E.Message );
end;
end;
end;
when I moved it to a thread as below, it works fine in Windows but in Android I get the exception 'bitmap too big' from the call to MakeScreenshot. Are there additional steps needed to employ MakeScreenshot in a thread?
procedure ScreenCaptureUsingThread;
begin
TThread.CreateAnonymousThread(
procedure ()
var
B : TBitmap;
begin
try
B := Layout1.MakeScreenshot;
try
B.SaveToFile( '...somefilepath.png' );
finally
B.Free;
end;
except
on E:Exception do
TThread.Synchronize( nil,
procedure ()
begin
ShowMessage( E.Message );
end );
end).Start;
end;
Later addition. Based on suggestions by Sir Rufo and Sebastian Z, this solved the problem and allowed use of a thread:
procedure Capture;
begin
TThread.CreateAnonymousThread(
procedure ()
var
S : string;
B : TBitmap;
begin
try
// Make a file name and path for the screen shot
S := '...SomePathAndFilename.png';
try
// Take the screenshot
TThread.Synchronize( nil,
procedure ()
begin
B := Layout1.MakeScreenshot;
// Show an animation to indicate success
SomeAnimation.Start;
end );
B.SaveToFile( S );
finally
B.Free;
end;
except
on E:Exception do
begin
TThread.Synchronize( nil,
procedure ()
begin
ShowMessage( E.Message );
end );
end;
end;
end).Start;
end;
MakeScreenShot is not thread safe, so you cannot safely use it in a thread. If this works in Windows then I'd say you were just lucky. I'd suggest that you take the screenshot outside of the thread and only use the thread for saving the screenshot to png. Painting should be fast while encoding to png needs a lot more resources. So you should still benefit pretty much from the thread.
I want to list all available raw sensor data in a Memo for Android.
Following code worked over the past years, but it doesn't work with XE8. There is probably an internal compiler bug. Is there anything I can do to make it work again, or is there an alternative solution?
uses
TypInfo;
type
TOrientationSensorAccessor = class(TCustomOrientationSensor);
TLocationSensorAccessor = class(TCustomLocationSensor);
procedure TForm2.Button1Click(Sender: TObject);
var
p_location: TCustomLocationSensor.TProperty;
p_orientation: TCustomOrientationSensor.TProperty;
n, v: string;
begin
Memo1.Lines.Clear;
if Assigned(OrientationSensor1.Sensor) then
begin
if not OrientationSensor1.Sensor.Started then OrientationSensor1.Sensor.Start;
// Error (only in XE8): Incompatible types 'TCustomLocationSensor.TProperty' and 'TCustomOrientationSensor.TProperty'
// In XE7 it works.
for p_orientation in OrientationSensor1.Sensor.AvailableProperties do
begin
n := 'OrientationSensor.'+GetEnumName(TypeInfo(TCustomOrientationSensor.TProperty), integer(p_orientation)) ;
v := FloatToStr(TOrientationSensorAccessor(OrientationSensor1.Sensor).GetDoubleProperty(p_orientation));
Memo1.Lines.Values[n] := v;
end;
end;
if Assigned(LocationSensor1.Sensor) then
begin
if not LocationSensor1.Sensor.Started then LocationSensor1.Sensor.Start;
for p_location in LocationSensor1.Sensor.AvailableProperties do
begin
n := 'LocationSensor.'+GetEnumName(TypeInfo(TCustomLocationSensor.TProperty), integer(p_location)) ;
v := FloatToStr(TLocationSensorAccessor(LocationSensor1.Sensor).GetDoubleProperty(p_location));
Memo1.Lines.Values[n] := v;
end;
end;
end;
Update
Some experiments:
(1) When I comment out the first "for", it will compile:
// for p_orientation in OrientationSensor1.Sensor.AvailableProperties do
// begin
n := 'OrientationSensor.'+GetEnumName(TypeInfo(TCustomOrientationSensor.TProperty), integer(p_orientation)) ;
v := FloatToStr(TOrientationSensorAccessor(OrientationSensor1.Sensor).GetDoubleProperty(p_orientation));
Memo1.Lines.Values[n] := v;
// end;
end;
(2) When I comment out the assigning of "n" and "v", it will compile too:
for p_orientation in OrientationSensor1.Sensor.AvailableProperties do
begin
// n := 'OrientationSensor.'+GetEnumName(TypeInfo(TCustomOrientationSensor.TProperty), integer(p_orientation)) ;
// v := FloatToStr(TOrientationSensorAccessor(OrientationSensor1.Sensor).GetDoubleProperty(p_orientation));
// Memo1.Lines.Values[n] := v;
end;
end;
Since neither "for", nor "n" and "v" is the bad region, where is the error then?
(3) When I comment out the second for-loop, it will compile again. If I comment out the first for-loop, it will compile too. Each for-loop works, but in combination they will not work.
It looks like the error is only happening if 5 factors are combined:
TypInfo
Accessors
for loop
Usage of TypInfo (GetEnumName)
Both for-loops are used.
Update 2
Here is the smallest reproducible code I could find. If any line is commented out, it compiles:
program ProjectCompilerBug;
{$APPTYPE CONSOLE}
uses
System.Sensors, System.Sensors.Components;
var
p_location: TCustomLocationSensor.TProperty;
p_orientation: TCustomOrientationSensor.TProperty;
begin
// Compilation Error (only in XE8):
// "Incompatible types 'TCustomLocationSensor.TProperty' and 'TCustomOrientationSensor.TProperty'"
// In XE7 it compiles
for p_orientation in TOrientationSensor.Create(nil).Sensor.AvailableProperties do
begin
FloatToStr(1.23);
end;
for p_location in TLocationSensor.Create(nil).Sensor.AvailableProperties do
begin
end;
end.
Yes, this looks like an XE8 compiler bug. I think you've done a fine job isolating it, for which I commend you. You'll need to submit bug report to Quality Portal.
To workaround the fault I think you will be able to put the loops in separate functions. My hypothesis is that the key is the presence of two for in loops with differently typed loop variables that is the key. Avoid that and you should be able to avoid the problem.
If this has already been visited here on SO, please point me to it, cause I cant seem to find it. Having said that:
Using the standard delphi application events, as well as the Mobile app lifecycle events handling , i am trying to find the best spot to read and write a INI file?
as I test, i created a demo app with a button which increments a count variable and displays it in a show message
procedure TfrmMain.Button1Click(Sender: TObject);
begin
inc(Count);
ShowMessage(IntToStr(Count));
end;
In the main form's OnCreate even, I read the inifile
procedure TfrmMain.FormCreate(Sender: TObject);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create( TPath.GetDocumentsPath + PathDelim + 'fortysixtozero.ini' );
try
Count := Ini.ReadInteger( 'Main', 'Count', 0 );
finally
Ini.Free;
end;
end;
Now, knowing that a mobile app can have different states, i am wondering where the best place is to write the ini file?
Best states to save the application state or store settings is "aeEnteredBackground". I used the delphi FMX event here. You should also check the "aeWillBecomeInactive" and "aeWillTerminate" events, but the first one is the most relavant. The application enters background when another application is opened or yours is closed (they are not terminated right away).
Check this article.
The code to listen for events looks like this:
function TfMain.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
case AAppEvent of
aeFinishedLaunching: ;
aeBecameActive: ;
aeWillBecomeInactive: ;
aeEnteredBackground: ;
aeWillBecomeForeground: ;
aeWillTerminate: ;
aeLowMemory: ;
aeTimeChange: ;
aeOpenURL: ;
end;
Result := True;
end;
To attach the listener you use the platform services:
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(SvcEvents)) then
SvcEvents.SetApplicationEventHandler(HandleAppEvent);
Just add "FMX.Platform" to your uses clause.
In Delphi XE7, there is an "OnSaveState" event for forms. This is the preferred place to save application data since it will execute when an iOS app goes into the "background" state. The documentation is quite helpful... search for "save state".
Here is my code in the main form's OnCreate handler:
procedure TMainWindow.FormCreate( Sender : TObject );
var
IniFile : TIniFile;
Metric : BOOLEAN;
IniFileName : STRING;
Reader : TBinaryReader;
begin
fInitializing := True;
SaveState.StoragePath := TPath.GetLibraryPath;
if SaveState.Stream.Size > 0 then begin
Reader := TBinaryReader.Create( SaveState.Stream );
try
Metric := Reader.ReadBoolean;
vMetricUnits.IsChecked := Metric;
SetSliderLimits( Metric );
Temperature := Reader.ReadDouble;
Dewpoint := Reader.ReadDouble;
Humidity := Reader.ReadDouble;
WindSpeed := Reader.ReadDouble;
finally
Reader.Free;
end;
end
else begin
Metric := False;
vMetricUnits.IsChecked := Metric;
SetSliderLimits( Metric );
Temperature := 70;
Dewpoint := 70;
Humidity := 100;
WindSpeed := 0;
end;
SetMetricUnits( cMetricUnits );
fInitializing := False;
WriteTrackbarCaptions;
CalculateTemperatures;
end;
And here is the code in the form's OnSaveState handler:
procedure TMainWindow.FormSaveState( Sender : TObject );
var
Writer : TBinaryWriter;
begin
SaveState.Stream.Clear;
Writer := TBinaryWriter.Create( SaveState.Stream );
try
Writer.Write( cMetricUnits );
Writer.Write( Temperature );
Writer.Write( Dewpoint );
Writer.Write( Humidity );
Writer.Write( WindSpeed );
finally
Writer.Free;
end;
end;
I've tested this on both the iPad and in Windows and it works on both platforms. Doing it this way completely avoids the use of the .ini file, however it does create a somewhat oddly named .tmp file in the Windows version. I assume that an equivalent file is also created on the iPad.