If you’re loading images off the web in your Android or IOS app you may want to take a look at this class. You can use the class to queue up loading images from the web asynchronously using a thread so that the UI of your app isn’t blocked. It also has some functionality where it will cache the images that you load with it so that if you load the same image multiple times it will only download the image once. There are some other web image loading components out there but if you simply want to queue up the loading with no frills then try this one out. It uses an asynchronous library from the same author called AsyncTask.
Head over and check out the TImageLoader class.
Update:Â Apparently Delphi XE5 has a threading issue with Bitmaps on Android which can cause this TImageLoader to access violation. I found the fix for this on StackOverflow here. Here is a demo project which demonstrates TImageLoader loading a Bitmap on Android.
Tried this on my mobile phone after creating a test project from Delphi XE5 update 2 and it just crashes. Anyone else made it work?
It doesn’t work on Android
I’ll see what I can do to get it working.
I get a message
‘Access violation at address 5DB090D0, accessing address 00000008’
I made this project with a fix and tested it on my Android device:
http://www.fmxexpress.com/wp-content/uploads/2014/01/ImageLoaderDemo.zip
I made a demo with a fix and got it working on Android:
http://www.fmxexpress.com/wp-content/uploads/2014/01/ImageLoaderDemo.zip
Have got your demo project working in the IOS Simulator with no problems. However, in my own project I get an access violation when the image is loaded. I have put the implementation code below.. Any ideas why it would not be working in XE6?
{$R *.fmx}
{$IFDEF ANDROID}
procedure MyEndThreadProc(ExitCode:Integer);
var
PActivity: PANativeActivity;
begin
PActivity := PANativeActivity(System.DelphiActivity);
PActivity^.vm^.DetachCurrentThread(PActivity^.vm);
pthread_exit(ExitCode);
end;
{$ENDIF}
procedure TfrmFriend.FormActivate(Sender: TObject);
begin
inherited;
lblDebug.Text := id;
getMember();
getProfile();
end;
procedure TfrmFriend.FormCreate(Sender: TObject);
begin
inherited;
{$IFDEF ANDROID}
EndThreadProc := MyEndThreadProc;
{$ENDIF}
IM := TImageLoader.Create();
end;
procedure TfrmFriend.FormDestroy(Sender: TObject);
begin
inherited;
im.Free;
end;
procedure TfrmFriend.FormShow(Sender: TObject);
begin
inherited;
IM.LoadImage(imgUser,'http://www.stoneacre.co.uk/assets/images/fuel-and-go-pricing.jpg');
end;
procedure TfrmFriend.getMember;
var
sResult : String;
begin
with dmdDataModule do
begin
// Open up the data.
rdsaMember.ClearDataSet;
fdmMember.Close;
respMember.Content.Empty;
reqMember.ClearBody;
reqMember.Params.ParameterByName('id').Value := id;
reqMember.Params.ParameterByName('signature').Value := signature('getMember');
reqMember.Params.ParameterByName('key').Value := getApiKey;
reqMember.Execute;
sResult := getResultString(respMember.Content);
if (sResult = 'OK') then
begin
rdsaMember.Response := respMember;
fdmMember.Open;
end;
end;
end;
initialization
RegisterFMXClasses([TfrmFriend]);
XE6 does threads differently so not sure. When I moved code over from XE5 to XE6 everywhere where I had used .DisposeOf; I basically had to comment out because it would AV. Use the debugger to find the line it is erroring on?
This component is not working on android with XE7
on xe6 is fine
My guess is you may be able to make it work on XE7 by removing this line:
fWorker.Enabled := False;
From:
procedure TImageLoader.QueueWorkerOnTimer(ASender: TObject);
What I have seen in XE7 is that if you set Enabled of the Timer to False from within the timer’s own event it Exit;’s the function right there and runs no code after that. By removing that line the function may run correctly.
I commented that line and still failing, on windows work fine, but on android don’t work
DP
This is the one I wrote and use (check it’s code for the timer enabled issue too though since it was for XE5):
http://www.fmxexpress.com/threaded-progress-bar-image-loader-for-delphi-xe5-firemonkey-on-android-and-ios/
Can’t get this component to work on Android XE7?
I have to migrate a project from XE6 to XE7, but this component is not working on (Android/XE7).
Help, Please
DP