Android get a list of files from a package

How can I get the entire list of .json configuration files that are NOT on the SD card, but in my package? The image below shows the files in the folder that I want to access. The idea is this: when I start the application, I want to get a list of configuration files, the path to them and show that the testers need to choose which server to connect to. How can i do this? I searched for PackageManager and AssetManager , but failed.

When I put folders in folder files, this game code gives me a list of available configurations, but how can I get the full path to them to read them?

 AssetManager am = getAssets(); String s = getPackageName(); Resources tmp = pm.getResourcesForApplication(s); String [] arr = am.list("configs2"); 

snapshot

+4
source share
2 answers

I finally solved my problem. This is the structure of my project: enter image description here

  • To get the list of files from the application package, you need to put all of your files that you want to receive in the resource 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; } 
    1. To read a specific file from a 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; 

}

+1
source

To do a similar thing, I used the following command:

InputStream is = context.getResources().openRawResource(R.raw.raw_data_file);

If the raw_data_file file was in the path res/raw/raw_data_file.txt . Then you can read the file using InputStream, as usual. I am sure that you can do something similar with your file, where it is, but as a rule, I usually put any resources in the res(ources) folder

+1
source

All Articles