Android Instrumentation Test get resource file handle - android

To test one of my implementations I need a valid and correctly initialised File-Object that points to an image file.
I've tried the following:
placed an image into the androidTest/ressources-Folder named cat.jpeg
tried to create a file handle the following way: new File(InstrumentationRegistry.getContext().getClassLoader().getResource(“cat.jpg”).getPath())
when calling exists() on the initialized file it always returns false.
How do I have to setup the test / initialize the file to make it work?

One option is to put all the test resources in androidTest/assets. By fetching the AssetManager from the test package's context, you can get the assets as a list of filenames (AssetManager.list(String path)). For each asset, open an InputStream with AssetManager.open(String filename) and create a temporary file that you write the asset to.

Related

Distribute an xml file with my Android app

I have created a java application using "Simple" that allows me to serialize objects in some configuration xml file.
These files must be redistributed with my android application, but here comes the problem:
Where do I place the files? Is it better to place them in the res/xml folder or create a custom path to the assets folder (assets/myfiles)?
In the examples given with Simple if I want to deserialize an object I need an xml file:
File fl = new File ("string to file path");
but if the file is in the resources of my project, how do I get the file?
Searching the internet I found only examples that use a XmlResourceParser but I don't know how to use it with Simple!
If you are going to include files you can later access from your application you'll need to put them in the assets folder. After your application has been installed and running you can get get that xml file by using the AssetManager class:
AssetManager assets = getAssets();
InputStream stream = asset.open("filename");
That will give you an InputStream for you to do whatever you need.

How do I open a text file that's in my app's package?

Using Eclipse, Android SDK.
I have a text file full of data that I need pulled in. (For now, it's the easier the way, eventually I'm going to need to scrape dynamic data from a URL, but for now I have the test data I need in this text file).
I've created a class to open this file, but no matter how I try to open it I keep geeting "file not found" exceptions.
I've tried putting my "data.txt" file in various relative paths (within my App):
- "/AppName/"
- "/AppName/src/com/example/appname/data.txt"
I've tried passing different relative paths. I've tried putting the text file in the same path of the .java class file that's trying to open it, and it still can't find it! What am I doing wrong?
What am I doing wrong?
You have two main options of where to store this file within your project directory: assets/ and res/raw/.
If you use assets/, you can call getAssets() on your Activity (or other Context), and on there call open() with the relative path within assets/ to get an InputStream on this file (e.g., assets/data.txt would be accessed via getAssets().open("data.txt")).
If you use res/raw/, you can call getResources() on your Activity (or other Context), and on there call openRawResource(), passing in the R.raw value based upon your filename (e.g., res/raw/data.txt would be accessed via getResources().openRawResource(R.raw.data)).
Use /res/raw directory. I read sound files from raw:
InputStream soundFile1 = context.getResources().openRawResource(soundOneId);
Where context is a base context of activity passed to the class.
http://developer.android.com/reference/android/content/Context.html

Access to resource folder route

I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).
I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?
new FileOutputStream("/res/drawable/xxx.png");
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream form a file in resources,you should consider creating a raw folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.

Can anyone explain the File() parameters used to download file in android?

In Reference to this android file download problem
Can anyone explain what does this line mean in the code
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
And what does it mean by the parameter root within the File().
Do I need to specify the root path to save the file?
If it is the case then how do we specify the root path in android ?
Regards
And what does it mean by the parameter root within the File(). Do I need to specify the root path to save the file? if it is the case then how do we specify the root path in android?
The code snippet from the question you linked doesn't define the variable, but if the code is downloading a file to the device, I would assume that it's a path on the SD card. Environment.getExternalStorageDirectory() will give you the root path to the SD card. You'll also need to specify the WRITE_EXTERNAL_STORAGE permission in your manifest.
If you're working on the emulator, you can create a virtual SD card when you create the emulator image.
The java.io.File(File, String) or java.io.File(String, String) are standard java constructors for Java. The first argument is just the parent directory path, while the second is the actual file name. If the file is in the current working directory or you know the full path as one string you can avoid the 2 argument constructors.
Since you are trying to download a file you can just acquire the file through a normal URL.openStream() to get an InputStream to get the contents of your downloaded file. For writing the data out you will follow the example you linked to to write the contents.
I'm unsure what the root variable was pointed to in the example. I'm not able to help you beyond this though since I have only gone through the first Hello, Android example myself.

Load file from resource

To sum up: in order to add easily unit tests for a SAX parser I would like to load XML from a file.
Now, I have my XML in a static string inside my unit test class, but it is not very convenient for large XML.
This is why I would like to add some XML files to my project and load them in my unit test. How can I do this?
This question is tagged as "Android" and I noticed that you mentioned an Activity, so I'm going to assume that you're trying to load an XML file within an Android application. If that is the case, put your XML file under /assets and call:
InputStream is = getAssets().open("input.xml")
from your Activity. From there, you can manipulate it into SAXBuilder. This will only work if you've set up your test to run on the emulator (or if you're just trying to debug outside of a unit test).
SAXBuilder has a constructor to read data from file:
Document build(java.io.File file)
This builds a document from the supplied filename.
http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html

Categories

Resources