Example usage for android.content Context getAssets

List of usage examples for android.content Context getAssets

Introduction

In this page you can find the example usage for android.content Context getAssets.

Prototype

public abstract AssetManager getAssets();

Source Link

Document

Returns an AssetManager instance for the application's package.

Usage

From source file:Main.java

public static String readAssetsByName(Context context, String name, String encoding) {
    String text = null;/*  w  w  w .j a v a  2  s  . c o m*/
    InputStreamReader inputReader = null;
    BufferedReader bufReader = null;
    try {
        inputReader = new InputStreamReader(context.getAssets().open(name));
        bufReader = new BufferedReader(inputReader);
        String line = null;
        StringBuffer buffer = new StringBuffer();
        while ((line = bufReader.readLine()) != null) {
            buffer.append(line);
        }
        text = new String(buffer.toString().getBytes(), encoding);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bufReader != null) {
                bufReader.close();
            }
            if (inputReader != null) {
                inputReader.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return text;
}

From source file:Main.java

private static void copyFileFromAssets(Context context, String destPathName, String fileName) {

    File destFile = new File(destPathName);

    if (!destFile.exists()) {

        try {//from   w ww.j  a  va 2s .  c om

            ///
            InputStream inputStream = context.getAssets().open(fileName);

            FileOutputStream outputStream = new FileOutputStream(destPathName);

            ///
            byte[] i_buffer = new byte[1024];

            for (;;) {

                int length = inputStream.read(i_buffer);

                if (length > 0) {

                    outputStream.write(i_buffer, 0, length);

                } else {

                    break;
                }
            }

            ///
            inputStream.close();

            outputStream.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

From source file:io.mpos.ui.shared.util.UiHelper.java

public static Typeface createAwesomeFontTypeface(Context context) {
    if (awesomeFont == null) {
        awesomeFont = Typeface.createFromAsset(context.getAssets(), "font/fontawesome-webfont.ttf");
    }/*  w ww .ja va  2  s  . c  o  m*/
    return awesomeFont;
}

From source file:edu.mecc.race2ged.helpers.Utils.java

/**
 * Sets the font of a view to Roboto-Bold
 * @param context The context of the activity.
 * @param view The view that will use Roboto-Bold
 *///from   w w w. j  av a2  s  . c o m
public static void setRobotoBold(Context context, View view) {
    if (sRobotoBold == null) {
        sRobotoBold = Typeface.createFromAsset(context.getAssets(), "Roboto-Bold.ttf");
    }
    setFont(view, sRobotoBold);
}

From source file:Main.java

static List<String> readAssetsText(Context context, String assetName) throws IOException {
    InputStream is = null;/*from   w  w w .  ja va 2  s  .  co  m*/
    InputStreamReader isr = null;
    BufferedReader br = null;
    List<String> lines = new ArrayList<String>();
    try {
        is = context.getAssets().open(assetName);
        isr = new InputStreamReader(is, "UTF-8");
        br = new BufferedReader(isr);
        String line = null;
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }
    } finally {
        closeQuietly(br);
        closeQuietly(isr);
        closeQuietly(is);
    }
    return lines;
}

From source file:com.android.build.gradle.internal.incremental.fixture.ClassEnhancement.java

private static Map<String, File> getCompileFolders(File folder) throws IOException {
    Context context = InstrumentationRegistry.getContext();
    String[] names = context.getAssets().list(folder.getPath());
    ImmutableMap.Builder<String, File> builder = ImmutableMap.builder();

    for (String name : names) {
        builder.put(name, new File(folder, name));
    }//from w w w.j a  v a2  s .co m

    return builder.build();
}

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

static void copyAssets(Context ctx) {
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;/*from  w w  w .  ja  v  a2s.  c  om*/
    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:Main.java

public static void copyAssets(Context context, String dir, String fileName) {
    // String[] files;
    File mWorkingPath = new File(dir);
    if (!mWorkingPath.exists()) {
        if (!mWorkingPath.mkdirs()) {
        }// w  ww  .j  a v a  2s .co m
    }
    try {
        InputStream in = context.getAssets().open(fileName);
        System.err.println("");
        File outFile = new File(mWorkingPath, fileName);
        OutputStream out = new FileOutputStream(outFile);
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

From source file:com.mobandme.ada.examples.advanced.model.helpers.CountriesLoaderHelper.java

/**
 * This method read a countries.json file from application Assets folder and return a list of Countries.
 * @param pContext//from   w ww.  j  a  v  a  2  s.c  om
 * @return List with filled Countries.
 */
public static List<Country> getList(Context pContext) {
    List<Country> returnedValue = new ArrayList<Country>();

    try {
        String data = null;

        if (pContext != null) {

            InputStream input = pContext.getAssets().open("countries.json");
            if (input != null) {

                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                if (reader != null) {
                    data = reader.readLine();
                    reader.close();
                }

                input.close();
            }

            if (data != null && !data.trim().equals("")) {
                JSONArray dataParser = new JSONArray(data);
                if (dataParser != null && dataParser.length() > 0) {
                    for (int index = 0; index < dataParser.length(); index++) {
                        returnedValue.add(new Country(dataParser.getString(index)));
                    }
                }
            }
        }

    } catch (Exception e) {
        ExceptionsHelper.manage(e);
    }

    return returnedValue;
}

From source file:codepath.watsiapp.utils.Util.java

public static void applyPrimaryFont(Context ctx, TextView textView) {
    Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "fonts/" + PRIMARY_FONT);
    textView.setTypeface(typeface);//from  w w w  . j ava  2 s . c  o m
}