Example usage for android.graphics.drawable Drawable createFromResourceStream

List of usage examples for android.graphics.drawable Drawable createFromResourceStream

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable createFromResourceStream.

Prototype

@Nullable
public static Drawable createFromResourceStream(@Nullable Resources res, @Nullable TypedValue value,
        @Nullable InputStream is, @Nullable String srcName, @Nullable BitmapFactory.Options opts) 

Source Link

Document

Create a drawable from an inputstream, using the given resources and value to determine density information.

Usage

From source file:android.content.res.VectorResources.java

@Override
Drawable loadDrawable(TypedValue value, int id) throws NotFoundException {
    boolean isColorDrawable = false;
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        isColorDrawable = true;//from  w ww.jav a  2s . c  o  m
    }
    final long key = isColorDrawable ? value.data : (((long) value.assetCookie) << 32) | value.data;

    Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);

    if (dr != null) {
        return dr;
    }

    if (isColorDrawable) {
        dr = new ColorDrawable(value.data);
    }

    if (dr == null) {
        if (value.string == null) {
            throw new NotFoundException("Resource is not a Drawable (color or path): " + value);
        }

        String file = value.string.toString();

        if (file.endsWith(".xml")) {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                XmlResourceParser rp = getXml(id);
                // XmlResourceParser rp = loadXmlResourceParser(
                //         file, id, value.assetCookie, "drawable");
                dr = createDrawableFromXml(rp);
                rp.close();
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);

        } else {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                InputStream is = openRawResource(id, value);
                //InputStream is = mAssets.openNonAsset(
                //        value.assetCookie, file, AssetManager.ACCESS_STREAMING);
                //                System.out.println("Opened file " + file + ": " + is);
                dr = Drawable.createFromResourceStream(this, value, is, file, null);
                is.close();
                //                System.out.println("Created stream: " + dr);
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        }
    }
    Drawable.ConstantState cs;
    if (dr != null) {
        dr.setChangingConfigurations(value.changingConfigurations);
        cs = dr.getConstantState();
        if (cs != null) {
            synchronized (mAccessLock) {
                //Log.i(TAG, "Saving cached drawable @ #" +
                //        Integer.toHexString(key.intValue())
                //        + " in " + this + ": " + cs);
                if (isColorDrawable) {
                    mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                } else {
                    mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                }
            }
        }
    }

    return dr;
}