Companion and APK: How to access files on Android

Do you need to edit the database, or just read from it ?

in this case I need just to read but I would prefer to have a solution that may work also to write. I am using an extension made by you that returns the assets path, does it return a different result working with companion or without? Because with companion it works perfeclty, if it returns the assets path accordingly if the app works with companion or without (I mean an apk installed on the phone) could be the simplest solution.

Try this

I made some tests and now the situation is a little bit more clear. I am able to test and debgu using Companion, the DB is located in /storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/file/assets/mydb.sqlite This path is returned calling the methods of SQLite extension and also your tools extension. Testing the apk the results are different. Your tools extension (and another couple of similar extensions) return /storage/emulated/0/Android/data/appinventor.ai_massimomanca_es.TestSQLite/files and the SQLite1.DatabasePath returns /storage/emulated/0/android_asset
But android_asset does not exist and also I did not find any other mydb.sqlite DB anywhere else. So, where AI2 copies mydb.sqlite installing the apk? I need a good file finder tool working on Android...

The sqlite extension is out of date (not likely to be updated) and not handling the new compiled path to assets. It also cannot cope with being fed the PathToAssets blocks from Taifun's Tools extension, and crashes the app.

Because of this you should try as I suggested and copy the database to your ASD, as in my blocks above, then access it from there (which also gives you read/write)

Your alternative is to use Taifun's SQLite extension, which is up to date and full support is provided. I would recommend this course of action given your determination to access the database from assets when compiled.

(note: the File extension is also by Taifun, not me...)

1 Like

Is there really a new path to the assets for the APK?
Since when?

well it is not this! (which is embedded in the extension)

see the source code

Honestly, at the moment I am a little bit confused... I have too many different results. I am sure that someone depends by the extension(s) but someone is coming from Android 11. I think that the simpler thing from a management point of view is find a way so that uploading a DB on the Media it will work both with Companion and apk. It may be a little bit more complicated to realize but seems to me that is the way to solve and prevent any DB update problems.

Exactly, seems there is something wrong in the SQLite extension about default path management.

Try this with the APK:

31 posts were split to a new topic: Webviewer - HTML embedded image - path to assets / Shared folder

I have also researched about this problem and in essence, there are two problems:

  1. The SQLite Extension (bennedum.SQLite) has a bug, because it hardcodes the path to the SQLite database file, so when App Inventor is updated (along with update to Android 10), the extension cannot import uploaded asset file anymore.

  2. Android 10 fails to read (well, actually fails to copy from asset folder to ASD folder) if the file is compressed in the distributed apk file. Because media files (with media-files-extension of png, mp3, etc.) are not compressed again in the apk file, Android 10 successfully copy them. So, the solution for this problem is to use change to uploaded file extension to become media-file extension (png, mp3, jpg, etc.).

I got the idea after reading a discussion about how to pass file from assets folder to native program (C++). The link to the discussion is here https://groups.google.com/g/android-ndk/c/ppCEAY6Hpag and the part of the discussion that relates to this problem is here:

.

The general work around solution for both the problems (no. 1 and 2) is to (i) upload sql-statement file that build the table(s) for the first time (and insert some initial data) with a media-file-extension, (ii) using block-code to copy the file from asset folder to application specific folder and (iii) then split the sql-statement file into several sql-statements and (iv) execute all the sql-statements.

@Anis_Cherid_UMB

Do you have a working example of this? My experiments in renaming file extensions for non media files did not work ..

:question:

Here is a working example. I hope it helps...

And the aia file...
Sistem_Informasi_RS.aia (126.0 KB)

I (and most) don't have time to reconstruct your project,
aia - please.

I cannot see any need for the file to have the sufffix .png (if it is a text file) when stored in the assets / media folder. This folder is only readable by the app itself....and can easily be opened with the File component.

1 Like

I am not sure if your approach will cause any problem in every android device. But I notice two problems when I upload "sqlstatement.txt" to the asset folder (uploading without the media-files-extension and without running the copy-from-asset-to-asd-folder block) :

  1. It is rather slow to read and parse the text file from asset folder, when running the application for the first time in App Inventor Companion.
  2. The application cannot find the "sqlstatement.txt" when I build and install the apk file to my device (Android 11, colorOS V11.1)
  1. Companion is for development, it does not always behave the same. A large file will take time to transfer across to and back from the AI2 servers.
  2. Sounds like your issue is specific to the extension you are using (bennedum ?), and not a generic issue. I forked the bennedum extension and updated it a while ago, altering several elements, and so that sqlite files could be read from the assets for all android versions.
1 Like

Great job, thank you for making me notice this. I will surely use your Sqlite Extension​:+1::+1::+1: