下面列出了android.content.res.AssetManager#list() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void copyFileOrDir(String path) {
AssetManager assetManager = context.getAssets();
String assets[] = null;
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyFile(path);
} else {
String fullPath = configsPath;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdirs();
for (int i = 0; i < assets.length; ++i) {
copyFileOrDir(path + "/" + assets[i]);
}
}
} catch (IOException ex) {
}
}
private static void copyFileOrDir(String path, Context context) {
try {
AssetManager assetManager = context.getAssets();
String assets[] = null;
assets = assetManager.list(path);
if (assets.length == 0) {
copyFile(path,context);
} else {
String fullPath = "/data/data/" + context.getPackageName() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
for (int i = 0; i < assets.length; ++i) {
copyFileOrDir(path + "/" + assets[i],context);
}
}
} catch (IOException ex) {
Log.e("tag", "I/O Exception", ex);
}
}
private void initBackgroundPic()
{
AssetManager am = getActivity().getAssets();
try {
String[] drawableList = am.list("bkgs");
mBgPicList = new ArrayList<BgPicEntity>();
for (String path : drawableList) {
BgPicEntity bg = new BgPicEntity();
InputStream is = am.open("bkgs/" + path);
Bitmap bitmap = BitmapFactory.decodeStream(is);
bg.path = path;
bg.bitmap = bitmap;
mBgPicList.add(bg);
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* V2 - Helper function to copy multiple files from asset to SDCard
*/
public void copyFileOrDir(String fullPath, String path) {
AssetManager assetManager = context.getAssets();
String assets[];
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyFile(fullPath, path);
} else {
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
for (String asset : assets) {
copyFileOrDir(fullPath, path + "/" + asset);
}
}
} catch (IOException ex) {
Log.e("Sample Data", "I/O Exception", ex);
}
}
private boolean doInBackground() {
boolean ok = true;
AssetManager asset = mContext.getAssets();
try {
File rootDir = new File(DeviceDirName + RootDirName);
if (!rootDir.exists()) {
rootDir.mkdir();
}
String[] fileList = asset.list(RootDirName);
if (null != fileList) {
for (String level : fileList) {
String realPath = RootDirName + File.separator + level;
if (isFile(realPath)) {
handleFile(asset, realPath);
} else {
handleDirectory(asset, realPath);
}
}
}
} catch (Exception e) {
e.printStackTrace();
ok = false;
}
return ok;
}
private void loadExtensions() {
AssetManager assetManager = cordova.getActivity().getAssets();
String[] extList;
try {
Log.i(TAG, "Iterate assets/xwalk-extensions folder");
extList = assetManager.list(XWALK_EXTENSIONS_FOLDER);
} catch (IOException e) {
Log.w(TAG, "Failed to iterate assets/xwalk-extensions folder");
return;
}
for (String path : extList) {
// Load the extension.
Log.i(TAG, "Start to load extension: " + path);
webView.getExtensionManager().loadExtension(XWALK_EXTENSIONS_FOLDER + File.separator + path);
}
}
private void checkFirstLaunch() {
if (mIconsDirectory.list().length == 0) {
AssetManager manager = mContext.getAssets();
try {
for (String assetChild : manager.list("icons")) {
String path = mContext.getFilesDir().getAbsolutePath() + MainActivity.ICONS_PATH +
File.separator + assetChild;
Utils.writeFile(path, manager.open("icons/" + assetChild));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void copyAssetFolder(AssetManager assetManager,
String fromAssetPath, String toPath) throws IOException {
String[] files = assetManager.list(fromAssetPath);
new File(toPath).mkdirs();
for (String file : files)
{
// try to copy file (will fail if its a directory)
try
{
copyAsset(assetManager, fromAssetPath + "/" + file, toPath
+ "/" + file);
}
catch(FileNotFoundException e)
{
copyAssetFolder(assetManager, fromAssetPath + "/" + file,
toPath + "/" + file);
}
}
}
/**
* If the litepal.xml configuration file exists.
* @return True if exists, false otherwise.
*/
public static boolean isLitePalXMLExists() {
try {
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.Config.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return true;
}
}
}
} catch (IOException e) {
}
return false;
}
private static void copyAssetsFileOrDirToExternalStorage(Context context, String path) {
AssetManager assetManager = context.getAssets();
String assets[] = null;
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyAssetsFileToExternalStorage(context, path);
} else {
String fullPath = Environment.getExternalStorageDirectory() + "/" + APP_DIR + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
for (int i = 0; i < assets.length; ++i) {
copyAssetsFileOrDirToExternalStorage(context, path + "/" + assets[i]);
}
}
} catch (IOException ex) {
Log.e("tag", "I/O Exception", ex);
}
}
private void getFontFromAssets() {
mTypefaceList.add(Typeface.DEFAULT);
String[] fontNameList = null;
AssetManager assetManager = mContext.getAssets();
try {
fontNameList = assetManager.list(FontPopup.FONTS);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < fontNameList.length; i++) {
String fontPath = FontPopup.FONTS + "/" + fontNameList[i];
Typeface typeface = Typeface.createFromAsset(assetManager, fontPath);//根据路径得到Typeface
mTypefaceList.add(typeface);
}
}
private String getUpgradeVersionFromAsset(){
String versionName = "";
try {
//query local version
AssetManager am = getActivity().getAssets();
String[] files = am.list("dfu");
for (String fileName : files) {
if (fileName.endsWith(".ver")) {
versionName = fileName.substring(0, fileName.lastIndexOf(".ver"));
}
}
}catch (Exception e){
e.printStackTrace();
}
return versionName;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pubu_layout);
puBuLayout = (PuBuLayout) findViewById(R.id.pubu_layout);
//获取AssetManager
AssetManager manager = getAssets();
try {
//获取Assets目录中的文件,得到文件名数组
String[] images = manager.list("images");
for(int i=0; i<images.length;i++){
//向布局中添加Bitmap
//把文件转换Bitmap
InputStream in = manager.open("images/" + images[i]);
Bitmap bitmap = BitmapFactory.decodeStream(in);
puBuLayout.addImage(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_chooser_activity);
Intent intent = getIntent();
String dataUri = intent.getDataString();
String[] uris;
if (dataUri != null) {
uris = new String[] {dataUri};
} else {
ArrayList<String> uriList = new ArrayList<>();
AssetManager assetManager = getAssets();
try {
for (String asset : assetManager.list("")) {
if (asset.endsWith(".exolist.json")) {
// uriList.add("asset:///" + asset);
}else if(asset.endsWith(".exolist.new.json")){
uriList.add("asset:///" + asset);
}
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.sample_list_load_error, Toast.LENGTH_LONG)
.show();
}
uris = new String[uriList.size()];
uriList.toArray(uris);
Arrays.sort(uris);
}
SampleListLoader loaderTask = new SampleListLoader();
loaderTask.execute(uris);
}
protected void copyAllTestSoundsToStorage() throws IOException {
String[] assets = null;
AssetManager am = getContext().getAssets();
File destBaseDir = getTempDir();
assets = am.list(TESTSOUND_DIR);
for (String asset : assets) {
CommonTestCaseUtils.copyAssetFile(
am, TESTSOUND_DIR + File.separator + asset,
destBaseDir, false);
}
}
private InputStream getInputStream() throws IOException {
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.Config.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
}
}
}
throw new ParseConfigurationFileException(
ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
}
private Bitmap loadFirstImage() throws IOException {
checkPermissions();
Context testContext = InstrumentationRegistry.getInstrumentation().getContext();
AssetManager assetManager = testContext.getAssets();
String pathToImages = "images";
String[] list = assetManager.list(pathToImages);
return getBitmapFromTestAssets(list[0], pathToImages);
}
void initList() {
AssetManager am = context.getAssets();
String[] path;
try {
path = am.list("bg"); //获取所有,填入目录获取该目录下所有资源
} catch (IOException e) {
e.printStackTrace();
return;
}
assetsFiles = new ArrayList<>();
Collections.addAll(assetsFiles, path);
}
static void displayFiles(final AssetManager mgr, final String path) {
try {
final String list[] = mgr.list(path);
if (list != null) {
for (int i = 0; i < list.length; ++i) {
Log.v("Assets:", path + "/" + list[i]);
displayFiles(mgr, path + "/" + list[i]);
}
}
} catch (final IOException e) {
Log.v("List error:", "can't list" + path);
}
}
public static String[] allFiles(Context context) throws IOException {
AssetManager manager = context.getAssets();
return manager.list("");
}