0

I need to put a folder in my Android project that I can access using File myDir = new File(path-to-dir); but I can't seem to find a way to do this. I have tried putting it in the assets folder but I need to access my folder itself, so I can perform operations on it once I have it in a File object. Any help would be appreciated.

Michael Morrow
  • 403
  • 2
  • 16

1 Answers1

0

Step 1 : Create a folder ins assets folder and place your files there..

Step 2 : assuming you have a folder named myfolder in assets folder, try this code

ReadFromfile("/myfolder/myfile.txt", mContext);

and the function is

public String ReadFromfile(String fileName, Context context) {
    StringBuilder returnString = new StringBuilder();
    InputStream fIn = null;
    InputStreamReader isr = null;
    BufferedReader input = null;
    try {
        fIn = context.getResources().getAssets()
                .open(fileName, Context.MODE_WORLD_READABLE);
        isr = new InputStreamReader(fIn);
        input = new BufferedReader(isr);
        String line = "";
        while ((line = input.readLine()) != null) {
            returnString.append(line);
        }
    } catch (Exception e) {
        e.getMessage();
    } finally {
        try {
            if (isr != null)
                isr.close();
            if (fIn != null)
                fIn.close();
            if (input != null)
                input.close();
        } catch (Exception e2) {
            e2.getMessage();
        }
    }
    return returnString.toString();
}

Credits to SO Question

Community
  • 1
  • 1
Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77