简体   繁体   中英

Android: Empty assets folder containing files

I have been searching around this website for quite a while now trying to find an answer to this, but I just can't get it.

What I am trying to get is a list of files in the assets folder. This is because I want to have a smart way of loading those files and using them in different screens without having to write a serparate activity for every single file in the folder. This is the code I'm currently using.

File file = new File("file:///android_asset/Peacefulpack.txt");
Log.e("Test", file.getName());

File folder = new File("file:///android_asset");
for(int i = 0; i < folder.listFiles().length; i++)
{
    Log.e("Test", folder.listFiles()[i].getName());
}

The top part of the code with file works just fine, but the one with folder doesn't. The strange thing is that folder.listFiles() returns null, but it should only do so when there are no files or subfolders in the directory (at least as far as I know). However, there are files in this folder since I'm accesing one in there.

Why is this giving me a null pointer?

try this:

String folder = "/";
AssetManager am = getResources().getAssets();
String fileList[] = am.list(folder);
for(int i = 0; i < fileList.length; i++)
{
    Log.e("Test", fileList[i]);
}

I've always written what you're trying to do

File folder = new File("file:///android_asset");
for(int i = 0; i < folder.listFiles().length; i++)
{
    Log.e("Test", folder.listFiles()[i].getName());
}

Using an AssetManager .

For example

AssetManager mgr = App.getContext().getAssetManager();
String[] listing = mgr.list("")
for(int i=0; i<listing.length; i++)
{
    Log.e("Test", listing[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM