Android Question Storing File location in a database

aklisiewicz

Active Member
Licensed User
Longtime User
I have following code to display an HTML document

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.Title = "Document Content"
  Activity.LoadLayout("DocumentFormView_LT")
  url = DocumentList.selectedURL
  WebView1.Initialize("WebView1")
  Activity.AddView(WebView1,0dip, 0dip, 100%x, 100%y)
  WebView1.LoadUrl(url)
  ProgressDialogShow("Loading " &url)
End Sub

The code seems to work but when I click on the document item to display it in another activity I get path error (see image). This is probably due to the wrond path stored in the table (which you can see on the image).

If the file to display is: MyFile1.html
what is the proper file path to store in SQLite record ?

how can I have MyFile1.html store in a separate folder or subfolder (so those files are separated from the rest) ?

Arthur
 

Attachments

  • b4a_sc03.png
    b4a_sc03.png
    55.5 KB · Views: 170

LucaMs

Expert
Licensed User
Longtime User
In a field of your db you CAN put only the file name, without path.
Before, create a sub directory in the path (usually) DirDefaultExternal,
using something like: File.MakeDir(File.DirDefaultExternal, "MyHTML");
then create a "fix variable" for the dir (Public HTMLDir as String = File.DirDefaultExternal & "/"MyHTML/")
and use ever: url = HTMLDir & FilenameFromDB.
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
LucaMS - That sounds exactly like what I needed - thanks a lot!
I assume I can sort out and store other types of files this same way. I do not like to keep everything in single folder.

If you don't mind let me extend this question. Let's say this approach is working. Now I need to open an HTML file in a WebView. I have already learned how to do it, but now I need to display an image and store an images in a separate folder
Any hints ?


Arthur
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
LucaMS - That sounds exactly like what I needed - thanks a lot!
I assume I can sort out and store other types of files this same way. I do not like to keep everything in single folder.

If you don't mind let me extend this question. Let's say this approach is working. Now I need to open an HTML file in a WebView. I have already learned how to do it, but now I need to display an image and store an images in a separate folder
Any hints ?

"...other types..."

An idea would be:
a field for Types ("htm", "html", "txt", "b4a"...);
a field for FileNames;
a field for Directories.

I have not used WebView: you want to superimpose an image to it? if it were so, you could probably use the method DrawBitmap of canvas object and Bitmap.WriteToStream to save the image
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
I'm having some problems implementing the path to the HTML file
Here is relevant code

In MAIN module:
B4X:
Sub Process_Globals
    Dim ProductionMode As Int = 0
    Dim MediaPlayer1 As MediaPlayer
    '----------------------------Start SQLite--------------------------------------------------------------------------------------------------
    Dim dbFileDir As String                        :dbFileDir = File.DirInternal
    Dim dbFileDirEx As String                  :dbFileDirEx = File.DirDefaultExternal
    Dim dbFileName As String                :dbFileName = "edbq_lite_LOC.db"
    Dim UserHTML As String = File.DirDefaultExternal & "/UserHTML/"
    Dim dImagesPath As String = File.DirDefaultExternal & "/dImagesPath/"
  
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim SQL1 As SQL
    Dim btnIntro As Button
    Dim btnAudio As Button
    Dim btnIssues As Button
    Dim btnProducts As Button
    Dim btnDocuments    As Button
    Dim lblVersion As Label
    Dim Panel1 As Panel
    Dim Timer1 As Timer

    Dim btnCS As Button
    End Sub
Sub Activity_Create(FirstTime As Boolean)

    If FirstTime Then
        File.MakeDir(File.DirDefaultExternal, "UserHTML")
        File.MakeDir(File.DirDefaultExternal, "dImagesPath")
        File.Delete(File.DirInternal, dbFileName) ' only for testing, removes the database
        'Check if the database already exists
        If File.Exists(File.DirInternal, dbFileName) = False Then
            'copy the default DB
            File.Copy(File.DirAssets, dbFileName, File.DirInternal, dbFileName)
            'if not, initialize it
            SQL1.Initialize(File.DirInternal, dbFileName, True)
            'and create it
            'CreateDataBase
            'copy the default DB
            File.Copy(File.DirAssets, "edbq_lite_LOC.db", File.DirInternal, "edbq_lite_LOC.db")
        Else
            'if yes, initialize it
            SQL1.Initialize(File.DirInternal, "edbq_lite_LOC.db", True)
        End If
    End If
    Activity.LoadLayout("Main")
In a field of your db you CAN put only the file name, without path.
Before, create a sub directory in the path (usually) DirDefaultExternal,
using something like: File.MakeDir(File.DirDefaultExternal, "MyHTML");
then create a "fix variable" for the dir (Public HTMLDir as String = File.DirDefaultExternal & "/"MyHTML/")
and use ever: url = HTMLDir & FilenameFromDB.


then in the activity I have this:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
  Dim WebView1 As WebView
  Dim url As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.Title = "Document Content"
  Activity.LoadLayout("DocumentFormView_LT")
      url = Main.UserHTML & DocumentList.selectedFile
  WebView1.Initialize("WebView1")
  Activity.AddView(WebView1,0dip, 0dip, 100%x, 100%y)
  WebView1.LoadUrl(url)
  ProgressDialogShow("Loading " &url)
End Sub

for some reason I get en error when I try to open the file (see attachment)
 

Attachments

  • des9.png
    des9.png
    62.3 KB · Views: 134
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
Unfortunately it did not help. I still have Page Not Found error
I noticed I have another procedure where the filename is (at this moment) hardcoded lie this
B4X:
url = "file:///android_asset/yl_intro1.html"
and it works

So, where do I need to put my HTML file ?

where is the physical location of the file path shown on the Error message (another words where is it on the PC) ?

Arthur
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
No, it is not enough!

You should copy them from "Files tab" (that corresponds to File.DirAssets) to your "UserHTML" folder.

B4X:
File.Copy(File.DirAssets, "MyFileName.html", File.DirDefaultExternal & "/UserHTML/", "MyFileName.html")
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
for some reason I still get Error.


???

Art
 

Attachments

  • des11.png
    des11.png
    69 KB · Views: 147
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
OK, great - thanks. Now - is it possible that instead of using a single HTML file I have ZIPed file with several HTML files and upon installation UNZIP them to to the:

File.DirDefaultExternal


???

Art


uhm.... thinking, so i'll not write the best reply :).

Yes, you can (I've heard this phrase :)).

Your zipped file should be in the DirAssets anyway (however, I read that you can add external files in the space that Google provides you, when you publish an app, but I don't know well this).

Then you should unzip the files and copy them to the "UserHTML"

P.S. more simply, on site there are certainly libraries for zip/unzip files. Probably a folder full of subfolders and files.

ABZipFile
 
Last edited:
Upvote 0
Top