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:bizapps.com.healthforusPatient.activity.RegisterActivity.java

private void CopyAssetsbrochure(String fName) {
    AssetManager assetManager = getAssets();
    String[] files = null;//from  w  w  w. ja  v  a 2s .  c  o m
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
    for (int i = 0; i < files.length; i++) {
        String fStr = files[i];
        if (fStr.equalsIgnoreCase(fName)) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(files[i]);
                out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                break;
            } catch (Exception e) {
                Log.e("tag", e.getMessage());
            }
        }
    }
}

From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java

private void loadThemeAudible(Context themeCtx, int type, PackageInfo pi) {
    AssetManager assetManager = themeCtx.getAssets();
    String assetPath;//  w w w .j ava 2s. com
    switch (type) {
    case RingtoneManager.TYPE_ALARM:
        assetPath = "alarms";
        break;
    case RingtoneManager.TYPE_NOTIFICATION:
        assetPath = "notifications";
        break;
    case RingtoneManager.TYPE_RINGTONE:
        assetPath = "ringtones";
        break;
    default:
        assetPath = null;
        break;
    }
    if (assetPath != null) {
        try {
            String[] assetList = assetManager.list(assetPath);
            if (assetList != null && assetList.length > 0) {
                AssetFileDescriptor afd = assetManager.openFd(assetPath + File.separator + assetList[0]);
                MediaPlayer mp = initAudibleMediaPlayer(afd, type);
                if (mp != null) {
                    addAudibleToLayout(type, mp);
                }
            }
        } catch (IOException e) {
            mMediaPlayers.put(type, null);
        }
    }
}

From source file:interactivespaces.controller.android.InteractiveSpacesFrameworkAndroidBootstrap.java

/**
 * Get all of the bundles from the bootstrap folder.
 *
 * @param assetManager//from  w w w  . j ava 2 s .  c  o  m
 *          the android asset manager for the application
 * @param bootstrapFolderPath
 *          The folder where the bootstrap folders are stored.
 *
 * @throws IOException
 */
private void getBootstrapBundleJars(AssetManager assetManager, String bootstrapFolderPath) throws IOException {
    // Look in the specified bundle directory to create a list
    // of all JAR files to install.
    initialBundles = new ArrayList<String>();
    finalBundles = new ArrayList<String>();

    for (String filename : assetManager.list(bootstrapFolderPath)) {
        if (filename.endsWith(".jar")) {
            if (filename.startsWith("interactivespaces")) {
                finalBundles.add(bootstrapFolderPath + "/" + filename);
            } else {
                initialBundles.add(bootstrapFolderPath + "/" + filename);
            }
        }
    }
}

From source file:com.chummy.jezebel.material.dark.activities.Main.java

private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;/*  w w  w  .  ja  v  a 2 s .co  m*/
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    if (files != null)
        for (String filename : files) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(filename);
                File outFile = new File(getExternalFilesDir(null), 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", "Could not close in", e);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        Log.e("tag", "Could not close out", e);
                    }
                }
            }
        }
}

From source file:com.gnuroot.debian.GNURootMain.java

private void copyAssets(String packageName) {
    Context friendContext = null;
    try {//from w w w  .  j  a  v a2  s .  c o m
        friendContext = this.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
    } catch (NameNotFoundException e1) {
        return;
    }
    AssetManager assetManager = friendContext.getAssets();
    File tempFile = new File(getInstallDir().getAbsolutePath() + "/support");
    if (!tempFile.exists()) {
        tempFile.mkdir();
    }
    String[] files = null;
    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);
            filename = filename.replace(".mp2", "");
            filename = filename.replace(".mp3", ".tar.gz");
            File outFile = new File(tempFile, filename);
            out = new FileOutputStream(outFile);
            if (filename.contains(".tar.gz"))
                out = openFileOutput(filename, MODE_PRIVATE);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            exec("chmod 0777 " + outFile.getAbsolutePath(), true);
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}

From source file:org.appcelerator.titanium.util.TiUIHelper.java

