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 void setupDatabase(Context context, String db_name) {
    ContextWrapper cw = new ContextWrapper(context);
    String db_path = cw.getDatabasePath(db_name).getPath();

    try {/*from  w ww.j  a v  a2  s.  c  o m*/
        // Setup
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = context.getAssets().open(db_name);
        OutputStream myOutput = new FileOutputStream(db_path);

        // Write all the things.
        while ((length = myInput.read(buffer)) > 0)
            myOutput.write(buffer, 0, length);

        // Cleanup
        myOutput.close();
        myOutput.flush();
        myInput.close();
    } catch (IOException e) {
        // You done goofed.
        e.printStackTrace();
    }
}

From source file:com.tassadar.multirommgr.Device.java

public static Device load(String name) {
    Context ctx = MgrApp.getAppContext();
    StringBuilder b = new StringBuilder();

    name = name.toLowerCase();// www .  jav  a 2s  .c o  m

    try {
        InputStream in = ctx.getAssets().open("devices.json");
        byte[] buff = new byte[1024];
        for (int len; (len = in.read(buff)) != -1;)
            b.append(new String(buff, 0, len));
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    try {
        JSONObject o = (JSONObject) new JSONTokener(b.toString()).nextValue();
        JSONArray devices = o.getJSONArray("devices");
        JSONArray names;
        for (int i = 0; i < devices.length(); ++i) {
            o = devices.getJSONObject(i);

            names = o.getJSONArray("names");
            for (int x = 0; x < names.length(); ++x)
                if (name.equals(names.getString(x).toLowerCase()))
                    return new Device(name, names.getString(0), o);
        }
        return null;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Creates Roboto typeface and puts it into cache
 *//*from  ww  w .  jav  a  2 s  .  co m*/
private static Typeface getRobotoTypeface(Context context, String fontType, Typeface typeface) {
    String fontPath = "fonts/" + getFontName(fontType, typeface) + ".ttf";

    if (!typefaceCache.containsKey(fontType)) {
        typefaceCache.put(fontType, Typeface.createFromAsset(context.getAssets(), fontPath));
    }

    return typefaceCache.get(fontType);
}

From source file:com.entertailion.android.launcher.spotlight.ProcessSpotlight.java

/**
 * Get the spotlight data from the asset file
 * //  w w w . ja va  2  s.co m
 * @param context
 * @return
 */
public static String getAssetFeed(Context context) {
    String result = null;
    try {
        StringBuffer buffer = new StringBuffer();
        InputStream is = context.getAssets().open("spotlight_sites.js");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        is.close();
        result = buffer.toString();
    } catch (Throwable x) {
    }
    return result;
}

From source file:Main.java

public static boolean extractTo(Context context, final String name, final String dir, final String dstName,
        byte endec[]) {
    File file = new File(dir + "/" + dstName);

    InputStream is = null;/*from  w w  w  . jav a  2 s . c o m*/
    OutputStream os = null;
    try {
        is = context.getAssets().open(name);
        os = new FileOutputStream(file);
        byte bs[] = new byte[4096];
        int rc = 0;
        if (endec != null && endec.length > 0) {
            int idx = 0;
            while ((rc = is.read(bs)) >= 0) {
                if (rc > 0) {
                    for (int i = 0; i < rc; i++) {
                        bs[i] ^= endec[idx++];
                        if (idx >= endec.length) {
                            idx = 0;
                        }
                    }
                    os.write(bs, 0, rc);
                }
            }
        } else {
            while ((rc = is.read(bs)) >= 0) {
                if (rc > 0) {
                    os.write(bs, 0, rc);
                }
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:com.mobandme.ada.examples.q.model.helpers.ImportLoaderHelper.java

/**
 * This method read a categories.json file from application Assets folder and return a list of Categories.
 * @param pContext//from  w w w.j a v  a 2s  .  c o  m
 * @return List with filled Categories.
 */
public static List<Category> getCategoriesList(Context pContext) {
    List<Category> returnedValue = new ArrayList<Category>();

    try {
        String data = null;

        if (pContext != null) {

            InputStream input = pContext.getAssets().open(CATEGORIES_ASSET_NAME);
            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 Category(dataParser.getString(index)));
                    }
                }
            }
        }

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

    return returnedValue;
}

From source file:com.dm.material.dashboard.candybar.helpers.ReportBugsHelper.java

@Nullable
private static String buildBrokenAppFilter(Context context, File folder) {
    try {/*from w w w  .  j  ava  2  s .  c  o m*/
        AssetManager asset = context.getAssets();
        InputStream stream = asset.open("appfilter.xml");
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(stream);
        NodeList list = doc.getElementsByTagName("item");

        File fileDir = new File(folder.toString() + "/" + "broken_appfilter.xml");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir), "UTF8"));

        boolean first = true;
        for (int i = 0; i < list.getLength(); i++) {
            Node nNode = list.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                if (first) {
                    first = false;
                    out.append("<!-- BROKEN APPFILTER -->");
                    out.append("\n\n\n");
                }

                int drawable = context.getResources().getIdentifier(eElement.getAttribute("drawable"),
                        "drawable", context.getPackageName());
                if (drawable == 0) {
                    out.append("Activity : ").append(
                            eElement.getAttribute("component").replace("ComponentInfo{", "").replace("}", ""));
                    out.append("\n");
                    out.append("Drawable : ").append(eElement.getAttribute("drawable"));
                    out.append("\n");
                    out.append("Reason : Drawable Not Found!");
                    out.append("\n\n");
                }
            }
        }
        out.flush();
        out.close();

        return fileDir.toString();
    } catch (Exception | OutOfMemoryError e) {
        Log.d(Tag.LOG_TAG, Log.getStackTraceString(e));
    }
    return null;
}

