Issue
How can I get all list of .json
config files, which are located NOT on SD card, but in my package? Bellow on the image showed the files in the folder which I want to access. The idea is the following: when application starts I want to get list of config files, the path to them and display that to testers to choose to what server connect to. How can I do that? I was looking for PackageManager
and AssetManager
, but resultless.
When I put folder configs to folder assets, this code game me a list of available configs, but how can I get full path to them to read them?
AssetManager am = getAssets();
String s = getPackageName();
Resources tmp = pm.getResourcesForApplication(s);
String [] arr = am.list("configs2");
Solution
I finally solved my problem. This is the structure of my project:
To get list of files from application package, you need to put all your files you want to get into assets folder. Here is the code:
private ArrayList getPackageConfigList() { AssetManager am = getAssets(); String [] arr = null; try { arr = am.list(folder); } catch(IOException e) {e.printStackTrace();}
ArrayList<String> flist = new ArrayList<String>(); for (int i=0; i<arr.length; i++) { flist.add(PKG + arr[i]); Log.d(tag, PKG+ arr[i]); } return flist; }
- To read concrete file from package:
private String loadConfigFromPackage(String fileName) { AssetManager am = getAssets(); InputStream in = null; String result = null;
try { //open file, read to buffer, convert to string in = am.open(folder + "/" + fileName); int size = in.available(); byte[] buffer = new byte[size]; in.read(buffer); in.close(); result = new String(buffer); } catch(IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch(Exception ex){} } return result;
}
Answered By - yozhik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.