Android Tutorial Text files

klaus

Expert
Licensed User
Longtime User
Hi Sarah,
There is an example here: add imageview half manually

The program loads image files from a folder and shows them in a ScrollView with ImageViews.
In the code you should either choose the right code line for the folder you want or change one line with your folder.
B4X:
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]bmpImage.Initialize([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]File[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].DirAssets,[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Image"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]&i&[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]".jpg"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]' bmpImage.Initialize(File.DirInternal,"Image"&i&".jpg")[/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=Courier New][COLOR=#008000]' bmpImage.Initialize(File.DirDefaultExternal,"Image"&i&".jpg")[/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
The default folder is in the program is File.DirAssets.

Best regards.
 

Gearcam

Active Member
Licensed User
Longtime User
Dir list and select text file to open

I want to be able to list the text files in a perticular dir on the SD card (not downloaded with app) then scroll down them, select one in list view and open it.

I can do the opening etc but how do you read a directory with all the files in it ?

thanks

Steve
 

alexb

Member
Licensed User
Longtime User
Cannot compile example

Hi Erol,

thanks for this great product and your outstanding support!

I have a quick question, for some unknown reason I cannot compile the example that you have posted to test the different file read/write functions. I copied and pasted it into a new project but when compiling I get the error message shown in the attached screenshot. Note: I do not have such error with any other project so far and I downloaded and tested quite a lot.

Btw: Is there anywhere a function such as 'GetProfileString/WriteProfileString' allowing to read/write valuenames/values in a given[SectionName] of a txt file?

Thanks for your help in advance!

alex
 

Attachments

  • Error Android Manifest.png
    18.6 KB · Views: 746

alexb

Member
Licensed User
Longtime User
Compilation issue solved

Oh, I think the compilation works now. After I deleted virtually all the source code I still had the problem, so had to be something in the header I thought. It turned then out that I do not get the compiler error message when changing the Aplication label from 'FileRead&Write" to simply 'FileReadWrite'. Didn't know that '&' sign is not allowed.

Anyway, could you reply to my 2nd question (Read/WriteProfileString).

Thank you!

alex
 

alexb

Member
Licensed User
Longtime User
... writing to a different section

Dear Erel, thanks for taking time to look at this!

I intend to use a script file for controlling some operations of the program that I am working on. A user or myself will then later be able to simply change the script to change the program behaviour rather than hardcoding customizations. So the script file will be a lot more complex than a simple INI file with a few keys and values to read/write for program initialization. Structurally this could look like this:

[ActionSection1]
Action1=DoThisThat
Action2=DoThisThat2
etc.

or simplier (but not preferred)
[ActionSection2]
DoThisThat
DoThisThat2

The use of sections makes it easier to read, understand and modify the script file.

For writing/reading such sections I have used in the past commands similar to the following:

ReadProfileString("MyScriptFile.INI", "ActionSection1", "Action1", %ActionToDo%)

or

WriteProfileString ("MyScriptFile.INI", "ActionSection1", "Action1", "DoThisThat")

I wonder if there exists anything as simple as this under B4A or how it could be emulated best. My understanding is that with WriteMap/ReadMap I can manipulate pairs of Keynames & Values whereas I require a 'third dimension' with the sections:
[SectionName]
Key/Valuename=Value

I believe other users might be interested in such a funcitonality as well, maybe not for a script file but for better readability of INI files. If a new library is required for doing this I am afraid my skills won't allow me to write and provide such a library.

Thanks for your attention.

alex
 

alexb

Member
Licensed User
Longtime User
Thanks for your reply Erel!

I am thinking about using RandomAccessFile.Read/WriteObject as you suggest but wouldn't I loose the capability of easily modifying the script file if I use a binary file as input file? Is there anywhere an example of writing/reading a user defined type or could you illustrate using my Read/WriteProfileString example as a starting point?

Thank you, alex
 

alexb

Member
Licensed User
Longtime User
Erel, I have decided to use TextReader for creating my ReadProfileString functionality:

'This returns the contents of the whole section (regardless whether the section contains just lines or pairs of keys/values)
ReturnValue = ReadProfileString(File.DirAssets,"INIFILE.INI","Section3","")

'This returns only the specified line #4 (= 5th line) within the section. Lines starting with a comment ';' or empty lines are ignored
ReturnValue = ReadProfileString(File.DirAssets,"INIFILE.INI","Section3","4")

'This returns the value matching the specified key
ReturnValue = ReadProfileString(File.DirAssets,"INIFILE.INI","Section2","Key_desc4")

I hope I caught all/most of the possible error conditions:
'Error 00 = section not found and EOF (defined by [End] section)
'Error 01 = key exists but no value assigned (missing value)
'Error 02 = key exists but no value assigned (missing '=' sign)
'Error 03 = section exists but key not found
'Error 04 = section name not found
'Error 05 = empty section
'Error 06 = invalid line number!

This is not rocket science but maybe it's useful for some others, so I post it here.

Certainly it's not economical to open/close every time the file for reading just a value, a line or a section - in particular if the file is big. I would prefer to open the file once, load the contents to memory and work from there. But don't know how to do that. Any comments, hints on errors and improvements welcome.

alex
 

Attachments

  • ReadProfileString 1.0.zip
    6.9 KB · Views: 952

Mickego

Member
Licensed User
Longtime User
Here is a ini.bas handling INI file Write/Read

I made a quick and dirty ini.bas that handle my needs
maybe someone else can use it.

(Info C&P from bas-file)
###################################################

' Functions to Read and Write ini-file from Activity

' ini.writeini(File.DirDefaultExternal,"test.ini","Section 1","TestKey1","Sunday")
' ini.writeini(File.DirDefaultExternal,"test.ini","Section 1","TestKey2","Monday")
' ini.writeini(File.DirDefaultExternal,"test.ini","Section 2","TestKey1","Tuesday")
' ini.writeini(File.DirDefaultExternal,"test.ini","Section 2","TestKey2","Wednesday")
' Creates this
'
' (File test.ini)
' [Section 1]
' TestKey1=Sunday
' TestKey2=Monday
' [Section 2]
' TestKey1=Tuesday
' TestKey2=Wednesday
'
' To read 1 of above Keys into variable 'str'
' str = ini.readini(File.DirDefaultExternal,"test.ini","Section 1","TestKey1")
' str will be set to Sunday
' ################################################


No error handling,

Simply return "" if not actual "Section-Key:Value" exist

rgds Mickego
 

Attachments

  • ini.bas
    3.1 KB · Views: 836

margret

Well-Known Member
Licensed User
Longtime User
Read & write to data files

I am new to B4A and I have no knowledge of how to work with data files. I have looked and read through the samples and I am still lost. I have been programming for 20+ years but in database apps like Foxpro, dbase, etc. I also have written many programs in gwbasic and understand that fine. I am looking for help to use B4A like dbase. I would like to have fields like name, address, zip, etc. in one record. I would like to be able to pull like the address from record 30 for example. I would also like to have a one record file that may have 30 different fields(varibles) in it. Any help would be great. I have been working on my first app and have all the menus and inputs working fine. The form designer works great, now I just need to know how to read/write the data to files and how to control where the files are written(path or directory). I really don't want them on the SDCard.

Thanks for any help.
Margret
 

zouriteman

Member
Licensed User
Longtime User
ReadList

EREL ,

why ReadList , with a non-existing file , does not start a runtime error ?

and does not initialize the string list with ZERO items ?


In my mine , this is a bug ?
 

Carol

Member
Licensed User
Longtime User
Redirect file loading link

Hi all, I am trying out the file reading example on the following link
Files - Basic4android Wiki
on my Asus android pad and so far I've not been able to successfully read the file since the File.DirRootExternal is /mnt/sdcard but the file exists in external_sd folder. Apprently, File.[location] there is no location that points to external_sd folder. Is there a way to directly specify the folder? thanks a lot.
 

Carol

Member
Licensed User
Longtime User
hmm how about from
/mnt/sdcard to
/mnt/external_sd?

May I ask how I can do that?
Thank you very much!
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…