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 asset(Context context, String assetPath) {
    try {/*  w  w w.j  a va  2 s . co m*/
        StringBuilder buf = new StringBuilder();
        InputStream inputStream = context.getAssets().open("body_files/" + assetPath);
        return inputStreamToString(inputStream, "UTF-8");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.musicpd.activities.fragments.LicenseFragment.java

private static String loadLicenseText(Context context) {
    String out = null;/*from  w ww . j  a  va  2  s. c o  m*/

    try {
        InputStream is = context.getAssets().open(LCNSE_FILE);

        if (is != null) {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            StringBuilder license = new StringBuilder();

            while (true) {
                String readLine = br.readLine();
                if (readLine == null)
                    break;
                license.append(readLine).append("\n");
            }

            br.close();

            out = license.toString();
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found: " + e.toString());
    } catch (Exception e) {
        System.out.println("Can not read file: " + e.toString());
    }

    return out;
}

From source file:Main.java

public static Typeface get(Context c, String name) {
    synchronized (cache) {
        if (!cache.containsKey(name)) {
            Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s.ttf", name));
            cache.put(name, t);//  w  w w  .java 2s.c  o m
            return t;
        }
        return cache.get(name);
    }
}

From source file:Main.java

/**
 * From http://stackoverflow.com/questions/9544737/read-file-from-assets
 *//*w w  w  . j  av  a  2 s  . com*/
public static String readAsset(Context context, String fileName) throws IOException {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(context.getAssets().open(fileName), "UTF-8"));

    String line;
    StringBuilder stringBuilder = new StringBuilder();

    while ((line = reader.readLine()) != null)
        stringBuilder.append(line + "\n");

    reader.close();
    return stringBuilder.toString();
}

From source file:Main.java

private static Properties loadPropties(Context context, String file) throws IOException {
    Properties properties = new Properties();
    try {//from  w w  w.  j ava  2s  .  co m
        InputStream fileStream = context.getAssets().open(file);
        properties.load(fileStream);
        fileStream.close();
    } catch (FileNotFoundException e) {
    }
    return properties;
}

From source file:Main.java

public static void playNotifycationMusic(Context context, String voicePath) throws IOException {
    // paly music ...
    AssetFileDescriptor fileDescriptor = context.getAssets().openFd(voicePath);
    if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();
    }/* w  w  w.j  ava 2s.  c o m*/
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
    mediaPlayer.reset();
    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
            fileDescriptor.getLength());
    mediaPlayer.prepare();
    mediaPlayer.setLooping(false);
    mediaPlayer.start();
}

From source file:Main.java

/**
 * check asset exists//from  w  w  w  .  java2 s. co  m
 *
 * @param context
 * @param assetFilePath
 * @return
 */
public static boolean exists(final Context context, final String assetFilePath) {
    boolean bAssetOk = false;
    try {
        InputStream stream = context.getAssets().open(assetFilePath);
        stream.close();
        bAssetOk = true;
    } catch (Exception e) {
    }
    return bAssetOk;
}

From source file:Main.java

public static String readTextFromFile(Context context, String filePath) throws IOException {
    StringBuilder builder = new StringBuilder();

    AssetManager manager = context.getAssets();
    InputStream stream = manager.open(filePath.replace(ASSET_FOLDER_PREFIX, ""));
    InputStreamReader streamReader = new InputStreamReader(stream);
    BufferedReader bufferedReader = new BufferedReader(streamReader);
    String line;/*ww  w . ja  v  a  2s .c om*/
    while ((line = bufferedReader.readLine()) != null) {
        builder.append(line);
        builder.append('\n');
    }
    bufferedReader.close();

    return builder.toString();
}

From source file:mc.lib.assets.AssetsHelper.java

public static String readText(Context context, String path) {
    InputStream is = null;//from  w  ww .j  a v a  2s .c o  m
    try {
        is = context.getAssets().open(path);
        return StreamHelper.readStream(is);
    } catch (IOException e) {
        Log.e(LOGTAG, "Can not read asset " + path, e);
    } finally {
        StreamHelper.close(is);
    }
    return null;
}

From source file:mc.lib.assets.AssetsHelper.java

public static Bitmap readImageToBitmap(Context context, String path) {
    InputStream is = null;/*from  ww w.  ja va  2s.  co  m*/
    try {
        is = context.getAssets().open(path);
        return BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        Log.e(LOGTAG, "Can not read asset " + path, e);
    } finally {
        StreamHelper.close(is);
    }
    return null;
}