Android Tutorial ImageDownloader - The simple way to download images

Status
Not open for further replies.
Don't use this. SimpleMediaManager is 100 times more powerful: [B4X] SimpleMediaManager (SMM) - framework for images, videos and more

Downloading images with HttpUtils2 is quite simple.

However correctly managing multiple downloads without downloading the same image multiple times, for example when the user changes the screen orientation, is more complicated.

ImageDownloader makes it very simple to efficiently download images and show them in ImageViews.

Example code:
B4X:
Sub Globals
   Dim ImageView3 As ImageView
   Dim ImageView2 As ImageView
   Dim ImageView1 As ImageView
   Dim ImageView4 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
   Dim links As Map
   links.Initialize
   links.Put(ImageView1, "http://www.b4x.com/basic4android/images/SS-2012-08-29_12.55.42.png")
   links.Put(ImageView2, "http://www.b4x.com/basic4android/images/SS-2013-03-04_11.42.38.png")
   links.Put(ImageView3, "http://www.b4x.com/basic4android/images/SS-2013-03-04_11.52.19.png")
   links.Put(ImageView4, "http://www.b4x.com/basic4android/images/SS-2012-02-06_12.45.56.png")
   CallSubDelayed2(ImageDownloader, "Download", links)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   CallSub(ImageDownloader, "ActivityIsPaused")
End Sub

How to use?

1. In Activity_Resume you should create a Map with the ImageViews as keys and the links as values. Use CallSubDelayed to call ImageDownloader.Download.
2. In Activity_Pause you should use CallSub to call ImageDownloader.ActivityIsPaused.
3. That's it.

SS-2013-07-09_16.03.45.png
 

Attachments

  • ImageDownloader.zip
    8.1 KB · Views: 7,344
Last edited:

tpakis

Active Member
Licensed User
Longtime User
So if i have to resume (orientation change) i will have to redownload the images?
 

Beja

Expert
Licensed User
Longtime User
Hi,
I used the above method:
B4X:
Public Sub ClearCache
   cache.Clear
End Sub
But I get the message on compile saying
Cache is used before assigning any value..
Any meaning appreciated.
 

Beja

Expert
Licensed User
Longtime User
No Don, what should I declare in process global and as what? thank you
 

Beja

Expert
Licensed User
Longtime User
No mention for process global variable, becides didn't know what to declare. Thanks for your help.
 

Beja

Expert
Licensed User
Longtime User
Hi Erel,
Thanks and yes, I appended it at the end of the module, this is what I got:
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


customlistview_clear (java line: 93)


java.lang.NullPointerException

at b4a.maps.example.customlistview._clear(customlistview.java:93)
at b4a.maps.example.main._activity_create(main.java:336)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at b4a.maps.example.main.afterFirstLayout(main.java:100)
at b4a.maps.example.main.access$100(main.java:17)
at b4a.maps.example.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
** Service (httputils2service) Create **


** Service (httputils2service) Start **


** Activity (main) Resume **


java.net.UnknownHostException: www.loc.gov
 

Beja

Expert
Licensed User
Longtime User
debugwindow.jpg
it is from clv.clear.. is it because it was put before the initialization line?
 

Beja

Expert
Licensed User
Longtime User
Attached is the project..
 

Attachments

  • testCLV.zip
    15.8 KB · Views: 665

johnB

Active Member
Licensed User
Longtime User
Hi
(Sorry if I have posted this in the wrong place but there are many examples/tutorials on retrieving images on the forum)
I'm trying to download images from my Windows Server to display.
I'm having trouble with the Address(Link).
I have been successfully using the address with RDC for, well as long as RDC's been available
Wondering if somebody would be good enough to point out where i'm going wrong

In RDC, the address I'm using is - reqManager.Initialize(Me, "http://j??????????.?????.net:17178"), this is a No-IP address

In the image download programs available on the forum

In the Flicker example I have used something like this - MainUrl = "http://www.j?????????.????.net/C:/Users/jb/Pictures/Family/" where the images are in C:/Users/jb/Pictures/Family/. I've tried it with the Server Name - \\WIN-KA57JNPJAAS but that didn't work either. I've tried many different combinations without success.

In the ImageDownloader2 example , again I have tried numerous variation without success eg

pageJob.Download("http://j?????????.????.net:17178/?method=test#WIN-KA57JNPJAAS/Users/jb/Dropbox/Photos/jb")
pageJob.Download2("http://j????????.????.net:17178/?method=test#WIN-KA57JNPJAAS/Users/jb/Dropbox/Photos/jb", Array As String("User = A......", "Password = A......"))
'pageJob.Download2("http://j?????????.????.net:17178/?method=test", Array As String("WIN-KA57JNPJAAS/Users/jb/Dropbox/Photos/jb"))

I'm sure it must need a Use and Password somewhere

Thanks in anticipation
 

DonManfred

Expert
Licensed User
Longtime User
In the ImageDownloader2 example , again I have tried numerous variation without success eg
You are confusing two different things. RDC has nothing to do wqith imagedownloading.

The image must be accessible via a webserver running on that machine and you need to use the right url for the download. Not the RDC url
 

johnB

Active Member
Licensed User
Longtime User
Ok thanks, so I need something like Apache and PHP on the Server, I've used them a long time ago
 

johnB

Active Member
Licensed User
Longtime User
Thanks Don for your prompt reply, I'll download them and see what my memory is like, but there's plenty of Online support these days, not like the days when I had to go into a city book store and spend $70 on a book. I have 2 on PHP, Apache and MySql, old but a good starting point

Thanks again
 

johnB

Active Member
Licensed User
Longtime User
Thanks Don for pointing me in the right direction, I've a NAS Server and I've got it working on that
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.
The good topic
When picture download and show in imageview,the picture is cache?
 
Status
Not open for further replies.
Top