Download and Install APK

thughesimsuk

Member
Licensed User
Longtime User
I can successfully download an apk file from a server to my device, using an example provided by Erel(HttpUtils2 version 2.00).

B4X:
job1.Initialize("Job1", Me)
job1.Download("http://xxxxxxxxxxx/downloads/audit_test.zip")

Then the following sub,

B4X:
Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1"
         
         ToastMessageShow("Download Complete", True)
   End Select
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

But what I need to do is install the file, now I think the file downloads to the File.DirInternalCache.



B4X:
Sub Service_Create
   TempFolder = File.DirInternalCache
   
   'TempFolder = File.DirRootExternal
   
   hc.Initialize("hc")
   TaskIdToJob.Initialize
End Sub

How could i install the file? Do i need to use 'intent'? My goal is to open up my project then I have a button which a user can click to install the latest version of the software.

If any more info is needed please ask, I thankyou for taking the time to read this post and appreciate any help.

Thanks,

Tom
 

NJDude

Expert
Licensed User
Longtime User
To install an app you need a code like this:
B4X:
Dim i As Intent 

i.Initialize(i.ACTION_VIEW, "file:///sdcard/MyAPK.apk")
i.SetType("application/vnd.android.package-archive")

StartActivity(i)

However, you cannot install from File.DirInternalCache or File.DirInternal, you must copy the file to File.DirDefaultExternal for example, the reason is because only your app has access to the DirInternal directories, the installer don't.
 
Last edited:
Upvote 0

thughesimsuk

Member
Licensed User
Longtime User
To install an app you need a code like this:
B4X:
Dim i As Intent 

i.Initialize(i.ACTION_VIEW, "file:///sdcard/MyAPK.apk")
i.SetType("application/vnd.android.package-archive")

StartActivity(i)

However, you cannot install from File.DirInternalCache or File.DirInternal, you must copy the file to File.DirDefaultInternal for example, thee reason is because only your app has access to the DirInternal directories, the installer don't.

Thanks NJ, the problem is how do i copy the file from the cache? I tried this,

B4X:
If File.Exists(File.DirInternalCache,"audit_test.zip") = True Then

File.Copy(File.DirInternalCache,"audit_test.zip",File.DirRootExternal,"audit_test.zip")

ToastMessageShow("File Copied!", True)

end if

but i cant seem to ever find the file to copy it?? Is it a permission issue? Is there a way for me to view the internal cache via some sort of file explorer?
 
Upvote 0

sasidhar

Active Member
Licensed User
Longtime User
Hi,

I could able to download file(httputils2) from given location, but where in mobile it shall store, unable to see the downloaded file..Can share the path.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You must save the downloaded file in Sub JobDone to a place where you want it to be...
See the examples in my signature (create dynamic jpg) and be inspired by that code. Look JobDone-sub and try to understand how i write the image (or in your case the apk) to disc.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
You may use my AppUpdating library as is or be inspired by its source code.
Sorry to post without any link and in a so concise way, but currently I am on the road with a very small device.

Umberto
 
Upvote 0

sasidhar

Active Member
Licensed User
Longtime User
Hi,

Thanks for your inputs and example shared. i could able to download, but file name as is not coming as-is. coming as java.lang.object@..I am new to this concept..I am downloding a zip file from a url downloading with the mentioned name..not the same name with extension coming.

I am using the same code you shared in the example.

Case "Job4"
'print the result to the logs

Dim OutStream As OutputStream
Log("DownloadReady: "&Job.Tag)
OutStream = File.OpenOutput(File.DirRootExternal, Job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
File.Copy2(Job.GetInputStream,OutStream) ' save the file
OutStream.Close
Log(Job.Tag&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)

Dim i As Intent
i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
"file://" & File.Combine(File.DirRootExternal, Job.Tag))
Dim p As Phone
p.SendBroadcastIntent(i)

ToastMessageShow("Download Complete", True)
ToastMessageShow("Sucess", True)
 
Upvote 0
Top