简体   繁体   中英

Android assets folder is empty

I want to list folders and files in asset folder, but I find out i empty. I searched for the answer but it did not helped, on google, stackoverflow and many others.

I tried many approaches with AssetManager and only that gave some output was:

getAssets().list("/");

But assets folder it listed was empty...

I run application from eclipse (device on usb). Can anyone tell me what I do wrong? I have feeling that assets folder I open is not form my application...

Thanks in advance!

I was using getAssets().list("/"); and it returned a list of the files in the root of the app:

D/        (25498): Listing files in /
D/        (25498): AndroidManifest.xml
D/        (25498): META-INF
D/        (25498): assets
D/        (25498): classes.dex
D/        (25498): lib
D/        (25498): res
D/        (25498): resources.arsc

Then I tried listing "/assets" but it was always empty. Then I found out that a relative path is needed and / for the root folder can not be used.

getAssets().list(""); lists the files in the root of the assets folder.

Try this:

AssetManager assetManager = appContext.getAssets();
String[] filelist = assetManager.list("");

You can get the assets files like this:

try {
    Resources res = getResources();
    AssetManager am = res.getAssets();
    String fileList[] = am.list("/");
} catch (IOException e) {
    e.printStackTrace();
}

I see you were using Eclipse, I switched to Android Studio but had the same problem. Comparing many answers led me to this:

https://stackoverflow.com/a/33542563/524355

The asset folder is "next" to the res folder (in AS, there is even a wizard for it). I put it manually "into" the res folder that's why asset folder was gone/not compiled into the apk! If this is done by the build tools that you use in Eclipse (ADT) you might have suffered from the same situation.

资产不在内部,但与res文件夹平行

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