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:com.kabootar.GlassMemeGenerator.ImageOverlay.java

/**
 * Draws the given string centered, as big as possible, on either the top or
 * bottom 20% of the image given./*from www. j  av a  2  s . co  m*/
 */
private static void drawStringCentered(Canvas g, String text, Bitmap image, boolean top, Context baseContext)
        throws InterruptedException {
    if (text == null)
        text = "";

    int height = 0;
    int fontSize = MAX_FONT_SIZE;
    int maxCaptionHeight = image.getHeight() / 5;
    int maxLineWidth = image.getWidth() - SIDE_MARGIN * 2;
    String formattedString = "";
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    Paint stkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    stkPaint.setStyle(STROKE);
    stkPaint.setStrokeWidth(8);
    stkPaint.setColor(Color.BLACK);

    //Typeface tf = Typeface.create("Arial", Typeface.BOLD);
    Typeface tf = Typeface.createFromAsset(baseContext.getAssets(), "fonts/impact.ttf");

    paint.setTypeface(tf);
    stkPaint.setTypeface(tf);
    do {

        paint.setTextSize(fontSize);

        // first inject newlines into the text to wrap properly
        StringBuilder sb = new StringBuilder();
        int left = 0;
        int right = text.length() - 1;
        while (left < right) {

            String substring = text.substring(left, right + 1);
            Rect stringBounds = new Rect();
            paint.getTextBounds(substring, 0, substring.length(), stringBounds);
            while (stringBounds.width() > maxLineWidth) {
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                }

                // look for a space to break the line
                boolean spaceFound = false;
                for (int i = right; i > left; i--) {
                    if (text.charAt(i) == ' ') {
                        right = i - 1;
                        spaceFound = true;
                        break;
                    }
                }
                substring = text.substring(left, right + 1);
                paint.getTextBounds(substring, 0, substring.length(), stringBounds);

                // If we're down to a single word and we are still too wide,
                // the font is just too big.
                if (!spaceFound && stringBounds.width() > maxLineWidth) {
                    break;
                }
            }
            sb.append(substring).append("\n");
            left = right + 2;
            right = text.length() - 1;
        }

        formattedString = sb.toString();

        // now determine if this font size is too big for the allowed height
        height = 0;
        for (String line : formattedString.split("\n")) {
            Rect stringBounds = new Rect();
            paint.getTextBounds(line, 0, line.length(), stringBounds);
            height += stringBounds.height();
        }
        fontSize--;
    } while (height > maxCaptionHeight);

    // draw the string one line at a time
    int y = 0;
    if (top) {
        y = TOP_MARGIN;
    } else {
        y = image.getHeight() - height - BOTTOM_MARGIN;
    }
    for (String line : formattedString.split("\n")) {
        // Draw each string twice for a shadow effect
        Rect stringBounds = new Rect();
        paint.getTextBounds(line, 0, line.length(), stringBounds);
        //paint.setColor(Color.BLACK);
        //g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2 + 2, y + stringBounds.height() + 2, paint);

        paint.setColor(Color.WHITE);
        g.drawText(line, (image.getWidth() - (int) stringBounds.width()) / 2, y + stringBounds.height(), paint);

        //stroke
        Rect strokeBounds = new Rect();
        stkPaint.setTextSize(fontSize);
        stkPaint.getTextBounds(line, 0, line.length(), strokeBounds);
        g.drawText(line, (image.getWidth() - (int) strokeBounds.width()) / 2, y + strokeBounds.height(),
                stkPaint);

        y += stringBounds.height();
    }
}

From source file:com.applozic.mobicommons.file.FileUtils.java

public static List<String> loadRestrictedWordsFile(Context context) {

    BufferedReader br = null;/* ww w. ja va2  s.c o m*/
    StringBuffer sb = new StringBuffer();

    try {
        br = new BufferedReader(new InputStreamReader(context.getAssets().open("restrictWords.txt"), "UTF-8"));
        String line;
        if (br != null) {

            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        }
    } catch (IOException ioe) {
        return null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
        }
    }
    String outputString = sb.toString();
    String[] words = outputString.split(",");
    List<String> wordList = Arrays.asList(words);
    return wordList;
}

From source file:com.noshufou.android.su.util.Util.java

