Android Question APK file download via httputils2 and save to SD card to setup issue

vincentehsu

Member
Licensed User
Dear Sir:
Attachment is my project use for down .apk file from server and savt to sd card,I have some problems as below:
(1)I got the sd card file path(Check my project)
B4X:
/storage/extSdCard/Android/data/b4a.samples.downloadservice/files
if i used below code,then i can see the file in my phone's sd card path
B4X:
File.WriteString(Path,"Agree.txt","Agree")
my issue is when jobdown finish,I can't see tmp.apk file in my phone's sd card
B4X:
Sub JobDone (job As HttpJob)
   Log("JobName = " & job.JobName & ", Success = " & job.Success)
   ProgressDialogHide
   If job.Success = True Then
      Select job.JobName
      Case "job2"
            Log(Path)
            If File.Exists(Path , "tmp.apk")=True Then
                File.Delete(Path,"tmp.apk")
            End If
            MyStream = File.OpenOutput(Path, "tmp.apk", False)
            MyStream.InitializeToBytesArray(1)
            File.Copy2(job.GetInputStream,MyStream) ' save the file
      
Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW, "File://" &  Path & "/tmp.apk")
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)

        End Select
    End If
    job.Release
End Sub
(2) when i exe the code as below,i got parser error,it's cause of the file not exist in my sd card path or something miss?
B4X:
Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW, "File://" &  Path & "/tmp.apk")
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
 

vincentehsu

Member
Licensed User
Dear DonManfred:
I've change my code as below,and the path shown as "/storage/emulated/0"

B4X:
Sub JobDone (job As HttpJob)
   Log("JobName = " & job.JobName & ", Success = " & job.Success)
   ProgressDialogHide
   If job.Success = True Then
      Select job.JobName
      Case "job2"
            Log(Path)
            If File.Exists(File.DirRootExternal , "tmp.apk")=True Then
                File.Delete(File.DirRootExternal,"tmp.apk")
            End If
            MyStream = File.OpenOutput(File.DirRootExternal, "tmp.apk", False)
            MyStream.InitializeToBytesArray(1)
            File.Copy2(job.GetInputStream,MyStream) ' save the file
           
            Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW,  File.DirRootExternal & "tmp.apk")
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
        End Select
    End If
    job.Release
End Sub
i got an error
B4X:
Error occurred on line: 90 (main)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0tmp.apk typ=application/vnd.android.package-archive flg=0x20000 }


    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1660)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
    at android.app.Activity.startActivityForResult(Activity.java:3532)
    at android.app.Activity.startActivityForResult(Activity.java:3493)
    at android.app.Activity.startActivity(Activity.java:3735)
    at android.app.Activity.startActivity(Activity.java:3703)
    at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:696)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at anywheresoftware.b4a.BA$3.run(BA.java:320)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5476)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
    at dalvik.system.NativeStart.main(Native Method)
as i know,the apk file we want to set up shoule be store at sd card,right?
 

vincentehsu

Member
Licensed User
B4X:
Sub JobDone (job As HttpJob)
   Log("JobName = " & job.JobName & ", Success = " & job.Success)
   ProgressDialogHide
   If job.Success = True Then
      Select job.JobName
      Case "job2"
            Log(Path)
            If File.Exists(File.DirRootExternal , "tmp.apk")=True Then
                File.Delete(File.DirRootExternal,"tmp.apk")
            End If
            MyStream = File.OpenOutput(File.DirRootExternal, "tmp.apk", False)
            MyStream.InitializeToBytesArray(1)
            File.Copy2(job.GetInputStream,MyStream) ' save the file
           
            If File.Exists(File.DirRootExternal , "tmp.apk")=True Then
                Log("True")
            End If
           
            Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW,"file://" & File.Combine(File.DirRootExternal, "tmp.apk"))
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
        End Select
    End If
    job.Release
End Sub
file is exist, but i got another error from StartActivity(iIntent)
parsing the package error,it's cause the file not in external storage(SD card)?
 

vincentehsu

Member
Licensed User
I've change to
B4X:
Sub JobDone (job As HttpJob)
   Log("JobName = " & job.JobName & ", Success = " & job.Success)
   ProgressDialogHide
   If job.Success = True Then
      Select job.JobName
      Case "job2"
            Log(Path)
            If File.Exists(File.DirRootExternal , "tmp.apk")=True Then
                File.Delete(File.DirRootExternal,"tmp.apk")
            End If
            MyStream = File.OpenOutput(File.DirRootExternal, "tmp.apk", False)
            MyStream.InitializeToBytesArray(1)
            File.Copy2(job.GetInputStream,MyStream) ' save the file
           
            If File.Exists(File.DirRootExternal , "tmp.apk")=True Then
                Log("True")
            End If
           
            Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW,"File://" & File.Combine(File.DirRootExternal, "tmp.apk"))
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
        End Select
    End If
    job.Release
End Sub
i got an error
B4X:
True
Error occurred on line: 94 (main)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=File:///storage/emulated/0/tmp.apk typ=application/vnd.android.package-archive flg=0x20000 }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1660)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
    at android.app.Activity.startActivityForResult(Activity.java:3532)
    at android.app.Activity.startActivityForResult(Activity.java:3493)
    at android.app.Activity.startActivity(Activity.java:3735)
    at android.app.Activity.startActivity(Activity.java:3703)
    at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:696)
    at b4a.samples.downloadservice.main._jobdone(main.java:552)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at anywheresoftware.b4a.BA$3.run(BA.java:320)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5476)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
    at dalvik.system.NativeStart.main(Native Method)
 

vincentehsu

Member
Licensed User
I'm sorry i don't get it,is here?
B4X:
Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW,"file://" & File.Combine(File.DirRootExternal, "tmp.apk"))
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
change to
B4X:
Dim iIntent As Intent
            iIntent.Initialize(iIntent.ACTION_VIEW,"File://" & File.Combine(File.DirRootExternal, "tmp.apk"))
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
 

vincentehsu

Member
Licensed User
yes,tks
B4X:
Sub btnDownload_Click
    job2.Initialize("job2", Me)   
       job2.Download("http://125.227.11.94/TMS/Files/APP1.apk")
    
End Sub
Sub JobDone (job As HttpJob)
   Log("JobName = " & job.JobName & ", Success = " & job.Success)
   ProgressDialogHide
   If job.Success = True Then
      Select job.JobName
      Case "job2"
            Log(Path)
            If File.Exists(File.DirRootExternal , "/tmp.apk")=True Then
                File.Delete(File.DirRootExternal,"/tmp.apk")
            End If
            MyStream = File.OpenOutput(File.DirRootExternal, "/tmp.apk", False)
            MyStream.InitializeToBytesArray(1)
            File.Copy2(job.GetInputStream,MyStream) ' save the file
           
            If File.Exists(File.DirRootExternal , "/tmp.apk")=True Then
                Log("True")
            End If
            Log(File.Size(File.DirRootExternal,"/tmp.apk"))
           
            Dim iIntent As Intent
            Dim k As String="file://" & File.DirRootExternal & "/tmp.apk"
            Log(k)
            iIntent.Initialize(iIntent.ACTION_VIEW,k)
            iIntent.SetType("application/vnd.android.package-archive")
            StartActivity(iIntent)
        End Select
    End If
    job.Release
End Sub
I still get package parsing error
so i check the file size and return 0,Why?is there something miss?
 

NJDude

Expert
Licensed User
I think you are complicating things somewhere, the attached sample works (sorry but I don't use httputils, however you'll get the idea)
 

Attachments

Top