How to read an xml file from R.java - android

I have an xml file inside the res folder under the xml forlder. I want to parse it with SAX. I know there are lots of example but I want to understand InputStream, InputSource, File deeply. I can reach the file writing R.xml.hippo (hippo is the name of the xml file).
But how can I give this resource as a stream?
I know when I write R.xml.hippo to anywhere it will be reference(int) but I want give it as a source. What is and how converting an int to starting point of stream?

You can read XML from assets folder as well as from res/raw folder.
To read from assets folder
AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");
To read from raw folder
InputStream inputStream = getResources().openRawResource(R.raw.your_xml);
And parse this inputStream to your XML Parser.

You should place the file inside the raw folder. Then you can access it via Resources.openRawResource(). For more information refer to Android Developer's page.

Related

(Android) Read matlab file from assets

I use the jmatIO library to read a .mat file. In plain java I can set the path to the matFileReader like this
MatFileReader mfr = new MatFileReader("/theta-phi_small_param5.mat");
and I can have access to all the .mat data. Inside the android i put the .mat file to the assets folder and I tried to access it like this
mfr = new MatFileReader("file:///assets/theta-phi.mat");
but it doesn't work. How can I get the path to the mat file inside the assets folder so to read it with the MatFileReader?
Does the MatFileReader accept an InputStream? If so you can do it like this:
InputStream in = getAssets().open("theta-phi.mat");
It might also work to use:
File file = new File("file:///android_asset/theta-phi.mat");
UPDATE: Since MatFileReader doesn't support InputStream and the File solution above doesn't work I guess your best bet is to copy the file from the Assets folder to your apps External/Internal storage and from there access the file.

XmlPullParser setInput issue

I have problem with setInput() method. When I've provided plain StringReader and parse XML
locationsXml.setInput(new StringReader("<locations>" + "<location>" etc.[...]
Everything gone well, but if I want to load file from resources:
locationsXml.setInput(new FileReader("res/xml/locations.xml"));
File is not being loaded.
Where is the problem?
You have create a Inputstream from the file in your resources (provided the resource xml is placed in 'assets' folder). You can get control of that resource using :
context.getAssets.open(name of the file);
eg - InputStream myInput = myContext.getAssets().open(locations.xml);
Noww - you can use this inputStream to do anything you want.
Or
Put the xml into /res/raw folder. It looks like openRawResource opens resources from that folder only. You can also try getResources().getXml(com.MYCLass.R.xml.programs); which will return you instance of XML parser.
InputStream object = this.getResources()
.openRawResource(R.raw.fileName);

Access PDF file in assets folder on android

I have seen many question about accessing files in assets folder but can't seem to get a solid answer. I'm working on a application that would extract a text from PDF file thus I'm using iText Library to do that but my problem here is the file pathing of PDF.
I have tried using assetManager and assetManager opens that file but i think iText needs the file path so it can open the file by itself, i am not sure but that is my theory.
PdfReader reader = new PdfReader(<String PDF-file>);
then How do I access the file under assets folder using iText? if it is not possible is there a way to do that?
Here is sample code to get asset directory for file and use it for pdfreader.
code:
File f = FileUtils.fileFromAsset(this, "filename.pdf");
PdfReader pdfReader = new PdfReader(f.getPath());
You can get an InputStream object like this
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
and then use this constructor
public PdfReader(InputStream is)
throws IOException
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#PdfReader(java.io.InputStream)
I have solved my problem my using this:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));

Android Including XML files = where to put them

I am developing an app that needs to refer to data in a number of XML files. There are a lot ( ~40) XML files required by the app.
The file have names like "AbnormalFlags-v1.0.xml" and I current have them located in
res/raw/v2/AbnormalFlags-v1.0.xml
res/raw/v2/ActualDeliveryPlace-v1.0.xml
res/raw/v2/AddressType-v1.0.xml ... ...
A number of questions
Is this the best place to locate these files ?
The filenames seem to be a problem, will that cause issues
Because I have to dynamically determine which file to use I use the:
xmlFile = getApplicationContext().getResources().getIdentifier(xmlFileId,"raw","com.apps4health.refpack");
This does not find the resource file, where am I going wrong ?
Given the format of the file names I would suggest you to save them in the assets folder. Create a folder named assets inside the res folder and put there the XML files.
The code below shows how to open files in the assets directory:
InputStream is = null;
try {
AssetManager am = getApplicationContext().getAssets();
is = am.open("AbnormalFlags-v1.0.xml");
// Process the XML file from "is"...
}catch( IOException e ) {
// Handle exception
}
AssetManager is responsible to open files saved in the res/assets folder. You just call the open() method passing the file name as argument.

Getting File from /res/raw

I need your help, may be this question is very easy for you.
I have a file in /res/raw folder. For example, it's id is R.raw.myFile. I need to get "File" object. Such as i can do with file on sdcard
File file = new File("/sdcard/myFolder/myFile");
How I can do this?
By the way, sorry for my english if there are some mistakes.
I need "File" object. Not stream.
Use this
InputStream inputStream = getResources().openRawResource(R.raw.filename);

Categories

Resources