Example usage for android.content.res AssetManager list

List of usage examples for android.content.res AssetManager list

Introduction

In this page you can find the example usage for android.content.res AssetManager list.

Prototype

public @Nullable String[] list(@NonNull String path) throws IOException 

Source Link

Document

Return a String array of all the assets at the given path.

Usage

From source file:Main.java

public static void copyAssets(Context context, File targetDir) {
    AssetManager assetManager = context.getAssets();
    String[] files = null;/*from   w ww  . j a  v a2  s  .  c  om*/
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for (String filename : files) {

        if (isSupported(filename)) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);

                File outFile = new File(targetDir, filename);

                out = new FileOutputStream(outFile);
                copyFile(in, out);
            } catch (IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        Log.e("tag", "Failed to close inputstream: " + filename, e);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        Log.e("tag", "Failed to close outputstream: " + filename, e);
                    }
                }
            }
        }
    }
}

From source file:Main.java

private static boolean copyAsset(AssetManager am, File rep, boolean force) {
    boolean existingInstall = false;
    try {//from  w w w  . j  a  va 2s  .co  m
        String assets[] = am.list("");
        for (int i = 0; i < assets.length; i++) {
            boolean hp48 = assets[i].equals("hp48");
            boolean hp48s = assets[i].equals("hp48s");
            boolean ram = assets[i].equals("ram");
            boolean rom = assets[i].equals("rom");
            boolean rams = assets[i].equals("rams");
            boolean roms = assets[i].equals("roms");
            int required = 0;
            if (ram)
                required = 131072;
            else if (rams)
                required = 32768;
            else if (rom)
                required = 524288;
            else if (roms)
                required = 262144;
            //boolean SKUNK = assets[i].equals("SKUNK");
            if (hp48 || rom || ram || hp48s || roms || rams) {
                File fout = new File(rep, assets[i]);
                existingInstall |= fout.exists();
                if (!fout.exists() || fout.length() == 0 || (required > 0 && fout.length() != required)
                        || force) {
                    Log.i("x48", "Overwriting " + assets[i]);
                    FileOutputStream out = new FileOutputStream(fout);
                    InputStream in = am.open(assets[i]);
                    byte buffer[] = new byte[8192];
                    int n = -1;
                    while ((n = in.read(buffer)) > -1) {
                        out.write(buffer, 0, n);
                    }
                    out.close();
                    in.close();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return existingInstall;
}

From source file:net.survivalpad.android.MainActivity.java

private static void copyFromAsset(Context context) {
    if (!BuildConfig.DEBUG && context.getCacheDir().list().length > 0) {
        return;// ww w  .jav a  2s.c o  m
    }

    AssetManager am = context.getAssets();
    String[] files = null;
    try {
        files = am.list("");
    } catch (IOException e) {
    }

    if (files == null) {
        return;
    }

    for (String file : files) {
        Log.d(TAG, "file = " + file);
        File to = new File(context.getCacheDir(), file);

        try {
            FileUtils.copy(am.open(file), to);
        } catch (IOException e) {
        }

    }
}

From source file:most.voip.api.Utils.java

static void copyAssets(Context ctx) {
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;//from  ww w . j a  va2  s  .c  o  m
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e(TAG, "Failed to get asset file list.", e);
    }
    for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outFile = new File(ctx.getExternalFilesDir(null), filename);
            Log.d(TAG, "Copying asset to " + outFile.getAbsolutePath());
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}

From source file:most.voip.api.Utils.java

/**
 * Copy the specified resource file from the assets folder into the "files dir" of this application, so that this resource
 * can be opened by the Voip Lib by providing it the absolute path of the copied resource
 * @param ctx The application context/*from   w  w  w.  j av a  2s  .c  o m*/
 * @param assetPath The path of the resource (e.g on_hold.wav or sounds/on_hold.wav)
 * @return the absolute path of the copied resource, or null if no file was copied.
 */
public static String getResourcePathByAssetCopy(Context ctx, String assetSubFolder, String fileToCopy) {
    Log.d(TAG, "getResourcePathByAssetCopy on folder *" + assetSubFolder + "* for file:" + fileToCopy);
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;
    String filename = null;

    try {
        files = assetManager.list(assetSubFolder);
        Log.d(TAG, "Found " + files.length + " files");
        if (files.length > 0) {
            for (String f : files) {
                Log.d(TAG, "Found resource:" + f);
                if (f == null)
                    continue;
                if (f.equals(fileToCopy)) {

                    filename = f;
                    break;
                }
            }

            Log.d(TAG, "Found:" + filename);
            if (filename == null)
                return null;

            InputStream in = null;
            OutputStream out = null;
            try {

                in = assetManager.open(filename);
                File outFile = new File(ctx.getExternalFilesDir(null), filename);
                Log.d(TAG, "Copying asset to " + outFile.getAbsolutePath());
                out = new FileOutputStream(outFile);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                return outFile.getAbsolutePath();
            } catch (IOException e) {
                Log.e(TAG, "Failed to copy asset file: " + filename, e);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "Failed to get asset file list.", e);
    }
    return null;
}

From source file:com.orange.ocara.tools.AssetsHelper.java

/**
 * Copy the asset at the specified path to this app's data directory. If the
 * asset is a directory, its contents are also copied.
 *///from   ww w . ja  va2 s  .c o  m
public static void copyAsset(AssetManager assetManager, String rootPath, String path, File targetFolder)
        throws IOException {

    String fullPath = StringUtils.isEmpty(path) ? rootPath : rootPath + File.separator + path;

    // If we have a directory, we make it and recurse. If a file, we copy its
    // contents.
    try {
        String[] contents = assetManager.list(fullPath);

        // The documentation suggests that list throws an IOException, but doesn't
        // say under what conditions. It'd be nice if it did so when the path was
        // to a file. That doesn't appear to be the case. If the returned array is
        // null or has 0 length, we assume the path is to a file. This means empty
        // directories will get turned into files.
        if (contents == null || contents.length == 0) {
            throw new IOException();
        }

        // Recurse on the contents.
        for (String entry : contents) {
            String newPath = StringUtils.isEmpty(path) ? entry : path + File.separator + entry;

            copyAsset(assetManager, rootPath, newPath, targetFolder);
        }
    } catch (IOException e) {

        File file = new File(targetFolder, path);

        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }

        FileUtils.copyInputStreamToFile(assetManager.open(fullPath), file);
    }
}

From source file:Main.java

public static void copyFolder(AssetManager assetManager, String source, String target) {
    // "Name" is the name of your folder!
    String[] files = null;//  w w  w .  j a va 2s.  co m

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        // Checking file on assets subfolder
        try {
            files = assetManager.list(source);
        } catch (IOException e) {
            Log.e("ERROR", "Failed to get asset file list.", e);
        }
        // Analyzing all file on assets subfolder
        for (String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            // First: checking if there is already a target folder
            File folder = new File(target);
            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }
            if (success) {
                // Moving all the files on external SD
                String sourceFile = source + "/" + filename;
                String targetFile = folder.getAbsolutePath() + "/" + filename;
                try {
                    in = assetManager.open(sourceFile);
                    out = new FileOutputStream(targetFile);
                    /*Log.i("WEBVIEW",
                      Environment.getExternalStorageDirectory()
                            + "/yourTargetFolder/" + name + "/"
                            + filename);*/
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                } catch (IOException e) {
                    try {
                        assetManager.list(sourceFile);
                    } catch (IOException f) {
                        Log.e("ERROR", "Failed to copy asset file: " + filename, f);
                        continue;
                    }

                    copyFolder(assetManager, sourceFile, targetFile);
                }
            } else {
                // Do something else on failure
            }
        }
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
    } else {
        // Something else is wrong. It may be one of many other states, but
        // all we need
        // is to know is we can neither read nor write
    }
}

From source file:com.clutch.ClutchSync.java

private static boolean copyAssetDir(AssetManager mgr, String path, File outFile) throws IOException {
    String[] assets = mgr.list(path);
    if (assets.length == 0) {
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }//w w  w.j a  va  2s  .  c o m
        if (!outFile.exists()) {
            outFile.createNewFile();
        }
        InputStream in = mgr.open(path);
        OutputStream out = new FileOutputStream(outFile);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        out.close();
        return true;
    }

    for (String asset : assets) {
        if (!copyAssetDir(mgr, path + "/" + asset, new File(outFile, asset))) {
            return false;
        }
    }

    return true;
}

From source file:uk.co.armedpineapple.cth.Files.java

private static void listAssetsInternal(Context ctx, String path, ArrayList<String> paths) throws IOException {
    AssetManager assetManager = ctx.getAssets();
    String assets[];//from  w w  w .  j  av a  2  s  .co  m

    assets = assetManager.list(path);

    if (assets.length == 0) {
        paths.add(path);

    } else {
        for (String asset : assets) {
            listAssetsInternal(ctx, path + "/" + asset, paths);
        }
    }

}

From source file:cx.ring.client.HomeActivity.java

private static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
    try {//  ww w.  ja  v  a 2 s  .  c om
        String[] files = assetManager.list(fromAssetPath);
        new File(toPath).mkdirs();
        Log.i(TAG, "Creating :" + toPath);
        boolean res = true;
        for (String file : files)
            if (file.contains("")) {
                Log.i(TAG, "Copying file :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
                res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
            } else {
                Log.i(TAG, "Copying folder :" + fromAssetPath + "/" + file + " to " + toPath + "/" + file);
                res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
            }
        return res;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}