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:org.opensourcetlapp.tl.CustomImageGetter.java

public CustomImageGetter(View t, Context context) {
    this.context = context;
    assetManager = context.getAssets();
    this.container = t;
}

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

/**
 * ???Asset?.//from   w w w. ja va  2 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) {
        //AbLogUtil.d(AbFileUtil.class, "?"+e.getMessage());
    }
    return drawable;
}

From source file:com.goliathonline.android.kegbot.io.LocalExecutor.java

public void execute(Context context, String assetName, JsonHandler handler) throws HandlerException {
    try {//from www .  j  a  v  a  2s.c om
        final InputStream input = context.getAssets().open(assetName);
        final JSONObject parser = new JSONObject(ParserUtils.convertStreamToString(input));
        handler.parseAndApply(parser, mResolver);
    } catch (HandlerException e) {
        throw e;
    } catch (JSONException e) {
        throw new HandlerException("Problem parsing local asset: " + assetName, e);
    } catch (IOException e) {
        throw new HandlerException("Problem parsing local asset: " + assetName, e);
    }
}

From source file:com.jaspersoft.android.jaspermobile.webview.dashboard.bridge.AmberTwoDashboardExecutor.java

@Override
void doPreparation() {
    InputStream stream = null;/*ww w .  ja v a2s. c o m*/
    Context context = webView.getContext();

    try {
        stream = context.getAssets().open("dashboard.html");
        StringWriter writer = new StringWriter();
        IOUtils.copy(stream, writer, "UTF-8");

        VisualizeEndpoint endpoint = VisualizeEndpoint.forBaseUrl(server.getBaseUrl()).optimized().build();

        Map<String, String> data = new HashMap<String, String>();
        data.put("visualize_url", endpoint.createUri());
        Template tmpl = Mustache.compiler().compile(writer.toString());
        String html = tmpl.execute(data);

        webView.loadDataWithBaseURL(server.getBaseUrl(), html, "text/html", "utf-8", null);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
    }
}

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

/**
 * ??sset?.//w w w .j  a  v  a 2s. c o  m
 *
 * @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) {
        AbLogUtil.d(AbFileUtil.class, "?" + e.getMessage());
    }
    return drawable;
}

From source file:net.peterkuterna.android.apps.devoxxsched.io.LocalExecutor.java

public void execute(Context context, String assetName, XmlHandler handler) throws XmlHandlerException {
    try {/*  w  w  w .  j a va  2  s. co  m*/
        final InputStream input = context.getAssets().open(assetName);
        final XmlPullParser parser = ParserUtils.newPullParser(input);
        handler.setLocalSync(true);
        handler.parseAndApply(parser, mResolver);
    } catch (XmlHandlerException e) {
        throw e;
    } catch (XmlPullParserException e) {
        throw new XmlHandlerException("Problem parsing local asset: " + assetName, e);
    } catch (IOException e) {
        throw new XmlHandlerException("Problem parsing local asset: " + assetName, e);
    }
}

From source file:com.google.android.apps.muzei.util.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  w w  . ja v a 2 s.  c  o  m

    String scheme = uri.getScheme();
    InputStream in = null;
    if ("content".equals(scheme)) {
        try {
            in = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            throw new OpenUriException(false, e);
        } catch (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 = client.open(new URL(uri.toString()));
            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.indexOf(reqContentTypeSubstring) < 0) {
                    throw new IOException("HTTP content type '" + contentType + "' didn't match '"
                            + reqContentTypeSubstring + "'.");
                }
            }
            in = conn.getInputStream();

        } catch (MalformedURLException e) {
            throw new OpenUriException(false, e);

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

        }
    }

    return in;
}

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

/**
 * ???Asset?./*from  w  w w.  java  2s  .c  om*/
 *
 * @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:net.peterkuterna.android.apps.devoxxsched.io.LocalExecutor.java

public void execute(Context context, String assetName, JSONHandler handler) throws JSONHandlerException {
    try {//from   w  w w  .  jav  a2s .c om
        final InputStream input = context.getAssets().open(assetName);
        byte[] buffer = new byte[input.available()];
        while (input.read(buffer) != -1)
            ;
        String jsontext = new String(buffer);
        ArrayList<JSONArray> entries = Lists.newArrayList();
        entries.add(new JSONArray(jsontext));
        handler.setLocalSync(true);
        handler.parseAndApply(entries, mResolver);
    } catch (JSONHandlerException e) {
        throw e;
    } catch (JSONException e) {
        throw new JSONHandlerException("Problem parsing local asset: " + assetName, e);
    } catch (IOException e) {
        throw new JSONHandlerException("Problem parsing local asset: " + assetName, e);
    }
}

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

/**
 * ???Asset?./*from   ww w  . j  ava 2 s.c o  m*/
 *
 * @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) {
        AbLogUtil.d(AbFileUtil.class, "?" + e.getMessage());
    }
    return drawable;
}