Getting text file into device system

jschuchert

Active Member
Licensed User
Longtime User
My app creates comma-delimited text files in a certain format, then uses them within the program. Say a user of my app already has that type of file and wants to import it into the app. He has copied it into the root directory of the device (or sd card in the device) from his computer. I need to know how to access it. So far I have used "DirInternal" for all of the files. The Home screen is where the files are opened in an edittext box where the filename is entered and a "txt" extension is added. Then the code is
B4X:
File.OpenOutput (File.DirInternal ,"temp.txt",True)
and opens that file.

Let's say he has copied "temp.txt" into the device's root system. Now what? What about the file on the sd card? What is the syntax of opening a file from there?

A problem I am having with my tablet is that I cannot find the file again on the device after copying it so it is difficult to monitor the process. I have a couple of file managers but they are little help.

Thanks in advance.

Jim
 

jschuchert

Active Member
Licensed User
Longtime User
Thanks Inman but here is additional detail about what I am trying to accomplish. So far everything in my app works good. If a user is creating a file from scratch, he simply enters a name in a text box on the opening screen, then chooses a routine from a menu. This file stays throughout the session. The code used when he taps OK to open that file is:
B4X:
Sub btnOK_click
strfilename=txtFileName.text & ".txt"
writer.initialize(File.OpenOutput (File.DirInternal ,strfilename,True))
End Sub
The files generated are text files that look like this:
1,1000,1000,None
2,1234.567,4567.890,None
3,3456.123,9876.345,None
.
.
etc.
Now assume the user already has a similar file in the same format. Naturally he doesn't want to have to reproduce it again so he copies it from his computer to an sdcard and puts it in his device. How does he now open that file? I finally was able to find files on the sdcard in my tablet using ES file explorer and tried the code above after using "sdcard/<name of file>" in the box asking for a file name: As soon as I tap OK I get the following error message:"An error has occurred in line....(the one opening the file) java.io.filenotfoundexception:/data/data/android.b4a.<name of my app>/files/sdcard/<name of file>

If I change the code to use DirRootExternal vs dirInternal I get this error message: same as above until filenotfoundexception: then "/nand/sdcard/<filename>"

Apparently my app doesn't recognize the syntax to find the file on the sdcard. I'm sure this action will be used frequently by users so I need to figure it out. Another caveat is after it has been opened and used, how does it become part of the internal file structure? I believe I can work out the details if I know how the external file can be seen. Thanks everybody.

Jim
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
I reviewed the wiki re:files, tried several different scenarios with DirDefaultExternal and DirRootExternal, using the entire path, including the package name, etc. according to the wiki but still get the "filenotfoundexception" error. The text file IS on the sdcard but I can't find the right combination to create a good "path" to it. Any other ideas? I looked at Klaus' reply to shane119 posted yesterday that is similar. Klaus did give me a clue how to get it into the internal storage but I still need to get it into my application first.

Jim
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
This is not the ideal solution to this problem, but within the app, why not ask the user to select the file from SD card instead of trying to guess the location?

In the Dialogs library written by agraham, there is a dialog box called FileDialog which shows a file browser to the user for selecting a file. You can show this dialog and ask the user to select the file he copied from PC.

views_filedialog.png
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Try using the File Picker (as shown in the wiki). If you can see the file in the pick list and you select it, then you can put a debugger break point on the TR.Initialize line to see what location and name are being used, then if you do NOT want to use the File Picker, you can just use the same location and file name.

By the way, the code you listed before was "writer.initialize(File.OpenOutput..." but you say now that you are trying to read it. You didn't show the code to read it, but I trust you are using something like: "Dim reader As TextReader: reader.Initialize(File.OpenInput...".
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Both of you have the same solution and it sounds like a great one. Would the code be placed in the Create first time... in my main activity where the aforementioned text box resides? Also, what is the library I need? There isn't one called 'Filedialog' in the libraries folder.

Jim
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Both of you have the same solution and it sounds like a great one. Would the code be placed in the Create first time... in my main activity where the aforementioned text box resides? Also, what is the library I need? There isn't one called 'Filedialog' in the libraries folder.



From the wiki Files page:
  • A file picker displays in a scrollable list all the files (optionally of a specified type) in a specified location (described below). File picker requires the Dialogs library.
