michael@0: /* michael@0: * Copyright (C) 2013 Square, Inc. michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: package com.squareup.picasso; michael@0: michael@0: import android.content.ContentResolver; michael@0: import android.content.Context; 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 ContentStreamBitmapHunter extends BitmapHunter { michael@0: final Context context; michael@0: michael@0: ContentStreamBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache, michael@0: Stats stats, Action action) { michael@0: super(picasso, dispatcher, cache, stats, action); michael@0: this.context = context; michael@0: } michael@0: michael@0: @Override Bitmap decode(Request data) michael@0: throws IOException { michael@0: return decodeContentStream(data); michael@0: } michael@0: michael@0: @Override Picasso.LoadedFrom getLoadedFrom() { michael@0: return DISK; michael@0: } michael@0: michael@0: protected Bitmap decodeContentStream(Request data) throws IOException { michael@0: ContentResolver contentResolver = context.getContentResolver(); 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 = contentResolver.openInputStream(data.uri); 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 = contentResolver.openInputStream(data.uri); 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: }