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.adrianlesniak.gamerspot.extra.CustomTypefaceSpan.java

/**
 * Load the {@link android.graphics.Typeface} and apply to a {@link Spannable}.
 *//*from w w w .j  ava2 s. c o  m*/
public CustomTypefaceSpan(Context context, String typefaceName) {
    mTypeface = sTypefaceCache.get(typefaceName);

    if (mTypeface == null) {
        mTypeface = Typeface.createFromAsset(context.getAssets(), typefaceName);

        // Cache the loaded Typeface
        sTypefaceCache.put(typefaceName, mTypeface);
    }
}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

private static boolean copyDefaultMap(Context context) {
    try {//  w w w  .ja  v  a  2s. c  om
        // write file to sd for mapsforge
        OutputStream mapFileWriter = new BufferedOutputStream(
                context.openFileOutput(MapFragment.MAP_FILE_NAME, Context.MODE_PRIVATE));
        InputStream assetData = new BufferedInputStream(context.getAssets().open(MapFragment.MAP_FILE_NAME));

        byte[] buffer = new byte[1024];
        int len = assetData.read(buffer);
        while (len != -1) {
            mapFileWriter.write(buffer, 0, len);
            len = assetData.read(buffer);
        }
        mapFileWriter.close();
    } catch (FileNotFoundException e) {
        Log.e(TAG, "copyDefaultMap", e);
        return false;
    } catch (IOException e) {
        Log.e(TAG, "copyDefaultMap", e);
        return false;
    }
    return true;
}

From source file:com.smartapps.avro.PhoneticXmlLoader.java