So... I guess it requires the Dialogs library. ;)
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Yes, I saw that but in the Libraries folder in the Anywhere Software folder in Program Files, there is no 'Dialogs' library, either. In the meantime, I conducted a little test like this to see if it is seeing the file on the device:
B4X:
strfilename=txtFileName.text & ".txt"
If File.Exists (File.DirRootExternal,strfilename)=True Then
Msgbox("The file is here","yes")
File.Copy(File.DirRootExternal,strfilename,File.DirInternal ,strfilename)
Else
Msgbox("Can't find it","No")
End If
It finds the file. I suppose that means it was in the 'root' directory of the sdcard all along and no further path was needed. However, when I tried to read the contents of the file, it was blank, causing more concern. I suspect even if I were able to select it from the file picker, the results would be the same. Maybe the files created within the application are different somehow from the external files and I won't be able to import as hoped. Regardless, I would still like to get the 'Dialogs' library. I'll continue to experiment and if I come up with a different solution, I will post. I never thought it would be this difficult. Thank you and Inman for your time.

Jim
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thanks, Andy. I did check it out but not able to open the demo because of not having the right libraries. I created a Lib folder in the path for my app and placed the 2 files (dialogs.jar and the xml file) in it. I also configured the paths for additional libraries to reflect the same. Still no luck. However, I have gotten closer with my other process. I can now see the content of files from the sdcard within my app but can't get the data to integrate properly...yet. At least I'm not getting a blank file anymore. Don't have a clue what I did except remove the card a couple of times.

Jim
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Ok, I believe I have gotten the answer and it was pretty straightforward. After discovering that DirRootExternal revealed the file was on the root of the sdcard, and no complex path was required (I had it in my head that "sdcard" has to be part of the path) the light dawned. Here is the code that handles it (at least for now).
B4X:
Sub btnOK_click
strfilename=txtFileName.text & ".txt"
If File.Exists (File.DirRootExternal,strfilename)=True Then
File.Copy(File.DirRootExternal,strfilename,File.DirInternal ,strfilename)
writer.initialize(File.OpenOutput (File.DirInternal ,strfilename,True))
Else
writer.initialize(File.OpenOutput (File.DirInternal ,strfilename,True))
End Sub
Now it doesn't matter whether or not the file is new, existing internally or on the sdcard. I think I tried to make it too complicated. And the wishy-washy nature of my tablet and sdcard didn't help. Again, thanks much to those who gave me some great help and information.

Jim
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Jim,

Have a look at using DirDefaultExternal for storing the files.

This configures a path in your external memory that is based on your package name so all the files can be kept in one directory.

Your users can then copy files to that directory, and your program can scan the directory for them and process them.

I have to say that, compared with some on here at the moment, you are an absolute pleasure to (try to) help.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thanks again Andy. I will check our your recommendation. I would still like to get the 'file picker' from the dialogs library but for whatever reason, it doesn't seem to work for me. I downloaded and opened the dialogs demo but every library declared is not available. I am assuming the internal library is the one within the program files-anywhere software folder. I took the 'jar and xml files and [it them in a separate folder and them reference that in the 'additional libraries path under configure paths. Where did I go wrong?

Jim
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I cannot see from your description where it is going wrong, so I will describe what I did.

1) Create a new folder within the Basic4android folder, named Additional Libraries.

2) Copy the .jar and .xml files for the library into this directory.

3) In "Paths Configuration", for "Additional libraries" I browsed to the folder I created in step 1, this gave me a full path (in the edit field) of:

"C:\Program Files\Anywhere Software\Basic4android\Additional Libraries"

Try that and see if it works.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Same as before. Maybe on or both of those files are corrupt. Why don't you attach a zip containing those 2 files and I'll try it again unless there is another source for them.

Jim
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Andy,

Boy, is my face red or what? After installing the libraries, I had not selected them with the 'libs' tab in the ide. So much to remember...so small a brain. Things work now. Thank you very much again.

Jim
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I find the way to remember thing like this is to unload my brain of trivia (like my age, the wifes birthday, dental appointments, and where I put the car keys) thus creating more room for important stuff.
 
Upvote 0
Top