Reading and editing files on a removable SD card, Android 8

Hello,

On my Samsung Galaxy S9 (Android 8.0.0), I have a removable SD card named "SD" with the following files : MyText.txt, MyPicture.jpg and History.txt.
I want to read MyText.txt and display its content, display MyPicture.jpg, and finally add text to History.txt.

However, when I attempt this, I get the following message : "the file /sdcard/MyText.txt could not be found" (and the same for History.txt and MyPicture.jpg).
I have tried multiple alternative paths, such as file:///storage/emulated/0/..., /MyText.txt (without /sdcard), and so on. I am all the more surprised as App Inventor states that : "Prefix the file name with / to read from a specific file on the SD card".

Could you please provide me with some help ?
Thank you,
A.F.

where exactly is that file stored?
which Android version are you using for your tests?

Remember: to access non media files which are not owned by your app you need SAF, see also

Taifun

1 Like

The file is stored directly on my SD card. I am using Android 8.0.0. Could you please explain what an SAF is? I am not familiar with programing.
Thank you very much for your help.

you answered immediately without reading the link...
what about first reading carefully the basics?
SAF = Storage Access Framework

I'm assuming you are talking about an non removable (emulated) sdcard...
for older devices afaik this still should work (I might be wrong, but I'm sure, @Anke will correct me then :grinning:)

set the default file scope in the project properties to Legacy
and for the image try this full path
file:///storage/emulated/0/MyPicture.jpg

for the file component set the file scope of the file component to Legacy and try
file:///storage/emulated/0/MyText.txt

Taifun

1 Like

Sorry for that, I have read the article you sent me, and now better understand SAFs.
No, I am talking about a removable sdcard, hence not emulated I guess.
I tried the paths you suggested, but I get the message "file not found"...
Do I necessarily need an SAF to open, read, and write these files from my removable sdcard, or can I use only the File component on the App Inventor ?
Thank you very much for your help and your time.

The file component is not able to read from a removable sdcard, use the MFile extension together with a full path

I currently don't know, if the MFile extension is able to return the path to the removable sdcard... if not, the file extension and its AvailableStorageDirectories method can help

Reading and editing files

You will only be able to read, but not write, see also Copy file from removable SD card - ASD (Taifun File Extension) - #23 by Anke

Taifun

1 Like

Thank you very much for your explanation.
I will try to see if the MFile extension could work, but is there preferentially a way to avoid using such extensions and do the same result (read a text file, write a text file, and display a picture)? For instance, by storing these files directly on the phone's "Files" ? If yes, how should I proceed and which paths should I use ?
Besides, here is the information provided for the File component :


So when they talk about an SD card, do they mean a non-removable SD card (just to be sure)?
Thank you very much.

Yes.

On older Android versions you can use any path on the emulated sdcard or directly the root directory

/storage/emulated/0

Taifun

1 Like

So it is not possible on Android 8.0.0 ? For which version is it possible ?
Thank you,
AF.

Older Android versions means older than Android 11
Taifun

1 Like

Note:

  1. With Companion you do not need to request READ permission, because it is granted by default.
  2. You can't use the File component, because it uses a relative path (and there is no relative path to a removable SD card).
  3. A media file can be accessed from any location with the appropriate permission(s).
1 Like
  1. "(Reading and) editing files on a removable SD card, Android 8"
    Since Android 4.4 (API 19, KitKat) it is no longer possible to edit/save files in a removable SD card.
1 Like

Thank you very much @Anke and @Taifun , it runs perfectly with the paths you suggested. I had initially not set the default file scope to "Legacy", hence the problem.

1 Like