0

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

Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50

3 Answers3

1

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());
                     }
                }
            }
    }
0

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.

Charu
  • 1,055
  • 13
  • 18
  • can you please explain in details. – Patel Pinkal Jan 03 '17 at 07:15
  • I meant that when your files were in external storage then they may have variable path. In that case you need to get the path at runtime. But now when your files are in android's asset folder then it will always be fixed in all devices. So you don't need to get path at run time. You can just give a hard coded path. & a typical Android Studio project, you will have an app/ module, with a main/ sourceset (app/src/main/ off of the project root), and so your primary assets would go in app/src/main/assets/. and for names: String[] listOfFileName = getContext().getAssets().list("your/path"); – Charu Jan 03 '17 at 07:36
  • String[] listOfFileName = getContext().getAssets().list("your/path/inside/asset"); – Charu Jan 03 '17 at 07:47
  • What else you need? path and name is there and bitmap: you are already getting from name – Charu Jan 03 '17 at 12:33
0

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

Community
  • 1
  • 1
GreenROBO
  • 4,725
  • 4
  • 23
  • 43