public PhoneticXmlLoader(Context context) {
    // this.url =
    // Data.class.getResource("file:///android_asset/phonetic.xml");
    ///*ww  w.  j  av  a 2 s.co  m*/
    try {
        is = context.getAssets().open("phonetic.xml");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.micro.utils.F.java

/**
 * ???Asset?./*from  w w  w.j av a 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) {
        L.D("?" + e.getMessage());
    }
    return bit;
}

From source file:cn.org.eshow.framwork.util.AbFileUtil.java

/**
 * ?Assetssd?//ww  w.  j  a  v a  2s . co m
 * @param context
 * @param assetDir  "dir"
 * @param outDir    sd?
 */
public static void copyAssets2SD(Context context, String assetDir, String outDir) {
    String[] files;
    try {
        files = context.getAssets().list(assetDir);
        File outDirFile = new File(outDir);
        if (!outDirFile.exists()) {
            outDirFile.mkdirs();
        }

        for (int i = 0; i < files.length; i++) {
            String fileName = files[i];

            String[] filesChild = context.getAssets().list(fileName);
            if (filesChild != null && filesChild.length > 0) {
                copyAssets2SD(context, fileName, outDir + "/" + fileName);
            } else {
                InputStream in = null;
                if (!AbStrUtil.isEmpty(assetDir)) {
                    in = context.getAssets().open(assetDir + "/" + fileName);
                } else {
                    in = context.getAssets().open(fileName);
                }
                File outFile = new File(outDir + "/" + fileName);
                if (outFile.exists()) {
                    outFile.delete();
                }
                outFile.createNewFile();
                OutputStream out = new FileOutputStream(outFile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.micro.utils.F.java

/**
 * ???Asset?.//from w  ww.  j  a va2 s  .  c  om
 *
 * @param context the context
 * @param fileName the file name
 * @return Drawable 
 */
public static Drawable getDrawableFromAsset(Context context, String fileName) {
    Drawable drawable = null;
    try {
        AssetManager assetManager = context.getAssets();
        InputStream is = assetManager.open(fileName);
        drawable = Drawable.createFromStream(is, null);
    } catch (Exception e) {
        L.D("?" + e.getMessage());
    }
    return drawable;
}

From source file:com.xperia64.timidityae.Globals.java

public static int extract8Rock(Context c) {
    InputStream in = null;// w w  w  .ja va2  s .c  o m
    try {
        in = c.getAssets().open("8Rock11e.sfArk");
    } catch (IOException e) {
        e.printStackTrace();
    }
    String[] needLol = null;
    try {
        new FileOutputStream(Globals.dataFolder + "/soundfonts/8Rock11e.sfArk", true).close();
    } catch (FileNotFoundException e) {
        needLol = getDocFilePaths(c, Globals.dataFolder);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (needLol != null) {
        File f = new File(Globals.dataFolder + "/soundfonts/8Rock11e.sfArk");
        if (f != null)
            if (f.exists())
                tryToDeleteFile(c, Globals.dataFolder + "/soundfonts/8Rock11e.sfArk");
        OutputStream out = null;
        String probablyTheDirectory = needLol[0];
        String probablyTheRoot = needLol[1];
        String needRename;
        String value;
        String value2;
        if (probablyTheDirectory.length() > 1) {
            needRename = dataFolder.substring(dataFolder.indexOf(probablyTheRoot) + probablyTheRoot.length())
                    + "/soundfonts/8Rock11e.sf2";
            value = probablyTheDirectory + '/' + "8Rock11e.sfArk";
            value2 = probablyTheDirectory + '/' + "8Rock11e.sf2";
        } else {
            return -9;
        }
        try {
            out = new FileOutputStream(value);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (out == null)
            return -1;
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = in.read(buf)) > 0)
                out.write(buf, 0, len);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JNIHandler.decompressSFArk(value, "8Rock11e.sf2");
        renameDocumentFile(c, value2, needRename);
        tryToDeleteFile(c, value);
    } else {
        File f = new File(Globals.dataFolder + "/soundfonts/8Rock11e.sfArk");
        if (f != null)
            if (f.exists())
                f.delete();
        OutputStream out = null;
        try {
            out = new FileOutputStream(Globals.dataFolder + "/soundfonts/8Rock11e.sfArk");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (out == null)
            return -1;
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = in.read(buf)) > 0)
                out.write(buf, 0, len);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JNIHandler.decompressSFArk(Globals.dataFolder + "/soundfonts/8Rock11e.sfArk", "8Rock11e.sf2");
        //System.out.println("decompresed sfark");
        new File(Globals.dataFolder + "/soundfonts/8Rock11e.sfArk").delete();
    }

    return 777;

}

From source file:fr.mixit.android.io.JsonExecutor.java

public void executeAndInsert(Context context, String assetName, JsonHandlerApply handler)
        throws JsonHandlerException {
    try {//  w w w  .  j ava  2s .  c  o  m
        final InputStream input = context.getAssets().open(assetName);
        final byte[] buffer = new byte[input.available()];
        while (input.read(buffer) != -1) {
            ;
        }
        final String jsontext = new String(buffer);
        executeAndInsert(jsontext, handler);
    } catch (final JsonHandlerException e) {
        throw e;
    } catch (final IOException e) {
        throw new JsonHandlerException("Problem parsing local asset: " + assetName, e);
    }
}

From source file:org.goodev.droidddle.utils.IOUtil.java

public static InputStream openUri(Context context, Uri uri, String reqContentTypeSubstring)
        throws OpenUriException {

    if (uri == null) {
        throw new IllegalArgumentException("Uri cannot be empty");
    }/*from w  ww . j  av a  2  s  .c om*/

    String scheme = uri.getScheme();
    if (scheme == null) {
        throw new OpenUriException(false, new IOException("Uri had no scheme"));
    }

    InputStream in = null;
    if ("content".equals(scheme)) {
        try {
            in = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException | SecurityException e) {
            throw new OpenUriException(false, e);
        }

    } else if ("file".equals(scheme)) {
        List<String> segments = uri.getPathSegments();
        if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) {
            AssetManager assetManager = context.getAssets();
            StringBuilder assetPath = new StringBuilder();
            for (int i = 1; i < segments.size(); i++) {
                if (i > 1) {
                    assetPath.append("/");
                }
                assetPath.append(segments.get(i));
            }
            try {
                in = assetManager.open(assetPath.toString());
            } catch (IOException e) {
                throw new OpenUriException(false, e);
            }
        } else {
            try {
                in = new FileInputStream(new File(uri.getPath()));
            } catch (FileNotFoundException e) {
                throw new OpenUriException(false, e);
            }
        }

    } else if ("http".equals(scheme) || "https".equals(scheme)) {
        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = null;
        int responseCode = 0;
        String responseMessage = null;
        try {
            conn = new OkUrlFactory(client).open(new URL(uri.toString()));
        } catch (MalformedURLException e) {
            throw new OpenUriException(false, e);
        }

        try {
            conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
            conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
            responseCode = conn.getResponseCode();
            responseMessage = conn.getResponseMessage();
            if (!(responseCode >= 200 && responseCode < 300)) {
                throw new IOException("HTTP error response.");
            }
            if (reqContentTypeSubstring != null) {
                String contentType = conn.getContentType();
                if (contentType != null && !contentType.contains(reqContentTypeSubstring)) {
                    throw new IOException("HTTP content type '" + contentType + "' didn't match '"
                            + reqContentTypeSubstring + "'.");
                }
            }
            in = conn.getInputStream();

        } catch (IOException e) {
            if (responseCode > 0) {
                throw new OpenUriException(500 <= responseCode && responseCode < 600, responseMessage, e);
            } else {
                throw new OpenUriException(false, e);
            }

        }
    }

    return in;
}

From source file:fr.mixit.android.io.JsonExecutor.java

public <T> T executeAndGet(Context context, String assetName, JsonHandlerGet<T> handler)
        throws JsonHandlerException {
    try {//  w w  w .jav  a  2 s .c o m
        final InputStream input = context.getAssets().open(assetName);
        final byte[] buffer = new byte[input.available()];
        while (input.read(buffer) != -1) {
            ;
        }
        final String jsontext = new String(buffer);
        return executeAndGet(jsontext, handler);
    } catch (final JsonHandlerException e) {
        throw e;
    } catch (final IOException e) {
        throw new JsonHandlerException("Problem parsing local asset: " + assetName, e);
    }
}