1

I drop a folder called profiles in the same directory where my java files stored.

enter image description here

I tried to find the folder but get a not-found error.

String dir = getApplicationInfo().dataDir;
Log.d("dir", dir);
File folder = new File("/profiles"); // also tried File folder = new File(dir+"/profiles");
if (!folder.exists()) {
    Log.d("Not Found Dir", "Not Found Dir  ");
} else {
    Log.d("Found Dir", "Found Dir  " );
}

Print

D/dir: /data/user/0/com.pakhocheung.o
D/Not Found Dir: Not Found Dir

Then I tried to list all the files in that directory

String path = dir;
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++) {
    Log.d("Files", "FileName:" + files[i].getName());
}

Print

D/Files: Path: /data/user/0/com.pakhocheung.o
D/Files: Size: 6
D/Files: FileName:cache
D/Files: FileName:code_cache
D/Files: FileName:lib
D/Files: FileName:shared_prefs
D/Files: FileName:app_Paas
D/Files: FileName:files

It seems I am in the wrong directory because I can't see those files. Any suggestions?

Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52

1 Answers1

2

Create assests folder inside main folder. Put your profiles folder in assets folder. To read file names inside profiles folder use this code.

String[] list = null;
    try {
        list = getAssets().list("profiles");
        for (String file: list){
            Log.d(TAG, "file name "+ file.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

To read file from this folder use this.

InputStream is = getAssets().open("profiles/example.txt");
Bek
  • 7,790
  • 4
  • 18
  • 31
  • If I can see all the files using ```getAssets().list("profiles")```, why ```File folder = new File("file:///android_asset/profiles/");``` shows not exist? Thanks for the help – Pak Ho Cheung Dec 07 '17 at 04:25
  • You can use this path only for `url` `file:///android_asset/profiles/"`. for example you can use this in `WebView`. – Bek Dec 07 '17 at 04:37
  • What do you have in `profiles`? What do you want to do with them. – Bek Dec 07 '17 at 04:40
  • Oh I see. I need to call the path to load some data. https://stackoverflow.com/a/22934175/5737856 – Pak Ho Cheung Dec 07 '17 at 04:46
  • Argument should be ```File``` or ```String``` (the path). I also tried to convert the ```InputStream``` to ```File``` using this function. https://stackoverflow.com/a/11820891/5737856. But get a ```java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File[] java.io.File.listFiles()' on a null object reference error```. Anyways, thanks for the help. I need to take a rest. If I figure out later, I will vote your answer .Thanks. – Pak Ho Cheung Dec 07 '17 at 05:15
  • You can't directly create file from assets folder. Read this may help you https://stackoverflow.com/a/10402757/8942811 and https://stackoverflow.com/a/15575813/8942811 – Bek Dec 07 '17 at 05:21
  • Damn. Finally done with it using other method. Thanks – Pak Ho Cheung Dec 07 '17 at 05:47