It's better to using this:https://www.b4x.com/android/forum/threads/imageloaderv2.75022/
dependson:HttpUtils2,KeyValueStore2,iSQL,iRandomAccessFile,iEncryption
iCache:
imagedownloader
comm
dependson:HttpUtils2,KeyValueStore2,iSQL,iRandomAccessFile,iEncryption
iCache:
B4X:
'Class module depends on KeyValueStore
Sub Class_Globals
Private kvscache As KeyValueStore
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Initialize2("","")
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize2(dir As String,filename As String)
If dir=Null Or dir.Length=0 Or filename=Null Or filename.Length=0 Then
kvscache.Initialize(File.DirTemp,"test.tmp")
Else
kvscache.Initialize(dir,filename)
End If
End Sub
'will encode the key to md5
Public Sub ContainsKey(key As String) As Boolean
key=comm.getMD5(key)
Return kvscache.ContainsKey(key)
End Sub
Public Sub getString(key As String,def As String) As String
key=comm.getMD5(key)
Return kvscache.GetDefault(key,def)
End Sub
Public Sub putString(key As String,val As String)
key=comm.getMD5(key)
kvscache.Put(key,val)
End Sub
Public Sub getDef(key As String,def As Object) As Object
If kvscache.ContainsKey(key) Then Return getObjectFromCache(key) Else Return def
End Sub
Public Sub putObj(key As String,val As Object)
key=comm.getMD5(key)
kvscache.Put(key,val)
End Sub
Public Sub getObjEnc(key As String,pass As String) As Object
Return getObjEncDef(key,pass,Null)
End Sub
Public Sub getObjEncDef(key As String,pass As String,def As Object) As Object
key=comm.getMD5(key)
If kvscache.ContainsKey(key) Then Return kvscache.GetEncrypted(key,pass) Else Return def
End Sub
Public Sub putObjEnc(key As String,val As Object,pass As String)
key=comm.getMD5(key)
kvscache.PutEncrypted(key,val,pass)
End Sub
Public Sub close
kvscache.Close
End Sub
Public Sub getBitmap(key As String) As Bitmap
key=comm.getMD5(key)
' Dim bmp As Bitmap
' bmp.Initialize(File.DirTemp,key)
' Return bmp
Return kvscache.GetBitmap(key)
End Sub
Public Sub putBitmap(key As String,bmp As Bitmap)
key=comm.getMD5(key)
' kvscache.PutSimple(key,File.Combine(File.DirTemp,key))
' Dim ops As OutputStream= File.OpenOutput(File.DirTemp,key,False)
' bmp.WriteToStream(ops,100,"PNG")
' ops.Close
kvscache.PutBitmap(key,bmp)
End Sub
Public Sub getObjectFromCache(key As String) As Object
key=comm.getMD5(key)
Return kvscache.Get(key)
End Sub
Public Sub remove(key As String)
key=comm.getMD5(key)
kvscache.Remove(key)
End Sub
Public Sub clearAllCache
kvscache.DeleteAll
End Sub
imagedownloader
B4X:
'depends on iCache,HttpUtils2
Sub Class_Globals
' Public gravitymode As Object=Null
Private taskobjs,taskkeys As List
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
taskkeys.Initialize
taskobjs.Initialize
End Sub
Public Sub addJob2(url As String,iv As ImageView,usingcache As Boolean)
Dim m As Map
m.Initialize
m.Put("url",url)
m.Put("iv",iv)
Dim key As String=comm.getMD5(url)
m.Put("key",key)
m.Put("cache",usingcache)
If usingcache And comm.cache.ContainsKey(key) Then
iv.Bitmap=comm.cache.getBitmap(key)
' If gravitymode<>Null Then iv.ContentMode=gravitymode
Else
If taskkeys.IndexOf(key)>-1 Then
Log("on going:"&url)
Else
taskobjs.Add(m)
taskkeys.Add(key)
Dim j As HttpJob
j.Initialize(key, Me)
j.Download(url)
End If
End If
End Sub
Public Sub addJob(url As String,iv As ImageView)
addJob2(url,iv,True)
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
Dim bmp As Bitmap = Job.GetBitmap
Dim idx As Int=taskkeys.IndexOf(Job.JobName)
Dim m As Map=taskobjs.Get(idx)
If m.GetDefault("cache",False) Then comm.cache.putBitmap(Job.JobName,bmp)
Dim iv As ImageView=m.Get("iv")
taskkeys.RemoveAt(idx)
taskobjs.RemoveAt(idx)
iv.Bitmap=bmp
' If gravitymode<>Null Then iv.ContentMode=gravitymode
Else
Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
End If
Job.Release
End Sub
comm
B4X:
'Code module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public bc As ByteConverter
Public cache,data As iCache
Public imgldr As ImageDownloader
End Sub
Public Sub Initialize
data.Initialize2(File.DirDocuments,"mdata.dat")
cache.Initialize
Starter.start
imgldr.Initialize
End Sub
Public Sub isLogin As Boolean
Return data.getDef("islogin",False)
End Sub
Public Sub setLogin(b As Boolean)
data.putObj("islogin",b)
End Sub
Sub loadimg (imgurl As String,v As ImageView)
imgldr.addJob(imgurl,v)
End Sub
Sub loadimg_nocache (imgurl As String,v As ImageView)
imgldr.addJob2(imgurl,v,False)
End Sub
Public Sub resize(width As Int,height As Int,pg As Page)
'////// Start resize all views to fit all screensize //////
Dim ratiox As Float = width/320 'iphone 4 3.5" = x320
Dim ratioy As Float = height/560 'iphone 4 3.5" = y480 - 20 !
For Each v As View In pg.RootPanel.GetAllViewsRecursive
If v.Tag="drs" Then'drs= dont chg my size
v.Left = v.Left*ratiox
v.Top = v.Top*ratioy
Else if v.Tag="drm" Then'drm = dont resize me...
Else
v.Width = v.Width*ratiox
If v.Height<>1 Then v.Height = v.Height*ratioy
v.Left = v.Left*ratiox
v.Top = v.Top*ratioy
' Log(v.Width&"*"&v.Height)
End If
Next
'////// End resize all views ///////
End Sub
Public Sub str2hex(str As String) As String
Return bc.HexFromBytes(str.GetBytes("utf8"))
End Sub
public Sub getMD5(str As String) As String
Dim buf() As Byte=str.GetBytes("utf8")
Dim mD As MessageDigest
Dim retbuf() As Byte=mD.GetMessageDigest(buf,"MD5")
Dim retstr As String=bc.HexFromBytes(retbuf)
' Log(retstr)
Return retstr
End Sub
Last edited: