Android Question Unable to copy file

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Using the latest B4X and latest Android version and suddenly unable to copy DB file from phone to SD.
I think there was an OS update and I take that this is to do with it.

Phone folder is: File.DirRootExternal/PhonePats/: /storage/emulated/0/PhonePats/
SD folder is: /storage/3637-6230/Android/data/b4a.sqlitelight1/files/

Is this indeed a new Android ?permission problem?
Should I store the DB (SQLite) somewhere else?

RBS
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Should I store the DB (SQLite) somewhere else?
Yes. Use File.DirInternal or XUI.DefaultFolder.

Accessing the secondary / external storage is more complicated in Android 10+ devices.

 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes. Use File.DirInternal or XUI.DefaultFolder.

Accessing the secondary / external storage is more complicated in Android 10+ devices.


Thanks, will try that and see if it solves the problem.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes. Use File.DirInternal or XUI.DefaultFolder.

Accessing the secondary / external storage is more complicated in Android 10+ devices.


Having problems finding the folder File.DirInternal to copy the DB to.
If I log File.DirInternal I get:
/data/user/0/b4a.sqlitelight1/files

I think this is:
This PC\Galaxy S10\Phone\Android\data\b4a.sqlitelight1\files when I use file explorer on the PC.

I copied the DB there but my app doesn't see the DB file when I check with:
If File.Exists(File.DirInternal, strMainDBName) Then

How do I find File.DirInternal with Windows Explorer?
I don't want to copy the DB file via compiling the app as it will take a very long time.

RBS
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I think this is:
This PC\Galaxy S10\Phone\Android\data\b4a.sqlitelight1\files when I use file explorer on the PC.
That is not File.DirInternal. It represents: Log(rp.GetSafeDirDefaultExternal("")) . You need runtimepermissions lib.

How do I find File.DirInternal with Windows Explorer?
You cannot.
Do this, you will be able to see if it is there:
B4X:
File.ListFiles(xui.DefaultFolder) 'or file.List(file.dirInternal)

Check this link:
There are so many threads written about permissions, especially lately. It is hard to give you just one link. Check @agraham posts. He has been answering so many questions about it.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Thanks. Serious trouble this file permissions problem. Is there any folder that the DB could be in and where that folder is visible with Windows Explorer?

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes. Run the example in the link I gave you above: 'Saveas....
You will see how it works.

How do I set the directory to copy to in the SaveAs Sub?
I will need to copy a .kvs file (datastorage) from a folder on the SD (I have the path to this) to File.DirInternal.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
That is not File.DirInternal. It represents: Log(rp.GetSafeDirDefaultExternal("")) . You need runtimepermissions lib.


You cannot.
Do this, you will be able to see if it is there:
B4X:
File.ListFiles(xui.DefaultFolder) 'or file.List(file.dirInternal)

Check this link:
There are so many threads written about permissions, especially lately. It is hard to give you just one link. Check @agraham posts. He has been answering so many questions about it.

In the SaveAs code how should I declare ion in Sub StartActivityForResult(i As Intent)?
I guess As Object
That is not File.DirInternal. It represents: Log(rp.GetSafeDirDefaultExternal("")) . You need runtimepermissions lib.


You cannot.
Do this, you will be able to see if it is there:
B4X:
File.ListFiles(xui.DefaultFolder) 'or file.List(file.dirInternal)

Check this link:
There are so many threads written about permissions, especially lately. It is hard to give you just one link. Check @agraham posts. He has been answering so many questions about it.

I tried the file copy with SaveAs:

B4X:
Wait For (SaveAs(File.OpenInput(File.DirRootExternal & "/PhonePats", "PhonePatsE.db"), File.DirInternal & "/virtual_assets", "PhonePatsE.db")) Complete (Success As Boolean)

But I still get a permission problem:
java.io.FileNotFoundException: /storage/emulated/0/PhonePats/PhonePatsE.db: open failed: EACCES (Permission denied)

RBS
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Wait For (SaveAs(File.OpenInput(File.DirRootExternal & "/PhonePats", "PhonePatsE.db"), File.DirInternal & "/virtual_assets", "PhonePatsE.db")) Complete (Success As Boolean
The project in the SaveAs link is designed to access secondary storage. Note that all examples there copy from File.DirInternal to secondary storage. In your case, you need to use this class as you are copying from external storage to internal.
In my case I have not had to use that ExternalStorage to this point to be of much help to you.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
The project in the SaveAs link is designed to access secondary storage. Note that all examples there copy from File.DirInternal to secondary storage. In your case, you need to use this class as you are copying from external storage to internal.
In my case I have not had to use that ExternalStorage to this point to be of much help to you.

OK, will look at ExternalStorage then.
I tried adding the DB file to the project and compile and install like that, but that gave me this error:

B4A Version: 10.70
Parsing code. (0.96s)
Java Version: 8
Building folders structure. (0.23s)
Compiling code. (0.73s)
Compiling layouts code. (0.05s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Compiling resources (1.17s)
Linking resources Error
filemap E 05-25 08:06:40 13848 2476 MapViewOfFile(0, 2598100992) failed with error 8
..\Files\phonepatse.db: error: failed to open file.

It is a large file, about 2.4 Gb

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You cannot add a 2gb file to the APK.

Still not figured this out. This is the code I used copy my DB file File.DirInternal:

B4X:
Sub CopyTest
    
    Dim i As Int
    Dim strFolder As String
    Dim strFileName As String
    
    strFolder = "PhonePats"
    strFileName = "PhonePatsE.db"
    
    File.Delete(File.DirInternal, "PhonePatsE.db")
    
    Dim lst As List
    lst = File.ListFiles(File.DirInternal)
    For i = 0 To lst.Size - 1
        Log(i & " - " & lst.Get(i))
    Next
    
    Dim extFolder As ExternalFile = Storage.FindFile(Storage.Root, strFolder)
    Log("Folder found: " & extFolder.IsInitialized & ", extFolder.Name: " & extFolder.Name)
    If extFolder.IsInitialized Then
        Dim extFile As ExternalFile = Storage.FindFile(extFolder, strFileName)
        Log("File found: " & extFile.IsInitialized & ", extFile.Name: " & extFile.Name)
        If extFile.IsInitialized Then
            Dim out As OutputStream = File.OpenOutput(File.DirInternal, strFileName, False)
            File.Copy2(Storage.OpenInputStream(extFile), out)
            out.Close
        End If
    End If
    
    Dim lst As List
    lst = File.ListFiles(File.DirInternal)
    For i = 0 To lst.Size - 1
        Log(i & " - " & lst.Get(i)) 'PhonePatsE.db is there
    Next
    
    Log("File.Exists: " & File.Exists(File.DirInternal, strFileName)) '>> True
    
End Sub

All seems to work fine, but when I try to connect with SQLCipher it says the file does not exist.
I provide the same folder, File.DirInternal and the same file name.
This turns out to be very difficult.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
1. Post the logs.
2. Where is the code that opens the database file?

All fixed now and this was a very simple and silly mistake.
As the main project is quite large and complex I did this in a small test app.
I overlooked the simple fact that File.DirInternal is not the same in the big app and the little test app.
Silly indeed, but perhaps I am not the only one who ever did this.

RBS
 
Upvote 0
Top