Loading a file from res folder in android test project - android

In a testcase I need to load a picture which is available in my testproject in the folder
/myproject/res/drawable-hdpi/attachment.png
I tried it already by using the InstrumentationTestCase and storing the file in the /res/raw folder and loading it with this, but the R file does not contain my attachment.png
Context testContext = getInstrumentation().getContext();
InputStream input = testContext.getResources().openRawResource(R.raw.?????????)
any idea?
thanks

You should not use raw to save images, used drawable o asset ;)
Difference between /res and /assets directories
Android images in /assets or res/raw

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

How to list folders in raw resources

I have a huge directory of infrared remote control codes, and I'd like to parse through each of them when needed. However, I am having issues trying to access the folders I have in my application's res/raw/ folder. I'd like to be able to get the folder names, because that would essentially sort for me and list out all the brands that I'd like to list in a Spinner, and then pull files from inside that folder later.
What's wrong with me doing something like this?
String path = "android.resource://" + getPackageName() + "/res/raw/";
File f = new File(path);
File[] files = f.listFiles();
for(File inFile : files){
if(inFile.isDirectory()){
Log.d(TAG, inFile.getName());
}
}
Just found out that I can't create subdirectories in my res/raw folder, and I'd have to go about this through the assets folder. Would this be as simple as this structure?
assets
- remotes
- Samsung
- remote1.txt
- remote2.txt
- Sony
- remote1.txt
- etc
However, I am having issues trying to access the folders I have in my application's res/raw/ folder.
You cannot have folders inside of res/raw/. A resource directory can contain files, not subdirectories.
If you want to package a directory tree with your app, use assets/ and AssetManager, not raw resources.

What is the path directory to res/drawable type String to pass through File(String path) in Android?

I am trying to get String path directory to images in the res/drawable and filter through it (jpg, png ..)
Something like this
File folder = new File("/res/drawable");
images = folder.listFiles(IMAGE_FILTER);
File image = images[randGen.nextInt(images.length)];
From sd card works easily but couldn't make it from the resource folder! I don't know if this is naive question but I have been trying different options like passing through uri, casting bitmap to File, no luck!
I am trying to get String path directory to images in the res/drawable
Resources are not files on the device. They are entries in the APK's ZIP file. Hence, you cannot construct a File that points to a resource.
You can use AssetManager to list assets, open InputStreams on assets, etc.

Open an image from asset folder in android

I need to get the images from asset folder by specifying the name of the image and set it to Drawable.
drw = Drawable.createFromStream(getAssets().open("/assets/images/"+drawables[i]),null);
But I get File not found exception.(images is subfolder of assets and drawables[i]- name of the image(say "ball.jpg").
Write instead:
drw = Drawable.createFromStream(getAssets().open("images/"+drawables[i]),null);
file path should be:
"images/"+drawables[i]
when you write getAssets() means open assets folder and find out path there.

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

Categories

Resources