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

浪尽此生 提交于 2020-02-08 09:58:09

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/18676235/access-external-files-outside-of-avd-device-using-android-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!