public static final void copyFromAssets(Context context, String source, String destination) throws IOException {

    // read file from the apk
    InputStream is = context.getAssets().open(source);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);//from   ww w  . j  a v  a 2 s  .c  o m
    is.close();

    // write files in app private storage
    FileOutputStream output = context.openFileOutput(destination, Context.MODE_PRIVATE);
    output.write(buffer);
    output.close();

    Log.d(TAG, source + " asset copied to " + destination);
}

From source file:com.applozic.mobicommons.file.FileUtils.java

public static String loadSettingsJsonFile(Context context) {
    BufferedReader br = null;/*from  ww w . j av  a 2 s.c  om*/
    StringBuffer sb = new StringBuffer();

    try {
        br = new BufferedReader(
                new InputStreamReader(context.getAssets().open("applozic-settings.json"), "UTF-8"));
        String line;
        if (br != null) {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        }
    } catch (IOException ioe) {
        return null;
    } catch (Exception e) {
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
        }
    }
    return sb.toString();
}

From source file:com.manning.androidhacks.hack011.view.LedTextView.java

private void init(Context context) {
    AssetManager assets = context.getAssets();
    final Typeface font = Typeface.createFromAsset(assets, FONT_DIGITAL_7);
    setTypeface(font);/* w  w  w .j  a v a2 s  . com*/
}

From source file:mr.robotto.ApplicationTest.java

public void testLoaders() {
    Context context = getContext();
    AssetManager am = context.getAssets();
    try {//from   ww w . jav  a 2 s  . c om
        InputStream stream = am.open("kingVer3.json");
        JSONObject drac = (JSONObject) new JSONTokener(MrStreamReader.read(stream)).nextValue();
        /*MrObjectLoader loader = new MrObjectLoader(drac);
        MrSceneData ob = (MrSceneData)loader.parse();
        getRenderer().setScene(ob);
        getRenderer().model = new MrModelController((MrModelData)ob.getChildren().findByKey(0), new MrModelRender());*/
        //MrRobottoJsonLoader loader = new MrRobottoJsonLoader(drac);
        //context1 = loader.parse();
        //getRenderer().setScene((MrSceneData)context1.getObjectsData().findByKey("Scene"));
        //getRenderer().model = new MrModelController((MrModelData)context1.getObjectsData().findByKey("Cube"), new MrModelRender());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    assertNotNull(null);
}

From source file:com.wheelphone.targetDocking.HttpServer.java

public HttpServer(int port, Context context, Handler handler) {
    super(port, context.getAssets());
    this.context = context;
    addRequestHandler("/spydroid.sdp*", new DescriptorRequestHandler(handler));
}

From source file:com.ris.mobile.ecloud.util.AbFileUtil.java

/**
 * ???Asset?.//w  w w  .j a va 2  s.  c  o m
 *
 * @param context the context
 * @param fileName the file name
 * @return Bitmap 
 */
public static Bitmap getBitmapFromAsset(Context context, String fileName) {
    Bitmap bit = null;
    try {
        AssetManager assetManager = context.getAssets();
        InputStream is = assetManager.open(fileName);
        bit = BitmapFactory.decodeStream(is);
    } catch (Exception e) {
        //AbLogUtil.d(AbFileUtil.class, "?"+e.getMessage());
    }
    return bit;
}

From source file:com.oprisnik.semdroid.permissions.AndroidPermissionMapFactory.java

public AndroidPermissionMapFactory(Context context) {
    mPermissionMap = new PermissionMap();
    InputStream is = null;/*from   ww  w  . j a  v a2 s.c  o  m*/

    try {
        is = context.getAssets()
                .open("config" + File.separator + "permissionmap" + File.separator + "APICalls.txt");
        mPermissionMap.createMap(new BufferedReader(new InputStreamReader(is)));
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.bnrc.util.AbFileUtil.java

/**
 * ??sset?.// w  ww.  j a  v  a  2 s  .co m
 *
 * @param context the context
 * @param fileName the file name
 * @return Bitmap ?
 */
public static Bitmap getBitmapFromAsset(Context context, String fileName) {
    Bitmap bit = null;
    try {
        AssetManager assetManager = context.getAssets();
        InputStream is = assetManager.open(fileName);
        bit = BitmapFactory.decodeStream(is);
    } catch (Exception e) {
        AbLogUtil.d(AbFileUtil.class, "?" + e.getMessage());
    }
    return bit;
}