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.nuvolect.deepdive.util.OmniUtil.java

/**
 * Copy a file from assets to an omni destination folder.
 * @param ctx//from  w  ww  .  j a  va  2 s.  c  o m
 * @param assetFilePath
 * @param destinationFile
 * @return number of bytes copied
 * @throws IOException
 */
public static int copyAsset(Context ctx, String assetFilePath, OmniFile destinationFile) throws IOException {

    InputStream inputStream = ctx.getAssets().open(assetFilePath);
    OutputStream outputStream = destinationFile.getOutputStream();
    int numBytes = IOUtils.copy(inputStream, outputStream);
    inputStream.close();
    outputStream.close();

    return numBytes;
}

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

/**
 * Copy the specified resource file from the assets folder into the "files dir" of this application, so that this resource
 * can be opened by the Voip Lib by providing it the absolute path of the copied resource
 * @param ctx The application context//  w  w  w . j  a v  a  2s .co  m
 * @param assetPath The path of the resource (e.g on_hold.wav or sounds/on_hold.wav)
 * @return the absolute path of the copied resource, or null if no file was copied.
 */
public static String getResourcePathByAssetCopy(Context ctx, String assetSubFolder, String fileToCopy) {
    Log.d(TAG, "getResourcePathByAssetCopy on folder *" + assetSubFolder + "* for file:" + fileToCopy);
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;
    String filename = null;

    try {
        files = assetManager.list(assetSubFolder);
        Log.d(TAG, "Found " + files.length + " files");
        if (files.length > 0) {
            for (String f : files) {
                Log.d(TAG, "Found resource:" + f);
                if (f == null)
                    continue;
                if (f.equals(fileToCopy)) {

                    filename = f;
                    break;
                }
            }

            Log.d(TAG, "Found:" + filename);
            if (filename == null)
                return null;

            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;
                return outFile.getAbsolutePath();
            } catch (IOException e) {
                Log.e(TAG, "Failed to copy asset file: " + filename, e);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, "Failed to get asset file list.", e);
    }
    return null;
}

From source file:Main.java

private static Bitmap loadAssetBitmap(Context context, String assetPath) {
    InputStream is = null;/*from  w  ww . j a v  a  2s.c om*/
    try {
        Resources resources = context.getResources();
        Options options = new Options();
        options.inDensity = DisplayMetrics.DENSITY_HIGH;
        options.inScreenDensity = resources.getDisplayMetrics().densityDpi;
        options.inTargetDensity = resources.getDisplayMetrics().densityDpi;
        is = context.getAssets().open(assetPath);
        Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options);
        if (bitmap != null) {
            drawableCache.put(assetPath, bitmap);
        }
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:fr.kwiatkowski.ApkTrack.UpdateSource.java

/**
 * Reads the update sources from the JSON asset file.
 * @param ctx The context of the application.
 * @return A list of available update sources.
 *//*from w  w w.  j  a v  a2 s.c om*/
public static ArrayList<UpdateSource> getUpdateSources(Context ctx) {
    if (_SOURCES != null) {
        return _SOURCES;
    }

    _SOURCES = new ArrayList<UpdateSource>();
    Log.v(MainActivity.TAG, "Reading update sources...");
    try {
        InputStream is = ctx.getAssets().open("sources.json");

        StringBuilder buffer = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String s;
        while ((s = br.readLine()) != null) {
            buffer.append(s);
        }

        JSONArray sources = new JSONArray(buffer.toString());
        for (int i = 0; i < sources.length(); ++i) {
            String name = sources.getJSONObject(i).getString("name");
            Log.v(MainActivity.TAG, "Reading " + name);
            String version_check_url = sources.getJSONObject(i).getString("version_check_url");
            String version_check_regexp = sources.getJSONObject(i).getString("version_check_regexp");
            String download_url = sources.getJSONObject(i).optString("download_url", null);
            String applicable_packages = sources.getJSONObject(i).optString("applicable_packages", ".*");
            _SOURCES.add(new UpdateSource(name, version_check_url, version_check_regexp, download_url,
                    applicable_packages));
        }
    } catch (IOException e) {
        Log.v(MainActivity.TAG, "Could not open sources.json!", e);
    } catch (JSONException e) {
        Log.v(MainActivity.TAG, "sources.json seems to be malformed!", e);
    }

    return _SOURCES;
}

From source file:com.example.tom.tryveg.carousel.AppUtils.java

/**
 * copies files on the native filesystem, optional unzip
 * //from w  w w . j av  a  2s.  c o  m
 */
public static void AssetFileCopy(Context context, String PathDest, String assetName, boolean gunzip) {
    try {

        File fdOut = new File(PathDest);
        fdOut.createNewFile();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fdOut));
        InputStream in = null;
        if (gunzip)
            in = new GZIPInputStream(context.getAssets().open(assetName));
        else
            in = new BufferedInputStream(context.getAssets().open(assetName));
        //copy file content
        int length;
        byte buffer[] = new byte[4096];
        while ((length = in.read(buffer, 0, 4096)) != -1) {
            out.write(buffer, 0, length);
        }
        out.flush();
        out.close();
        in.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static boolean installDll(Context context) {
    boolean isSuccess = false;
    File file = context.getFileStreamPath("Disdll.dll");
    boolean isDeleteSuccess = true;
    if (file.exists()) {
        isDeleteSuccess = file.delete();
    }/*from   w  w  w . j a v a  2s.co  m*/
    if (isDeleteSuccess) {
        try {
            FileOutputStream outputStream = context.openFileOutput("Disdll.dll", Context.MODE_PRIVATE);
            InputStream inputStream = context.getAssets().open("Disdll.dll");
            byte[] temp = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(temp)) != -1) {
                outputStream.write(temp, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        isSuccess = checkIfDllInstalled(context);
    }
    return isSuccess;
}

From source file:com.spoiledmilk.ibikecph.util.Util.java

public static String stringFromJsonAssets(Context context, String path) {
    String bufferString = "";
    try {// w w w.j a v a 2 s.com
        InputStream is = context.getAssets().open(path);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        bufferString = new String(buffer);
    } catch (Exception e) {
        LOG.e(e.getLocalizedMessage());
    }
    return bufferString;
}

From source file:crow.util.Util.java

/**
 * Assists ?//from ww w  . java2  s . co m
 */
public static String getAssistFileAsString(Context context, String assistFile) throws IOException {
    String result = null;
    AssetManager assetManager = context.getAssets();
    result = inputStreamToString(assetManager.open(assistFile));
    return result;
}

From source file:com.mobicage.rogerthat.util.TextUtils.java

public static void overrideFonts(final Context context, final View v) {
    try {//from   w  w w.j av a2 s .  co  m
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof EditText) {
            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/lato_light.ttf"));
            ((EditText) v).setTextColor(ContextCompat.getColor(context, R.color.mc_words_color));
        } else if (v instanceof Button) {
            ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/lato_bold.ttf"));
            ((Button) v).setTextColor(ContextCompat.getColor(context, android.R.color.white));
        } else if (v instanceof TextView) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/lato_light.ttf"));
            ((TextView) v).setTextColor(ContextCompat.getColor(context, R.color.mc_words_color));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.sythealth.fitness.util.Utils.java

public static void setTypeface1(Context context, TextView tv) {
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/fzcqjw.ttf");
    tv.setTypeface(typeface);//from   ww w .  j  av a 2  s . com
}