Problem reading file in assets/media

86 views
Skip to first unread message
Assigned to ewpa...@gmail.com by steve....@gmail.com

Ori Avihail

unread,
Dec 6, 2018, 8:27:47 AM12/6/18
to MIT App Inventor Forum

Problem reading file in assets/media using "File.Readfrom" block

The "File.Readfrom" documentation is saying:

"… To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a filename does not start with a slash, it will be read from the application's private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion"

It is working indeed when I "build" and install and application.

But not working when I "connect" using the companion!

1) Why?

2) Is there a way to read a file in assets when using the companion?

I attach an example AIA (click the "get file" button

Message has been deleted

Ori Avihail

unread,
Dec 6, 2018, 8:42:53 AM12/6/18
to MIT App Inventor Forum

Taifun

unread,
Dec 6, 2018, 9:01:11 AM12/6/18
to MIT App Inventor Forum
If you are asking for help, I recommend you to make it as easy for others to be able to help you ...
You probably will get more feedback then...

which means in your case post a screenshot of your relevant blocks...

To download the aia file, upload it to  App Inventor, open it, do some bug hunting for you, etc... this takes time, and most people will not do that...
Thank you.

Taifun

Trying to push the limits of App Inventor! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun. 

Ori Avihail

unread,
Dec 6, 2018, 9:09:07 AM12/6/18
to MIT App Inventor Forum
Actualy it is very simple (I think it is a bug). The attached block in the picture works with build and does not work with companion

FileRead.JPG

Ori Avihail

unread,
Dec 6, 2018, 9:11:08 AM12/6/18
to MIT App Inventor Forum
Of course I uploaded the file to "media"

SteveJG

unread,
Dec 6, 2018, 9:30:36 AM12/6/18
to mitappinv...@googlegroups.com
I believe this is a bug introduced with Companion 2.48.

I get this response  attempting to read a file in Media using the File component using Companion on an Android 4.2.2 Tablet

Fileerror.PNG


You are correct Ori, prefixing your file with // should allow you to read a text file in Media using Companion.


Regards,
Steve


Evan Patton

unread,
Dec 6, 2018, 10:36:08 AM12/6/18
to MIT App Inventor Forum
Hi Ori,

This bug was reported in another post on the forum. I've already implemented a fix and it is queued for review.

Regards,
Evan

Abraham Getzler

unread,
Dec 6, 2018, 11:46:57 AM12/6/18
to MIT App Inventor Forum
(added to subsection Companion 2.48 bugs of section Companion and Emulator of FAQ)
ABG

John Fig

unread,
Dec 8, 2018, 12:02:38 PM12/8/18
to MIT App Inventor Forum
Do you have a date for when the fix will be released? I am trying to use this functionality and it is not working as of today.
Thanks

SteveJG

unread,
Dec 8, 2018, 12:19:37 PM12/8/18
to MIT App Inventor Forum
@John   .. a guess to the Fix might be late December.    MIT announced it would be fixing some bugs within that time-frame and hopefully this will be fixed then.

Be aware, if you compile your Project, the File should be found on your device now.  Yes, it will probably work.  The problem is only finding the file using Companion.    Try this:   Build you app, then load it on your device and test it.  You should be able to use File to read the file.   It is only when a developer uses Companion the problem occurs.  Why don't you build the apk and test and tell us what happens?

Regards,
Steve

TimAI2

unread,
Dec 8, 2018, 12:22:08 PM12/8/18
to MIT App Inventor Forum
Use the full path to the file, and it should work

/
file:///storage/emulated/0/
//
file:///storage/emulated/0/AppInventor/assets/

Ori Avihail

unread,
Dec 8, 2018, 2:53:19 PM12/8/18
to MIT App Inventor Forum
Thanks TimAI2

Can someone tell me the full path to access a file in assets using the companion?

file:///storage/emulated/0/AppInventor/assets/file.csv
is not working

and also not:
file:///storage/emulated/0/file.csv

SteveJG

unread,
Dec 8, 2018, 2:59:32 PM12/8/18
to MIT App Inventor Forum
Unfortunately Tim's advice will not help you Ori... no one can do what you want to do with the File control and live development using the Companion.  The Companion's path is determined withing the Companion code.  Tim missed that the problem occurs while using Companion but if you build and run the app. 

See my previous post where I answered John's question.

-Steve

Ori Avihail

unread,
Dec 8, 2018, 3:14:41 PM12/8/18
to MIT App Inventor Forum

Thanks for all your help up till now.

This situation is bad for one of my students who needs to access a file in assets using the companion. As I wrote, it is working using "build", but development is harder and takes much more time. Thanks for a fast solution. (It seems this will be problematic for many people)

SteveJG

unread,
Dec 8, 2018, 3:35:25 PM12/8/18
to MIT App Inventor Forum
Sorry, the student will have to build the apk to test the File that is loaded from assets.

Here is a potential work around the student might use while awaiting the fix.    If the student's file is  a text file  or a csv, the  information within the file can also be read directly by hard coding the information into the Blocks.

For example:     Label1.Text =   a Text block with  plain text   "now is the time for all good friends.."

or         myCSVFile (where myCSVFile is a csv) =  a Text block with  the csv  "1,2,3,4,55,4, ..."  

that are DRAGGED and DROPPED from the  file that you have loaded into assets....

Something like that might help until MIT is able to fix the behavior of File + Companion live development.  




Ori Avihail

unread,
Dec 8, 2018, 3:56:58 PM12/8/18
to MIT App Inventor Forum
I really thank you for your help. (and sorry to bother you again) but it looks like:
/AppInventor/assets/UNITS.csv
is working for me

SteveJG

unread,
Dec 8, 2018, 4:37:10 PM12/8/18
to MIT App Inventor Forum
Wonderful Ori,

You discovered
ReadFrom(text fileName)
Reads text from a file in storage. Prefix the filename with / to read from a specific file on the SD card (for example, /myFile.txt will read the file /sdcard/myFile.txt). To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a filename does not start with a slash, it will be read from the application's private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.   That reading from the sdcard to use the app's private storage.

 
That may be the best work around until it is possible to use  // to read directly from Assets.




TimAI2

unread,
Dec 8, 2018, 7:50:45 PM12/8/18
to MIT App Inventor Forum
Bah!

My problem was for html files being sent to a webview by activity starter

Still not sure how to access the private storage area...

Taifun

unread,
Dec 8, 2018, 7:59:25 PM12/8/18
to MIT App Inventor Forum
I really thank you for your help. (and sorry to bother you again) but it looks like:
/AppInventor/assets/UNITS.csv
is working for me

this will work while using the companion app, but not anymore after building the app 
Taifun

Taifun

unread,
Dec 8, 2018, 8:00:55 PM12/8/18
to MIT App Inventor Forum
My problem was for html files being sent to a webview by activity starter
Still not sure how to access the private storage area...

another app will not be able to access private data of your own app
therefore copy the file to the internal sdcard and access it from there

Taifun
Reply all
Reply to author
Forward
0 new messages