Search in sources :

Example 1 with AssetInputStream

use of android.content.res.AssetManager.AssetInputStream in project robolectric by robolectric.

the class ShadowBitmapFactory method decodeStream.

@Implementation
protected static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) {
    byte[] ninePatchChunk = null;
    if (is instanceof AssetInputStream) {
        ShadowAssetInputStream sais = Shadow.extract(is);
        if (sais.isNinePatch()) {
            ninePatchChunk = new byte[0];
        }
        if (sais.getDelegate() != null) {
            is = sais.getDelegate();
        }
    }
    try {
        if (is != null) {
            is.reset();
        }
    } catch (IOException e) {
    // ignore
    }
    boolean isNamedStream = is instanceof NamedStream;
    String name = isNamedStream ? is.toString().replace("stream for ", "") : null;
    RobolectricBufferedImage image = isNamedStream ? null : getImageFromStream(is);
    if (!allowInvalidImageData && image == null) {
        if (opts != null) {
            opts.outWidth = -1;
            opts.outHeight = -1;
        }
        return null;
    }
    Bitmap bitmap = create(name, outPadding, opts, null, image);
    ReflectionHelpers.callInstanceMethod(bitmap, "setNinePatchChunk", ClassParameter.from(byte[].class, ninePatchChunk));
    ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
    shadowBitmap.createdFromStream = is;
    if (image != null && opts != null) {
        opts.outMimeType = image.getMimeType();
    }
    return bitmap;
}
Also used : AssetInputStream(android.content.res.AssetManager.AssetInputStream) Bitmap(android.graphics.Bitmap) RobolectricBufferedImage(org.robolectric.shadows.ImageUtil.RobolectricBufferedImage) IOException(java.io.IOException) NamedStream(org.robolectric.util.NamedStream) Implementation(org.robolectric.annotation.Implementation)

Example 2 with AssetInputStream

use of android.content.res.AssetManager.AssetInputStream in project robolectric by robolectric.

the class ShadowAssetInputStream method createAssetInputStream.

static AssetInputStream createAssetInputStream(InputStream delegateInputStream, long assetPtr, AssetManager assetManager) {
    Asset asset = NATIVE_ASSET_REGISTRY.getNativeObject(assetPtr);
    AssetInputStream ais = ReflectionHelpers.callConstructor(AssetInputStream.class, from(AssetManager.class, assetManager), from(long.class, assetPtr));
    ShadowAssetInputStream sais = Shadow.extract(ais);
    if (sais instanceof ShadowLegacyAssetInputStream) {
        ShadowLegacyAssetInputStream slais = (ShadowLegacyAssetInputStream) sais;
        slais.setDelegate(delegateInputStream);
        slais.setNinePatch(asset.isNinePatch());
    }
    return ais;
}
Also used : AssetInputStream(android.content.res.AssetManager.AssetInputStream) AssetManager(android.content.res.AssetManager) Asset(org.robolectric.res.android.Asset)

Example 3 with AssetInputStream

use of android.content.res.AssetManager.AssetInputStream in project robolectric by robolectric.

the class ShadowImageDecoder method ImageDecoder_nCreateAsset.

protected static ImageDecoder ImageDecoder_nCreateAsset(long asset_ptr, Source source) throws DecodeException {
    // Asset* asset = reinterpret_cast<Asset*>(assetPtr);
    // SkStream stream = new AssetStreamAdaptor(asset);
    // return jniCreateDecoder(stream, source);
    Resources resources = ReflectionHelpers.getField(source, "mResources");
    AssetInputStream assetInputStream = ShadowAssetInputStream.createAssetInputStream(null, asset_ptr, resources.getAssets());
    return jniCreateDecoder(new ImgStream() {

        @Override
        protected InputStream getInputStream() {
            return assetInputStream;
        }
    });
}
Also used : AssetInputStream(android.content.res.AssetManager.AssetInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AssetInputStream(android.content.res.AssetManager.AssetInputStream) InputStream(java.io.InputStream) Resources(android.content.res.Resources)

Aggregations

AssetInputStream (android.content.res.AssetManager.AssetInputStream)3 AssetManager (android.content.res.AssetManager)1 Resources (android.content.res.Resources)1 Bitmap (android.graphics.Bitmap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Implementation (org.robolectric.annotation.Implementation)1 Asset (org.robolectric.res.android.Asset)1 RobolectricBufferedImage (org.robolectric.shadows.ImageUtil.RobolectricBufferedImage)1 NamedStream (org.robolectric.util.NamedStream)1