Android Question Download and Install APK as Update

Blake K

New Member
What am I missing here? I am able to successfully install an APK if it is in the DirAssets folder and copied into Starter.Provider.SharedFolder. The download works and I have use File.Exists to confirm that it is being downloaded, and succesfully copied over to the 'SharedFolder' however when I attempt to run the same intent but with the downloaded apk as shown below the APK fails to install and a dialog box shows, " There was a problem parsing the package."

I am very stuck on this issue and need to figure out a solution as soon as possible. This is simply a "demo project". Once it is functional it will be incorporated into its intended project.

If anyone could help, I'd greatly appreciate it! Thank you -- Blake

APK UPDATE (Download & Install):
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private phone As Phone
    Public rp As RuntimePermissions
End Sub

Sub Globals
Dim ApkName As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    ApkName = "test.apk"
    
    Activity.LoadLayout("1")
    
    If File.Exists(Starter.Provider.SharedFolder, ApkName) Then '' For testing purposes to check file is downloaded each time
        File.Delete(Starter.Provider.SharedFolder, ApkName)
        ToastMessageShow("File Deleted",False)
    End If
    
    Dim job As HttpJob
    job.Initialize("j", Me)
    
    job.Download("http://tdadevelopment.byethost5.com/" & ApkName)
    Sleep(1000)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(job As HttpJob)
    If job.Success Then
 
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, ApkName, False)
        File.Copy2(job.GetInputStream, out)
        out.Close '<------ very important
        Log("Download Success")
    End If
    job.Release
End Sub

Sub Button1_Click
    Wait For (CheckInstallationRequirements) Complete (Result As Boolean)
    If Result Then
        SendInstallIntent
    End If
End Sub

Private Sub CheckInstallationRequirements As ResumableSub
    If File.ExternalWritable = False Then
        MsgboxAsync("Storage card not available. Make sure that your device is not connected in USB storage mode.", "")
        Return False
    Else If phone.SdkVersion >= 26 And CanRequestPackageInstalls = False Then
        MsgboxAsync("Please allow me to install applications.", "")
        Wait For Msgbox_Result(Result As Int)
        Dim in As Intent
        in.Initialize("android.settings.MANAGE_UNKNOWN_APP_SOURCES", "package:" & Application.PackageName)
        StartActivity(in)
        Wait For Activity_Resume '<-- wait for Activity_Resume
        Return CanRequestPackageInstalls
    Else If CheckNonMarketAppsEnabled = False Then
        MsgboxAsync("Please enable installation of non-market applications." & CRLF & "Under Settings - Security - Unknown sources" _
             & CRLF & "Or Settings - Applications - Unknown sources", "")
        Return False
    Else
        Return True
    End If
End Sub

Private Sub CanRequestPackageInstalls As Boolean
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
    Return PackageManager.RunMethod("canRequestPackageInstalls", Null)
End Sub

Private Sub CheckNonMarketAppsEnabled As Boolean
    If phone.SdkVersion >= 26 Then Return True
    If phone.SdkVersion < 17 Or phone.SdkVersion >= 21 Then
        Return phone.GetSettings("install_non_market_apps") = "1"
    Else
        Dim context As JavaObject
        context.InitializeContext
        Dim resolver As JavaObject = context.RunMethod("getContentResolver", Null)
        Dim global As JavaObject
        global.InitializeStatic("android.provider.Settings.Global")
        Return global.RunMethod("getString", Array(resolver, "install_non_market_apps")) = "1"
    End If
End Sub

Private Sub SendInstallIntent
    File.Copy(File.DirInternal, ApkName, Starter.Provider.SharedFolder, ApkName)
    
    Dim i As Intent
    If phone.SdkVersion >= 24 Then
        i.Initialize("android.intent.action.INSTALL_PACKAGE", Starter.Provider.GetFileUri(ApkName))
        i.Flags = Bit.Or(i.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
    Else
        i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Starter.Provider.SharedFolder, ApkName))
        i.SetType("application/vnd.android.package-archive")
    End If
    StartActivity(i)
End Sub
 

Blake K

New Member
Thanks for your response, Erel! I found if I named the file something like "download.txt" on the server and then when I used File.Copy, I renamed the file to "test.apk" it would work fine.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
If anyone could help
Have you tried this library?

 
Upvote 0
Top