Android Including XML files = where to put them - android

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.

Related

what is OpenNLP's model file path?

I try to use OpenNLP in an Android application with eclipse, and I imported the 4 JARs into the libs folder.
It's probably a stupid question.. but where should I put the model files "en-pos-maxent.bin"? I can't find anything regarding the path anywhere
I try to run the code contains this line:
POSModel model = new POSModelLoader()
.load(new File("en-pos-maxent.bin"));
I tried putting the en-pos-maxent.bin inside a new folder within the project("tagger/en-pos-maxent.bin"), inside the libs folder, or simply give the path where the en-pos-maxent.bin is put when downloaded("Users/ariel/Downloads/en-pos-maxent.bin"), it always give me the error: POS Tagger model file does not exist path: (the path I typed in)
Could anybody help please?
When you use new File("filename") the file is expected to be in the current working directory, I don't think that's different on Android. You can use System.getProperty("user.dir") to get the current working directory. It's by default the directory you start the program from. You could also specify a full path like new File("/full/path/filename") instead.
In android the right way to add file to the application (inside APK) is to put into the assets folder. Then you can't get File object from assets folder but, you can create that File using a buffer or in you case just get it into the InputStream. For example if you will create models folder into the assets folder (assets/models), your code will look like this:
AssetManager am = getAssets();
InputStream inputStream = am.open("models/en-pos-maxent.bin");
POSModel posModel = new POSModel(inputStream);

I get file not found exception trying to open a linked file in my assets folder (android)

I have a file in my assests folder, that I imported using the 'link' option (as opposed to copy). I use the following code to open it, but I get a file not found exception.
super.onCreate(savedInstanceState);
setContentView(R.layout.bar_page_lo);
appContext = getApplicationContext();
InputStream ins;
ins = appContext.getResources().getAssets().open("bar-data.json");
I need it to be linked because the file is in a dropbox folder so that my colleague can edit it. is there something special I need to do to use a linked file?
try:
appContext.getResources().openRawResource(R.raw.bar-data);
File must be placed in raw folder.

Get preloaded file in android

I'm new to android development and I am working on a little project. What I am having some issue with is getting access to preloaded files.
In my app, I have an XML file that I preloaded (I just simply put it in my src folder in a package). How do I access them in my classes? I need to get a File object pointing to this file so that I can use it as I would I/O files. It seems like this should be trivial, but alas I am stuck.
Lets say the file is located under: com.app.preloadedFiles/file1.XML
I've tried something along the lines of this, but have had no success:
URL dir_url = ClassLoader.getSystemResource("preloadedFiles/file1.XML");
FIle file = new File(dir_url.toURI());
I solved this in my app by getting an InputStream to the file -- something like:
myContext.getAssets().open(fileName);
//read the data and store it in a variable
Then, if you truly need to do File related opterations with it, you can write it to a private (or public) directory and do your operations from you newly written file. Something like:
File storageDir = myContext.getDir(directoryName, Context.MODE_PRIVATE);
File myFile = new File(storageDir + File.separator + fileName);
//then, write the data to the file and manipulate it -- store the name for access via File later

It is possible to search for a file with a desired extension in assets folder?

I have an app with randomly generated assets folder. In that assets folder, each time my app opens, i must open a file with ".jet" extension. The problem is that the file will have a different random name each time the app opens.
So. How can i search for the first file of assets folder with ".jet" extension?
I can't find any info about that in google
Thanks
The only way is to search for the extensions:
AssetManager am = getAssets();
String fileList[] = am.list("");
for (String fileName : fileList) {
if (fileName.endsWith(".jet") {
}
}
let me know if below code works for you?
Arrays.asList(getResources().getAssets().list("")).contains(".jet");
it will return a array with filenames containing .jet , or you can use endsWith on file names too

File not found issue in Android

I need to access myfile.txt file using FileReader in Android , please suggest me where to add the text file in Eclipse. I tried it adding it in Resource and Asset but I am getting File not found issue.
FileReader fr = new FileReader("myfile.txt");
Even
File ff = new File("myfile.txt");
File Supports only the below listed parameters
FileReader Supports only the below listed parameters
Note: I want solution for this issue , only with FileReader or File
The directory would be /res/raw/ this is where you put all your extra resources.
you can refer to it using getResources().openRawResource(resourceName)
and check here Android how to get access to raw resources that i put in res folder?
EDIT:
how can i edit the text files in assets folder in android
in short
the easiest way would be to copy the file to external directory then do your stuff there
link is here
Android: How to create a directory on the SD Card and copy files from /res/raw to it?
One thing to mention - prior to 2.3 the file size in the assets cannot exceed 1MB.
hope it helps abit
That's how I obtain my file from the SD card, perhaps this can be some use to you.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File options = new File(getAppDirectory(), "portal.xml");
}
The getAppDirectory method used in the bit of code looks like this :
private String getAppDirectory() {
return new String(Environment.getExternalStorageDirectory().toString()
+ "/foldername/foldername/");
}
After this bit of code I also make sure the file exists and what not before I attempt to read from it.

Categories

Resources