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.basic4ppc.com/basic4android/images/SS-2012-08-29_12.55.42.png")
   links.Put(ImageView2, "http://www.basic4ppc.com/basic4android/images/SS-2013-03-04_11.42.38.png")
   links.Put(ImageView3, "http://www.basic4ppc.com/basic4android/images/SS-2013-03-04_11.52.19.png")
   links.Put(ImageView4, "http://www.basic4ppc.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: 6,361
Last edited:

lock255

Well-Known Member
Licensed User
In practice, when would that be in error would show ImageView1 an image contained locally.
The type to show an image prepared by me saying "Page not available", so as to make more cute dialogue with the user.
 

lock255

Well-Known Member
Licensed User
Yes I understand, but have not uploaded an image locally, would you suggest me a simple tutorial to do that?
 

stanks

Active Member
Licensed User
i moved part of code from activity resume to service_create, but what to do with activity_pause? do i need code in activity_pause?
 
Last edited:

Douglas Farias

Expert
Licensed User
Erel how can i know when the imageview have the photo or no ?

i make this

B4X:
    If kv.GetSimple("imgcrop") = "3" Then
    links.Put(imvImage, "http://localhost/clickfight/UsersImages/"&userid&"/MeuPerfil/NaoEnviadas/FinalPictures/"&"3.jpg")
    CallSubDelayed2(ImageDownloader, "Download", links)
    ProgressDialogHide

my progressdialog close but the image dont show in the same time show me 2 3 seconds later
how can i know if the image downloaded is on imageview ProgressDialogHide
 

Douglas Farias

Expert
Licensed User
done *-* when the image is realy visible close my progressdialog

this is the code from downloaderimage

B4X:
Sub JobDone(Job As HttpJob)
    ongoingTasks.Remove(Job.JobName)
    If Job.Success Then
        Dim bmp As Bitmap = Job.GetBitmap
        cache.Put(Job.JobName, bmp)
        If tasks.IsInitialized Then
            For i = 0 To tasks.Size - 1
                Dim link As String = tasks.GetValueAt(i)
                If link = Job.JobName Then
                    Dim iv As ImageView = tasks.GetKeyAt(i)
                    iv.SetBackgroundImage(bmp)
                    ProgressDialogHide
                End If
            Next
        End If
    Else
    ProgressDialogHide
        Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
    End If
    Job.Release
End Sub
 

eSolution

Member
Licensed User
With Image Downloader 2 - I have a problem when I try to start the app in rapid debug mode on my device, I get this error: "
pkg: /data/local/tmp/1_RAPID_DEBUG.apk
Failure [INSTALL_FAILED_VERSION_DOWNGRADE]

Restarting ADB Server may solve this problem.
"
I have restarted ADB several times, other projects compile ok and run ok ... I have tried all android platforms above 14 (14 to L) and the error is present on all... I use Java 1.8.05 , My Android SDK is up to date (last update made 30 min ago) and my B4A is the last update 3.82

Any fix for this kind of behavior?

Image Downloader 1 works fine


EDIT: I have found the problem: the example use the 1.0 CustomListView, after an update to 1.1 it works like a charm :D
 
Last edited:

bluedude

Well-Known Member
Licensed User
I use this solution but my images in an imageview never get the correct gravity. Does setbackground actually use a Gravity.fill setting? My images always get cut off at the sides a little.

Tried to use a resizeimage solution but that reduces the quality too much.

Should I use loadbitmap instead?
 

bluedude

Well-Known Member
Licensed User
Hi,

I already do that but it does not work as expected. Is there another solution to make images (company logos) always scale perfectly?

On iPhone we use a small PDF viewer, this works extremely well.
 

bluedude

Well-Known Member
Licensed User
Found the issue, I set a panel background with a holo bg panel, this one cuts off the image on the sides.
 

tpakis

Active Member
Licensed User
I'm trying to optimize the memory allocation, cause i am having problems when i show a large list of images. In the service there is a cache map where the links and the bitmaps are stored, and in the main activity there is the clv with the loaded images. What is causing the problem, the cache or the clv? Should i try to have less images loaded, or to erase the cache?
 
Status
Not open for further replies.
Top