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.westlinkin.android_feedback_helper.utils.MailUntils.java

public static String getMailBody(Context context, String msg, String userEmail) {
    String html = "";
    try {/*from w ww  .  j  a va2 s .  c o  m*/
        html = IOUtils.toString(context.getAssets().open("android_feedback_helper.html"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (html.isEmpty())
        return null;

    // version name and code
    PackageManager packageManager = context.getPackageManager();
    String versionName = "Unknown";
    int versionCode = -1;
    if (packageManager != null) {
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss_zzz");
    String time = sdf.format(now);

    return html.replaceAll(Data.APP_PACKAGE, context.getPackageName())
            .replaceAll(Data.VERSION_NAME, versionName)
            .replaceAll(Data.VERSION_CODE, String.valueOf(versionCode)).replaceAll(Data.FEEDBACK_TIME, time)
            .replaceAll(Data.FEEDBACK_MESSAGE, msg).replaceAll(Data.USER_EMAIL, userEmail);
}

From source file:Main.java

public static Bitmap getRemoveBitmap(Context context, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from w  w w  . j  a v  a 2  s . co  m
    Bitmap bitmap1;
    try {
        Bitmap bitmap2 = BitmapFactory.decodeStream(context.getAssets().open("remove@2x.png"));
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap1);
        canvas1.drawARGB(0, 0, 0, 0);
        canvas1.drawBitmap(bitmap, bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, paint);
        bitmap.recycle();
        canvas1.drawBitmap(bitmap2, 0.0F, 0.0F, paint);
        bitmap2.recycle();
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
    }
    return bitmap1;
}

From source file:Main.java

static public void copyFileFromAssets(Context context, String file, String dest) throws Exception {
    InputStream in = null;//from w  w w. j a v a  2 s . c om
    OutputStream fout = null;
    int count = 0;

    try {
        in = context.getAssets().open(file);

        File hydappdir = new File("/data/data/" + context.getPackageName() + "/hydapp");
        hydappdir.mkdirs();

        fout = new FileOutputStream(new File(dest));

        byte data[] = new byte[1024];
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {/*from w ww. ja va2  s  . c  om*/
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

/**
 * The most useful answer about text drawing ever. http://stackoverflow.com/a/32081250
 *///from  w w  w. j a  va  2  s  .  co  m
@Nullable
public static Bitmap createTypefaceBitmap(final Context context, @NonNull final String text, final int color,
        final float textSizePx) {
    final Typeface robotoMedium = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf");

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(robotoMedium);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(color);
    paint.setTextSize(textSizePx);

    final Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);

    Bitmap bitmap = null;
    if (!bounds.isEmpty()) {
        final int width = bounds.width();
        final int height = bounds.height();

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

        final float x = bounds.left;
        final float y = height - bounds.bottom;

        final Canvas canvas = new Canvas(bitmap);
        canvas.drawText(text, x, y, paint);
    }

    return bitmap;
}

From source file:Main.java

public static String extractFromAssets(Context ctx, String file, String destinationDirectory)
        throws IOException, FileNotFoundException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    AssetManager assetManager = ctx.getAssets();
    InputStream in = assetManager.open(file);
    String destinationFilename = destinationDirectory + File.separator + file;
    OutputStream out = new FileOutputStream(destinationFilename);
    byte[] buffer = new byte[1024];
    int read;/*from  w  w  w.  j  ava 2 s . c  o m*/
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();
    out.close();
    return destinationFilename;
}

From source file:Main.java

/**
 * Load an asset file and return file's content
 * /*ww  w .  j  a  v  a 2  s .  c o  m*/
 * @param mContext Context
 * @param mFilename filename
 * @return file's content
 */
private static String loadFileText(final Context mContext, final String mFilename) {
    try {
        StringBuffer mFileData = new StringBuffer();
        final BufferedReader mReader = new BufferedReader(
                new InputStreamReader(mContext.getAssets().open(mFilename)));
        String line;
        while ((line = mReader.readLine()) != null) {
            mFileData.append(line);
        }
        return mFileData.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:com.github.hobbe.android.openkarotz.util.AssetUtils.java

/**
 * Load a JSON resource from the asset filename.
 * @param context the context//w ww . j  av a  2 s . c om
 * @param filename the name of the JSON object
 * @return the JSON object
 */
public static JSONObject loadJsonFromAsset(Context context, String filename) {
    JSONObject json = null;
    InputStream is = null;

    try {
        is = context.getAssets().open(filename);

        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);

        String content = new String(buffer, "UTF-8");
        json = new JSONObject(content);

    } catch (IOException e) {
        Log.e(LOG_TAG, "Could not load JSON asset " + filename, e);
        return null;
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Could not parse JSON from asset " + filename, e);
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // Ignored
            }
        }
    }

    return json;
}

From source file:Main.java

/**
 * Returns true if the given tile file exists as a local asset.
 *///from  w w  w  . j ava  2  s .  c om
public static boolean hasTileAsset(Context context, String filename) {

    //cache the list of available files
    if (mapTileAssets == null) {
        try {
            mapTileAssets = context.getAssets().list("maptiles");
        } catch (IOException e) {
            // no assets
            mapTileAssets = new String[0];
        }
    }

    // search for given filename
    for (String s : mapTileAssets) {
        if (s.equals(filename)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean prepareDex(Context context, File dexInternalStoragePath, String dexFile) {
    BufferedInputStream bis = null;
    OutputStream dexWriter = null;

    try {//from  ww w .j a  v  a2s. com
        bis = new BufferedInputStream(context.getAssets().open(dexFile));
        dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath));
        byte[] buf = new byte[BUF_SIZE];
        int len;
        while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
            dexWriter.write(buf, 0, len);
        }
        dexWriter.close();
        bis.close();
        return true;
    } catch (IOException e) {
        if (dexWriter != null) {
            try {
                dexWriter.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return false;
    }
}