@SuppressLint("DefaultLocale")
private static Typeface loadTypeface(final Context context, final String fontFamily) {
    if (context == null) {
        return null;
    }//www  .  j av  a 2s  .c om
    if (mCustomTypeFaces.containsKey(fontFamily)) {
        return mCustomTypeFaces.get(fontFamily);
    }
    if (mCustomTypeFacesList == null) {
        AssetManager mgr = context.getAssets();
        try {
            ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(mgr.list(customFontPath)));
            Iterator it = fileList.iterator();
            while (it.hasNext()) {
                String text = (String) it.next();
                if (!text.endsWith(".ttf") && !text.endsWith(".otf")) {
                    it.remove();
                }
            }
            mCustomTypeFacesList = fileList.toArray(new String[] {});
        } catch (Exception e) {
            Log.e(TAG, "Unable to load 'fonts' assets. Perhaps doesn't exist? " + e.getMessage());
        }
    }
    if (mCustomTypeFacesList != null) {
        Typeface tf = null;
        AssetManager mgr = context.getAssets();
        for (String f : mCustomTypeFacesList) {
            if (f.equals(fontFamily) || f.startsWith(fontFamily + ".") || f.startsWith(fontFamily + "-")) {
                tf = Typeface.createFromAsset(mgr, customFontPath + "/" + f);
                synchronized (mCustomTypeFaces) {
                    mCustomTypeFaces.put(fontFamily, tf);
                }
                return tf;
            }
        }
        tf = Typeface.create(fontFamily, Typeface.NORMAL);
        if (tf != null) {
            synchronized (mCustomTypeFaces) {
                mCustomTypeFaces.put(fontFamily, tf);
            }
            return tf;
        }
    }

    mCustomTypeFaces.put(fontFamily, null);
    return null;
}

From source file:com.softanalle.scma.MainActivity.java

private void copyTestAssets() {
    logger.debug("copyTestAssets()");
    AssetManager am = getResources().getAssets();
    String[] list;//from   w  ww.  j a va  2  s .  c om
    try {
        list = am.list("test");
        File destDir = new File(mStorageDir);
        for (String s : list) {

            File dstFile = new File(destDir, s);

            InputStream in = am.open("test/" + s);

            FileUtils.copyInputStreamToFile(in, dstFile);

            Log.d(TAG, "Copied: " + s);
            Log.d(TAG, "Dest: " + mStorageDir);
            in.close();

        }
    } catch (IOException e) {

        e.printStackTrace();
        String message = "";
        if (e.getMessage() != null) {
            message = e.getMessage();
        }
        showError(e.toString(), message);
    } catch (Exception e) {
        String message = "";
        if (e.getMessage() != null) {
            message = e.getMessage();
        }
        showError(e.toString(), message);
    }

}

From source file:interactivespaces.controller.android.InteractiveSpacesFrameworkAndroidBootstrap.java

/**
 * Copy all asserts needed for initial boot.
 *
 * @param assetManager/*from www  .j av  a  2  s  .c o  m*/
 *          the android asset manager for the application
 * @param filesDir
 *          the folder where items can be copied to
 */
private void copyInitialBootstrapAssets(AssetManager assetManager, File filesDir) {
    try {
        File confDir = new File(filesDir, "config");
        confDir.mkdirs();
        File isConfigDir = new File(confDir, "interactivespaces");
        isConfigDir.mkdirs();
        File bootstrapDir = new File(filesDir, "bootstrap");
        bootstrapDir.mkdirs();
        File systemLibDir = new File(filesDir, "lib/system/java");
        systemLibDir.mkdirs();
        File logsDir = new File(filesDir, "logs");
        logsDir.mkdirs();
        File activitiesStagingDir = new File(filesDir, "controller/activities/staging");
        activitiesStagingDir.mkdirs();
        File activitiesInstalledDir = new File(filesDir, "controller/activities/installed");
        activitiesInstalledDir.mkdirs();

        copyAssetFile("config/container.conf", new File(confDir, "container.conf"), assetManager);

        String[] configFiles = assetManager.list("config/interactivespaces");
        for (String configFile : configFiles) {
            copyAssetFile("config/interactivespaces/" + configFile, new File(isConfigDir, configFile),
                    assetManager);
        }

        copyAssetFile("lib/system/java/log4j.properties", new File(systemLibDir, "log4j.properties"),
                assetManager);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.androzic.Androzic.java

/**
 * Copies file assets from installation package to filesystem.
 *///ww w.ja  v a2s.co m
public void copyAssets(String folder, File path) {
    Log.i(TAG, "CopyAssets(" + folder + ", " + path + ")");
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list(folder);
    } catch (IOException e) {
        Log.e("Androzic", "Failed to get assets list", e);
        return;
    }
    for (String file : files) {
        try {
            InputStream in = assetManager.open(folder + "/" + file);
            OutputStream out = new FileOutputStream(new File(path, file));
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            Log.e("Androzic", "Asset copy error", e);
        }
    }
}