michael@0: package com.squareup.picasso; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.AssetManager; michael@0: import android.graphics.Bitmap; michael@0: import android.graphics.BitmapFactory; michael@0: import java.io.IOException; michael@0: import java.io.InputStream; michael@0: michael@0: import static com.squareup.picasso.Picasso.LoadedFrom.DISK; michael@0: michael@0: class AssetBitmapHunter extends BitmapHunter { michael@0: private AssetManager assetManager; michael@0: michael@0: public AssetBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache, michael@0: Stats stats, Action action) { michael@0: super(picasso, dispatcher, cache, stats, action); michael@0: assetManager = context.getAssets(); michael@0: } michael@0: michael@0: @Override Bitmap decode(Request data) throws IOException { michael@0: String filePath = data.uri.toString().substring(ASSET_PREFIX_LENGTH); michael@0: return decodeAsset(filePath); michael@0: } michael@0: michael@0: @Override Picasso.LoadedFrom getLoadedFrom() { michael@0: return DISK; michael@0: } michael@0: michael@0: Bitmap decodeAsset(String filePath) throws IOException { michael@0: BitmapFactory.Options options = null; michael@0: if (data.hasSize()) { michael@0: options = new BitmapFactory.Options(); michael@0: options.inJustDecodeBounds = true; michael@0: InputStream is = null; michael@0: try { michael@0: is = assetManager.open(filePath); michael@0: BitmapFactory.decodeStream(is, null, options); michael@0: } finally { michael@0: Utils.closeQuietly(is); michael@0: } michael@0: calculateInSampleSize(data.targetWidth, data.targetHeight, options); michael@0: } michael@0: InputStream is = assetManager.open(filePath); michael@0: try { michael@0: return BitmapFactory.decodeStream(is, null, options); michael@0: } finally { michael@0: Utils.closeQuietly(is); michael@0: } michael@0: } michael@0: }