Uploading files to a device

Andras

Active Member
Licensed User
Longtime User
I think I understand how to use the httputils library to upload materials from a website to a device (haven't tried it yet, but it looks straightforward enough!) but how to I put the uploaded files (db files among others) where the application will know how to find them - or, thinking about it, right away?

Specifically I need to be able to upload both db and jpeg files so as to regularly (monthly or so) push a data update onto devices.

Thanks for help!

John
 

wl

Well-Known Member
Licensed User
Longtime User
Several options:

- pop: you could get the files through HTTP when your application starts up, or every so many minutes while your application is running (using a service and " StartServiceAt")

- push: you could effectively push a message to your Android device that your app should obtain the new files. The push message would just be a singal to your app it needs to obtain these files.

For the later option you can use C2DM.
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
Thanks, and yes, I understand all that already.

I'm just not sure where the uploaded files should go on the device! I presume to some particular folder, but I don't know what the directory structure is, or whether it's protected in some way.

JOhn
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Andras,
I note that you are going to be uploading database files - I would suggest onto the SD card (File.DirDefaultExternal) may be the best place depending on the size of the database files - Its all too easy to fill internal memory with large databases.

Cheers,
Douglas
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
Thanks for replies - I'm going to do some testing, but may be back with more questions later. Either way, very helpful so far!

John
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
I must be doing something wrong (nothing new there, then!) - either that or I've got completely the wrong end of some stick or other.

The obvious place to get to grips with this seems to be with the httputils program; but it doesn't actually seem to do anything, either on the emulator or on an actual device, other than display the B4A logo.

?

Thanks!

John
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Andreas,
I assume you have copied the HttpUtils & HttpService modules into the 'Modules Tab' otherwise you'd get ane error, not just nothing happening.
The code below does the tick for me - obviously any variables need to be declared as appropriate.
This is take from a current project which uses to HTTPUtils jobs. The first calls a PHP page with the date of the current file, this is compared to the date of the current data available for download, and only if the data is newer is it made available for download via the 'Job2' call.

B4X:
Sub Button_InstallUpdate_Click
   datasrc = "http://www.somewhere.com")
   ToastMessageShow("Downloading Update - May take a minute or so. . .",True)
   'Log("Database=" & datasrc)

   HttpUtils.CallbackActivity = "Main" 'Current activity name.
    HttpUtils.CallbackJobDoneSub = "JobDone"
    HttpUtils.Download("Job2", dlfilename)
   
End Sub

Sub JobDone (Job As String)
   Dim updatestatus As String
    Dim In As InputStream, Out As OutputStream
   
   If HttpUtils.IsSuccess(dlfilename) Then
      Select Job 
         Case "Job1"
              '......
         Case "Job2"
            ' Handling input stream
            '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            If HttpUtils.IsSuccess(dlfilename) Then
               In = HttpUtils.GetInputStream(dlfilename)
               
               Out = File.OpenOutput(File.DirDefaultExternal, "update.db", False)
                File.copy2(In,Out )
               Out.Flush
               Out.Close

               File.copy(File.DirDefaultExternal,"workingfilename",File.DirDefaultExternal,"workingfilename.bak")
               File.copy(File.DirDefaultExternal,"newfilename",File.DirDefaultExternal,"workingfilename")
               '
               ToastMessageShow("New Data downloaded OK!", False)
            Else
                ToastMessageShow("Problem Downloading Data!", False)
            End If   
      End Select
    Else
      Button_InstallUpdate.Enabled = False
      ToastMessageShow("Can't Connect to Server: Check Internet Connection or Try Later!",True)      
   End If
End Sub
Hope this gets you off in the right direction - it took me a while to get it right, even with the examples available!

Cheers,
Douglas
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
I can now reliably upload a db file to the appropriate device - which seems like something of a minor miracle, I can say! I know it's there on the SD card, I can look at it and see that it matches the date and time it was uploaded and so on.

However, the program refuses to see it, and clearly I need to be able to put it somewhere where it's visible to the software - or, better, tell the software where to find it. The cursor is already pointed at File.DirDefaultExternal, which is where the file is physically located, so I'm stuck.

Trying to copy it to File.DirAssets simply results in a Java 'file exception error - no such file or directory'.

What am I doing wrong?

Ta for help!

John
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
you might be at the point where you need to upload a code snipplet for us to help

Well, I know that the db file (places.db) is on the SD card as I can see it there in the correct version; I then try to access it with the following:

PHP:
If File.Exists(File.DirDefaultExternal, "places.db") = False Then
   File.Copy(File.DirAssets, "places.db", File.DirDefaultExternal, "places.db")
End If

mSQL.Initialize(File.DirDefaultExternal, "places.db", True)

and then later read it with:


PHP:
mCursor = mSQL.ExecQuery("SELECT * FROM places where Type  = '" & a & "' order by Name")

This works perfectly well as such; it just doesn't pick up any changes made to the db file.

Help much appreciated!

John
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…