From source file:com.adrguides.utils.HTTPUtils.java

public static InputStream openAddress(Context context, URL url) throws IOException {
    if ("file".equals(url.getProtocol()) && url.getPath().startsWith("/android_asset/")) {
        return context.getAssets().open(url.getPath().substring(15)); // "/android_asset/".length() == 15
    } else {/*  w w w .j a  va 2  s .c  o m*/
        URLConnection urlconn = url.openConnection();
        urlconn.setReadTimeout(10000 /* milliseconds */);
        urlconn.setConnectTimeout(15000 /* milliseconds */);
        urlconn.setAllowUserInteraction(false);
        urlconn.setDoInput(true);
        urlconn.setDoOutput(false);
        if (urlconn instanceof HttpURLConnection) {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            int responsecode = connection.getResponseCode();
            if (responsecode != HttpURLConnection.HTTP_OK) {
                throw new IOException("Http response code returned:" + responsecode);
            }
        }
        return urlconn.getInputStream();
    }
}

From source file:Main.java

/**
 * Copy the file from the assets to the map tiles directory if it was
 * shipped with the APK.// w w w. ja  v a2 s .  com
 */
public static boolean copyTileAsset(Context context, String filename) {
    if (!hasTileAsset(context, filename)) {
        // file does not exist as asset
        return false;
    }

    // copy file from asset to internal storage
    try {
        InputStream is = context.getAssets().open(TILE_PATH + File.separator + filename);
        File f = getTileFile(context, filename);
        FileOutputStream os = new FileOutputStream(f);

        byte[] buffer = new byte[1024];
        int dataSize;
        while ((dataSize = is.read(buffer)) > 0) {
            os.write(buffer, 0, dataSize);
        }
        os.close();
    } catch (IOException e) {
        return false;
    }

    return true;
}

From source file:Main.java

public static void assetsDataToSD(String fileOutPutName, String fileInPutName, Context context)
        throws IOException {
    InputStream myInput;/*  w ww  .j  a  v  a  2 s.co m*/
    File file = new File(fileOutPutName);
    /*if (!file.exists()) {
    file.createNewFile();
       }else {
       return;
    }*/
    OutputStream myOutput = new FileOutputStream(fileOutPutName);
    myInput = context.getAssets().open(fileInPutName);
    byte[] buffer = new byte[1024];
    int length = myInput.read(buffer);
    while (length > 0) {
        myOutput.write(buffer, 0, length);
        length = myInput.read(buffer);
    }

    myOutput.flush();
    myInput.close();
    myOutput.close();
}