Android File System : How to Read a local file
Hello everybody!
(hi dr. nick!)
I have been able to successfully parse a XML file from an internet URL using SAX parser. Now im looking to change my code to be able to parse a local XML file. Below is a part of the code I am using.
My problem could be due to syntax or it could be do to a lack of understanding on how the Android file system works.
// ------------------------------
FileInputStream fstream = null;
try {
fstream = new FileInputStream(filename);
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance()
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampl
xr.parse(new InputSource(fstream));
// more code......
}catch (Exception FileNotFoundException){
Log.v(logCat, "File not found exception!:" + filename);
}
// ------------------------------
A file not found exception is getting thrown so im thinking that the problematic code is this line:
fstream = new FileInputStream(filename);
// filename = "/res/xml/sample.xml"
Does that file path make sense to use for accessing a file local to this application? ie. / is where my project is stored, /src is where the source files are located, /res/layout is where the layout files are. I created the /res/xml directory
Thats a long post. Hopefully i made my problem clear.
Thanks, daniel
I am very much happy that I found this blog. I enjoyed reading every little bit of it.Some interesting and well researched information on cameras. I’ll put a link to this site on my blog. Thanks for sharing. Oil Mill Machinery
Thanks for the help!
I guess ive got to learn a little more about Java.
If you need a full example project source code of this issue, let's try this link http://www.codemobiles.com/for
Regards
You can't refer to the the files in the res folder as /res/xml/sample.xml, etc.
You need to use the function Resources.openRawResource(Int)
So something like this should work to get the InputStream:
InputStream is = context.getResources().openRaw
Goood luck! I'm a huge Floyd fan, so love the name!
it is sometimes world of hard to Pandora in a world of adults Pandora Bracelets
Ok. Made some changes. I placed a copy of the sample.xml file in the /res/raw and the /res/xml directories (not sure which one to use). I tried this line and am getting an error: "context cannot be resolved":
istream = context.getResources().openRaw
Eventually, I want this to be able to let the user choose which XML file they want to load (from all XML files in a particular directory). Also, they will be able to download new XML files which will be placed with the other XML files. Should I even be storing my XML files in /res/raw or /res/xml?
Thanks for the help. Im glad Android has forums. Its been a lot of fun learning the Android SDK.
I just found this bit of info. Files written to:
/data/data/your_project_packa
Can be loaded using openFileInput()
Link: http://www.anddev.org/working_
Half-way down the tutorial page; section 2: Reading the File
That context variable must be a Context object. So when you call your DefaultHandler from your activity you need to pass in a Context. If you look at this page on the NewsDroid rss tutorial you'll see that the updateArticles and createFeed functions take in a Context to use in those functions.
I have years of C,C++, Actionscript, Perl and Python expierence but I literally started learning Java a week ago so I could make Aps for Android.
So..... im a little confused on what your trying to tell me to do on that last post. Here is my code for parsing the XML feed (R.raw.tour i guess....), and then putting it into a list. Can you show me what I need to do here? Thanks a bunch man.
FileInputStream istream = null;
try {
istream = context.getResources().openRaw
SAXParserFactory spf = SAXParserFactory.newInstance()
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampl
Log.v(logCat, "Calling parse() in ReadTourFromLocal: "+filename);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(istream));
Log.v(logCat, "Returned from Parse: "+filename);
int numberOfSites = myExampleHandler.getNumberOfSi
Log.v(logCat, "Sites = "+numberOfSites);
/* Set the result to be displayed in our GUI. */
for(int i = 0; i
can you put the whole function in there for that code including the function declaration?
Thanks!
private void readFileFromLocal()
{
FileInputStream istream = null;
try {
istream = context.getResources().openRaw
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance()
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampl
Log.v(logCat, "Calling parse() in ReadTourFromLocal: "+filename);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(istream));
Log.v(logCat, "Returned from Parse: "+filename);
/* Parsing has finished. */
int xcount = myExampleHandler.getNumber();
} catch (Exception FileNotFoundException){
Log.v(logCat, "File not found exception!:" + filename);
}
}
Hi
I have obviously been searching around for a way to do this, but i can not get it to work for me. Could you please explain in more detail how everything in this method works?
What is context, examplehandler and so on...
private void readFileFromLocal( Context context )
{
FileInputStream istream = null;
try {
istream = context.getResources().openRaw
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance()
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampl
Log.v(logCat, "Calling parse() in ReadTourFromLocal: "+filename);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(istream));
Log.v(logCat, "Returned from Parse: "+filename);
/* Parsing has finished. */
int xcount = myExampleHandler.getNumber();
} catch (Exception FileNotFoundException){
Log.v(logCat, "File not found exception!:" + filename);
}
}
Then when you call this function from your Activity you can call it like this:
myobj.readFileFromLocal(this)
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@fraufletscher (Jessica Griebsch)I've just received an achievement: Affable http://t.co/ZobME8Cv #Android #Androidgames
2 min 12 sec ago -
@mark_pawlowski (mark pawlowski)#IPhone apps crash more than #Android apps. Who would have known?
http://t.co/8ln0iopQ2 min 19 sec ago -
@Labelmars (Label MARS PROJECT)MASAKI YODA on #Spotify Title--Voltage- http://t.co/zKs2TktH #music #musik #iPhone #Android 20-42-14
2 min 20 sec ago -
@180rad (paul radcliffe)I've just received an achievement: Lover of space https://t.co/f4Mcp9dW #Android #Androidgames
2 min 20 sec ago -
@Labelmars (Label MARS PROJECT)MASAKI YODA on #Spotify Title-FREEDOM- http://t.co/hbx947eL http://t.co/Nz84sxao #music #musik #iPhone #Android 20-41-54
2 min 39 sec ago
Poll
Useful resources
Android Development Projects
- Android App wanted immediately by JoePublic
- LIST DATA PROJ by nhammoud
- Nonpublic project #1433932 by subpariq
- Alarm Android Application Design by globalheed
- Simple Album App for Android by ayfonfan
- iOS and Android photo manipulation 'Morph App' by whatwedomedia
- Onsite Software Engineers in Germany by sudhirshree
- Augmented reality by merder99
- Nonpublic project #1433560 by vobla73
- Mobile app coder needed for quick, simple app by Ergometrix



as if she can't forgive sense of dismay. mui mui handbags Pocket the phone ring, hasn't attract her attention.