Access external files (Outside of AVD device) using Android JUnit - java

I am using Robotium to automate Android application. Created a android test project for the same. Inside project folder I want to keep some files like .properties/.xls etc. and want to read from /write to those files. For example, I have one config.properties file under my Android test project directory (src/main/java/config) and I want to access that file through coding:
For normal Java project I used following code snippet to load config.properties file:
CONFIG = new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/config/config.properties");
CONFIG.load(fs);
This same code - when executed as Android JUnit project throwing error saying java.io.FileNotFoundException: //src/main/java/config/config.properties: open failed: ENOENT (No such file or directory) and unable to locate the file.
If anyone faced this anytime before please help me to get through.
Thanks
Sitam Jana
Software QA Enginner
Mindfire Solutions

You cannot access the internal structure of your java project that way. When you install your apk on the device, your java code gets converted to dex code for the Dalvik Virtual Machine, and you won't have the file structure from your project.
You can try the following:
If you only want to read the config.properties you should use the http://developer.android.com/reference/android/content/res/AssetManager.html class, and put your config.properties in /assets/ in your project.
If you also want to write your .properties file, consider creating a temporary file like this: Creating temporary files in Android.
Best Regards.

Related

How to read a file in android studio project folder?

I am new to android and it seems I cannot get a filepath of a file within the project structure.
I have a text file "db-info.txt", that I am trying to read from a class. Every method I tried has said no file found. This file I am trying to read from a java class within the same folder.
Any help is appreciated. Here is where my file is within the project -

File not found on Android Device

I have an error here:
FileHandle file = Gdx.files.internal("hscore.json");
Showing that it can't be found:
...
com.badlogic.gdx.utils.GdxRuntimeException: File not found: hscore.json (Local)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
...
It works on my Desktop, but fails like this on my Android Phone.
I use Android Studio, where I keep the file in the assets folder which is in the android folder.
I have no idea why it can't find it on the android?
Update: I don't know what happened in meantime, but now it highlights this line of code after throwing the same error:
String s = file.readString();
It can read .txt files and other files I'm using just fine, seems the .json file is the problem for android?
Until this is resolved, I'm using Preferences to save data there, rather than the whole class in .json
Thanks to a chat with p.streef This question has come to a closure, sorta.
1) I created a new test.txt file and it worked fine reading that.
Maybe then the .json file itself was a real issue?
2) But then I renamed it to text.json, and afterwards back to hscore.json
It worked fine in both cases when opening and reading the file.
I had no idea why the initial file .json was not readable?
3) I also killed and restarted my android studio in meantime because it froze at a point when building my project.
After all of this the problem of file not being found was resolved by itself.
Anyway then I realized that the file is not writable since internal files are read-only.
I decided to stick then with the Preferences as the final solution.

Not able to read file in jenkins using java code

I am able to read a file using java,File is in eclipse workspace.
My file location is:src\test\resources
So,I have given like this.
String filePath = "\src\test\resources\ARImport_Copy3.csv";
This is working fine but when i am running in jenkins ,I am getting not able to load file messages.
Please provide me the solution.
Reading file directly from filesystem is not flexible. You can't control what working directory would be when executed by different tools in different environments.
It is more reliable to read a resource file from classpath like this:
URL url = this.getClass().getResource("/ARImport_Copy3.csv");
File testFile = new File(url.getFile());
A broader discussion you will find here: Easy way to get a test file into JUnit

Issue file assets Libgdx

I have a problem to load a file from assets.
In pratcise I have to load TiledMap file and I do it in this way:
arrayTiledMap.add(new TmxMapLoader().load(Gdx.files.internal("scenario.tmx").path())));
(I add it in array for other reason)
In the project the tmx file(scenario.tmx) is located in the android assets folder.
When I execute the program in Eclipse there aren't problems , but when I create the JAR file for Desktop project and I execute it I get this error on console(I launch it by terminal):
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: Documents/University/Programming/Street.png (Internal)
The file Street.png is a file that I used in tiledMap editor for create the map.
At this point I check the JAR file contents and In the root directory of JAR there is the Documents folder and in it there is University folder and so on.
Because if the path is in the JAR file I get this error?
What is that wrong?
Thank you very much for your time, this error is driving me crazy
If you need other code, in particular, tell me.
(although I do not think since the problem only occurs when loading the file)
Because if the path is in the JAR file I get this error?
No. Its most likely you aren't exporting the JAR correctly.
File -> Export -> Java -> Runnable JAR File
And be sure to check this:
package required libraries into generated JAR.

What is the proper way to use ResourceBundle.getBundle() in an Android application?

I want to know how I can use ResourceBundle.getBundle() in an Android application given that I use it in my Java applications. For example:
I have a properties file, "MyOrg.properties", which I've included in a JAR file named "MyOrg.jar". The path information in the JAR file associated with "MyOrg.jar" is "myorg\" (this is on a Windows system). I build my Java application using "myorg.jar" and note that the following line of code works as expected, which is that it loads the file "MyOrg.properties" from "MyOrg.jar" as a java.util.ResourceBundle:
ResourceBundle resources = ResourceBundle.getBundle( "myorg.MyOrg" );
Next, I place a modified copy of the file "MyOrg.properties" on the file system in the directory "c:\myorg", which is on my Java application's class path. I now rerun my Java application and note that the Resource.getBundle() returns, as expected, a bundle for the modified copy of "MyOrg.properties" (the one that is on the file system).
The behavior I've just described is what I would like to be able to accomplish with an Android application. However, ResourceBundle.getBundle() throws a MissingResourceException; it fails to find "MyOrg.properties" in either the JAR file or as a stand-alone file.
So I have two questions:
1) - Is it possible for an Android application to retrieve a resource from a JAR file using ResourceBundle.getBundle() (or any other way for that matter)?
2) - Is it possible for an Android application to retrieve a resource from a file using ResourceBundle.getBundle()?
Regarding 2): I'm using the nbandroid plugin with NetBeans 6.7.1 and I've placed copies of "MyOrg.properties" on the file system as follows prior to building my Android application:
MyProject
-- build
-- classes
myorg (directory contains "MyOrg.properties")
...
src
myorg (directory contains "MyOrg.properties")
you need to make sure the properties file makes it to the .apk file. your best bet is probably in res/raw/ or assets/. see also PropertyResourceBundle's constructors (since it's easy to get hold of an InputStream).

Categories