Android Tutorial [B4X] TextEditor - Save and load external files

This is a B4i and B4A example, which demonstrates various external files related tasks.

1626784777442.png
1626784790557.png


The behavior is not exactly the same as the platforms capabilities and behavior are different.

B4A
Note that no permission is needed. The minimum version was set to 7 (API 24), though it will probably work in older versions as well.
Don't miss the manifest editor additions and ime in the main module.

B4i
Don't miss the main module PListExtra attributes and OpenUrl event.

A bit related B4J example: https://www.b4x.com/android/forum/threads/class-recent-files-manager.104633/post-822842


Updates:

- 9/2023 - Fixed an issue in B4A where overwriting existing files didn't trim file. Thanks @noeleon for reporting!
 

Attachments

  • TextEditor.zip
    184.2 KB · Views: 767
Last edited:

Roger Daley

Well-Known Member
Licensed User
Longtime User
Erel, great example and possibly what I need.

A couple of dumb questions. [Due to health issues I've done F.A. on B4A for ages, so the questions are probably dumber than usual.]

I have a couple of Apps [not in B4X Pages] that create their own folder in the root directory and use this folder as an import/export location between App and user. With the ongoing changes for access to secondary/external memory I have become completely confused as to where we stand and what is coming. Sometime ago a notice from Google appeared to state that Apps requesting access to this memory would be removed from Playstore in May so I nobbled those Apps to avoid this. This did not happen.

1. Currently this texteditor can read/write to the SD Card. Will this still work when Google make it's forecast changes to memory access?
2. Is there a way for the App to check/create a folder on the SD Card?

Regards Roger
 
Last edited:

Roger Daley

Well-Known Member
Licensed User
Longtime User
1. Yes. It follows Google recommendations.
2. With this approach you are letting the user choose whichever folder they like so it is not really relevant to create a folder. Your program cannot directly create a folder with the "SaveAs" approach.
Thanks Erel,
I guess I'm starting with a blank page and two lot's of code. One in pages one not. It will come together eventually.

The next bit will be dealing with the data in a database and getting a html help file from assets to the real world. That's for another day. 🙂🤔

Many thanks
Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

Trying to save using name of existing file.
If I type in the file name and SAVE, the file is saved with the addition of (1). EG FileX.txt becomes FileX.txt(1) . Leaving the original in place
If I click on FileX.txt in the file list this become the file name. When I click SAVE a pop-up queries if it should overwrite the existing file.

How can I change to always query overwriting existing files?

Regards Roger
 

Mahares

Expert
Licensed User
Longtime User
How can I change to always query overwriting existing files?
If you want to overwrite the file, simply when it prompts you with the newfile.txt, scroll down the screen to locate the original file and click it. It will ask you if you want to overwrite, click yes to preserve the original name.
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
If you want to overwrite the file, simply when it prompts you with the newfile.txt, scroll down the screen to locate the original file and click it. It will ask you if you want to overwrite, click yes to preserve the original name.

Thanks again Mahares but I want to avoid relying on the user checking to make sure that the file name does not already exist.
If a user SavesAs FileX.txt and does not see that FileX.txt already exists they get FileX.txtx(1). Surprisingly some users see this as a problem.

Regards Roger
 

LucaMs

Expert
Licensed User
Longtime User
Since this is the thread and example reference for file handling, load and save, I used this to allow the user to select and play an audio file.

I just changed the MIME type in the Sub Load:
B4X:
cc.Show("audio/*", "Choose audio file")
Although it seems to work fine, this is the contents of the LoadResult:
B4X:
    Wait For (FileHandler1.LoadAudioFile) Complete (Result As LoadResult)

    If Result.Success Then
        Log("DIR: " & Result.Dir)
        Log("FN : " & Result.FileName)
        Log("RN : " & Result.RealName)
    End If

DIR: ContentDir
FN : content://com.android.providers.media.documents/document/audio%3A2247
RN : Dream_It_Possible.flac

I cannot use MediaPlayer passing those values to it, of course.
 
Last edited:
Top