B4A Library RSAsyncDownloader - Download files asynchronously & easily!

Hi everyone,

I present to you my RSAsyncDownloader.
This will download any file very easy and asynchronously from a given URL.

B4X:
Dim AsyncDownloader As RSAsyncDownloader
AsyncDownloader.Initialize("AsyncDownloader")
AsyncDownloader.FileName = "FileName.pdf"
AsyncDownloader.Directory = File.DirDefaultExternal
AsyncDownloader.Download("Your url here.")

3 Events are raised:

B4X:
Private Sub AsyncDownloader_Started
'The download has started.
End Sub

Private Sub AsyncDownloader_Update (Progress As Int)
'Monitor the progress here.
End Sub

Private Sub AsyncDownloader_Finished (Result As String)
'The download has finished.
End Sub


Kind regards,
Tomas
 

Attachments

  • RSAsyncDownloader.zip
    6.6 KB · Views: 726

billzhan

Active Member
Licensed User
Longtime User
Tomas,

I played with if for a while. Really easy to use.

I may found a bug.On OS 2.3 and 4.1:
B4X:
Private Sub AsyncDownloader1_Finished (Result As String)
Log(AsyncDownloader.Directory&"  "&AsyncDownloader.FileName)
End Sub
I got the log: /mnt/sdcard/ /mnt/sdcard/


Cheers,
Bill
 

qsrtech

Active Member
Licensed User
Longtime User
hey what is the "result" suppose to return in the "Finished"? It would be nice if it was the complete dir and filename...
 

billzhan

Active Member
Licensed User
Longtime User
When the download is success,Result= Null.
Before downloading,you need to set the file name and path to this download. See the #1 post:
B4X:
AsyncDownloader.FileName = "FileName.pdf"
AsyncDownloader.Directory = File.DirDefaultExternal

You can get the file name and directory anywhere by:
B4X:
Directory=AsyncDownloader.Directory
FileName=AsyncDownloader.FileName
 

qsrtech

Active Member
Licensed User
Longtime User
I'm getting a Null pointer error when re-downloading my file. First time works fine but get this when trying again. have to totally close app get d/l to work again.
at com.rootsoft.rsasyncdownloader.DownloadTask.onPreExecute(DownloadTask.java:102)
 

billzhan

Active Member
Licensed User
Longtime User
I am quite sure that you need to initialize(initialize,filename,directory) your AsyncDownloader every time before you restart a download, else an error will occur.
B4X:
AsyncDownloader.Initialize("AsyncDownloader")
AsyncDownloader.FileName = "FileName.pdf"
AsyncDownloader.Directory = File.DirDefaultExternal
AsyncDownloader.Download("Your url here.")
 

qsrtech

Active Member
Licensed User
Longtime User
I did try initialize again but I didn't reset the filename, etc. I'll let you know if the filename makes any difference.
 

susu

Well-Known Member
Licensed User
Longtime User
I need help about RSAsyncDownloader.LoadBitmap method. My code here:

B4X:
Dim downloader As RSAsyncDownloader
Dim img As ImageView

downloader.Initialize("downloader")
img.Initialize("")
Activity.AddView(img, 0, 10dip, Activity.Width, 300dip)
img.Gravity = Gravity.FILL
img.Bitmap = downloader.LoadBitmap("http://www.b4x.com/new_images/b4a_logo.png")

It works perfect if I compile in Debug (rapid) mode, I can see B4A logo but when I compile in Release/Release (obfuscated) mode, there's no image. Especially, when I compile in Debug (legacy) mode, it stops at line:
img.Bitmap = downloader.LoadBitmap("http://www.b4x.com/new_images/b4a_logo.png")
with LastException error: java.lang.NullPointerException

Tomas, could you please check it? Thank you so much.
 

aminoacid

Active Member
Licensed User
Longtime User
Works great. Thanks!

Just wondering why the "Prevent phone from Sleeping" feature is enabled. When I use your library and install my APP, I get a message from APP Permissions "xxx needs access to: System Tools - Prevent phone from sleeping".

Any way to not require this permission?

Thanks.
 

dibb3386

Member
Licensed User
Longtime User
Old topic but just using this now, (if right might help others searching) @amin If downloading and phone went in to sleep status, wouldn't the download be interrupted? Especially with wifi if the phone/tablet is configured to turn off wifi when in sleep mode. Or even power saving mode on some devices. This would prevent this from happening.
 

Devv

Active Member
Licensed User
Longtime User
thanks fro this nice lib
it is working nicely except with the following code it will not toast the progress

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim AsyncDownloader As RSAsyncDownloader
    AsyncDownloader.Initialize("AsyncDownloader")
    AsyncDownloader.FileName = "test.apk"
    AsyncDownloader.Directory = File.DirRootExternal
    AsyncDownloader.Download("http://downloader-apk.com/apps/2015/02/19/WhatsApp_Messenger%202.11.528_[www.Downloader-Apk.com].apk")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Private Sub AsyncDownloader_Started
ToastMessageShow("The download has started.",False)
End Sub

Private Sub AsyncDownloader_Update (Progress As Int)
ToastMessageShow(Progress,False)
End Sub

Private Sub AsyncDownloader_Finished (Result As String)
ToastMessageShow("The download has finished.",False)
End Sub


this is very wired if you placed any url instead of the flowing it will work

any ideas ?
 

jahswant

Well-Known Member
Licensed User
Longtime User
I'm trying to download multiple files here with no success is it possible?
B4X:
** Activity (download) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (download) Create, isFirst = true **
** Activity (download) Resume **
The download has finished.null
download_downbtn2_click (java line: 428)
java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
    at android.os.AsyncTask.execute(AsyncTask.java:534)
    at com.rootsoft.rsasyncdownloader.RSAsyncDownloader.Download(RSAsyncDownloader.java:48)
    at in.liam.music.download._downbtn2_click(download.java:428)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4229)
    at android.view.View$PerformClick.run(View.java:17629)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5413)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
 
Last edited:
Top