(Android) Read matlab file from assets - android

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.

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

Referencing files in Android

Why can't I refer to a PDF file in my Android device?
I have a folder named "templates" in the root folder, and I have a PDF file in that. I've tried to refer to the file via the following:
File file = new File("/templates/myPDFFile.pdf");
but when I try to print file.exists(), it always returns me false.
Just as further information, my file hierarchy is:
root
-src
-com.example.text
-MyActivity.java
-templates
-MyPDFFile.pdf
and my code is stored in MyActivity.java.
How do I reference accurately to the PDF file?
You must put it in assets and get it from there like :
How to open a pdf stored either in res/raw or assets 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

How to open and read a file in the src/my/package folder?

I would like to load the contents of a text file in a String.
The text file should stay in the same folder src/my/package as the .java.
However, I can't find the path to this file:
I have tried:
File f = new File("src/my/package/file.js");
File f = new File("file.js");
and many others but nothing worked.
What is the correct path to my file?
To open files located on the classpath, use the Class.getResource() family of methods. They work even if the file is inside a jar file with your classes. So something like
InputStream is = getClass().getResourceAsStream("/my/package/file.js");

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