Patel Pinkal
Patel Pinkal

Reputation: 9522

get assets path in File to read multiple files

I have multiple .epub file in assets, I want that all .epub file in listFile. In my old code i use external storage file path, for getting .epub files, like this

File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Files" );

File listFile[] = myFile.listFiles();
if (listFile != null && listFile.length > 0) {
    for (int i = 0; i < listFile.length; i++) {

        if (listFile[i].getName().endsWith(".epub")) {

            myNewBookModels = new NewBookModels();
            Log.i(TAG, "getEpubFilesFromFileManager: " + ".." + listFile[i].getName());

            myNewBookModels.setBookName(listFile[i].getName().toString());
            myNewBookModels.setBookPath(listFile[i].getPath().toString());
            myNewBookModels.setBitmap(bookLoaded(listFile[i].getName().toString()));
            arrayList.add(myNewBookModels);
        }
    }
}

but i want that .epub files from assets folder. How can i do this?

I also want this field from that .epub file

1) Name

2) Path

3) Bitmap (cover page Image)

It is compulsory for me. This can only possible in File but i want assets .epub file in File

Upvotes: 0

Views: 2320

Answers (3)

Pramit Patel
Pramit Patel

Reputation: 59

You can access Asset folder using this path - "file:///android_asset/files"

OR

//Directory or File name
dirForm = "files";  
private void listFiles(String dirFrom) {
        Resources res = getResources(); //if you are in an activity
        AssetManager am = res.getAssets();
        String fileList[] = am.list(dirFrom);

            if (fileList != null)
            {   
                for ( int i = 0;i<fileList.length;i++)
                {
                     File file = new File(fileList[i]);
                     if ((file.getName()).endsWith(".epub")) {

                      Log.i(TAG, "getEpubFilesFromFileManager: " + ".." + file.getName() +"Path"+ file.getPath());
                     }
                }
            }
    }

Upvotes: 1

GreenROBO
GreenROBO

Reputation: 4920

You can Use AssetManager class & then you can Read files from Assets Folder.

Upvotes: 0

Charu
Charu

Reputation: 1075

If your files are in asset folder then it will always be a fixed path. so you can give a hard coded path in string.

Upvotes: 0

Related Questions