Android File System : How to Read a local file

15 replies [Last post]
cheap ugg boots
User offline. Last seen 2 years 11 weeks ago. Offline
Joined: 11/19/2009

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(myExampleHandler);
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

1xuntong
User offline. Last seen 5 weeks 6 days ago. Offline
Joined: 12/25/2011
miu miu

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

smallyfish
User offline. Last seen 32 weeks 6 days ago. Offline
Joined: 03/18/2011
I am very much happy that I

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

geojoil
User offline. Last seen 21 weeks 1 day ago. Offline
Joined: 05/21/2010
Thanks for the help! I guess

Thanks for the help!
I guess ive got to learn a little more about Java.

verified torrents

jaboho
User offline. Last seen 2 years 17 weeks ago. Offline
Joined: 10/11/2009
full example project source code

If you need a full example project source code of this issue, let's try this link http://www.codemobiles.com/forum/code-mobile-topic-69.html
Regards

hobbs
User offline. Last seen 12 weeks 1 day ago. Offline
Joined: 11/12/2007
use function resources

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().openRawResource(R.xml.sample);

Goood luck! I'm a huge Floyd fan, so love the name!

pandorauk
User offline. Last seen 1 year 8 weeks ago. Offline
Joined: 12/07/2010
it is sometimes world of hard

it is sometimes world of hard to Pandora in a world of adults Pandora Bracelets

cheap ugg boots
User offline. Last seen 2 years 11 weeks ago. Offline
Joined: 11/19/2009
Im a fan too

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().openRawResource(R.raw.sample);

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.

cheap ugg boots
User offline. Last seen 2 years 11 weeks ago. Offline
Joined: 11/19/2009
more info

I just found this bit of info. Files written to:
/data/data/your_project_package_structure/files/

Can be loaded using openFileInput()

Link: http://www.anddev.org/working_with_files-t115.html
Half-way down the tutorial page; section 2: Reading the File

hobbs
User offline. Last seen 12 weeks 1 day ago. Offline
Joined: 11/12/2007
context variable

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.

cheap ugg boots
User offline. Last seen 2 years 11 weeks ago. Offline
Joined: 11/19/2009
XML parsing code

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().openRawResource(R.raw.tour);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);

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.getNumberOfSites();
Log.v(logCat, "Sites = "+numberOfSites);
/* Set the result to be displayed in our GUI. */
for(int i = 0; i

hobbs
User offline. Last seen 12 weeks 1 day ago. Offline
Joined: 11/12/2007
function declaration

can you put the whole function in there for that code including the function declaration?

Thanks!

cheap ugg boots
User offline. Last seen 2 years 11 weeks ago. Offline
Joined: 11/19/2009
more code

private void readFileFromLocal()
{
FileInputStream istream = null;
try {
istream = context.getResources().openRawResource(R.raw.tour);

/* 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(myExampleHandler);
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);
}
}

hoppestokk
User offline. Last seen 1 year 37 weeks ago. Offline
Joined: 05/21/2010
Please explain more

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...

hobbs
User offline. Last seen 12 weeks 1 day ago. Offline
Joined: 11/12/2007
Try this (only changed the function declaration):

private void readFileFromLocal( Context context )
{
FileInputStream istream = null;
try {
istream = context.getResources().openRawResource(R.raw.tour);

/* 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(myExampleHandler);
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);

cheap ugg boots
User offline. Last seen 2 years 11 weeks ago. Offline
Joined: 11/19/2009
Thanks for the help!

That worked perfectly Hobbs. Thanks for the help!
I guess ive got to learn a little more about Java.

Again, thanks!