|
1 /* |
|
2 * Copyright (C) 2013 Square, Inc. |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 package com.squareup.picasso; |
|
17 |
|
18 import android.content.ContentResolver; |
|
19 import android.content.Context; |
|
20 import android.graphics.Bitmap; |
|
21 import android.graphics.BitmapFactory; |
|
22 import java.io.IOException; |
|
23 import java.io.InputStream; |
|
24 |
|
25 import static com.squareup.picasso.Picasso.LoadedFrom.DISK; |
|
26 |
|
27 class ContentStreamBitmapHunter extends BitmapHunter { |
|
28 final Context context; |
|
29 |
|
30 ContentStreamBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache, |
|
31 Stats stats, Action action) { |
|
32 super(picasso, dispatcher, cache, stats, action); |
|
33 this.context = context; |
|
34 } |
|
35 |
|
36 @Override Bitmap decode(Request data) |
|
37 throws IOException { |
|
38 return decodeContentStream(data); |
|
39 } |
|
40 |
|
41 @Override Picasso.LoadedFrom getLoadedFrom() { |
|
42 return DISK; |
|
43 } |
|
44 |
|
45 protected Bitmap decodeContentStream(Request data) throws IOException { |
|
46 ContentResolver contentResolver = context.getContentResolver(); |
|
47 BitmapFactory.Options options = null; |
|
48 if (data.hasSize()) { |
|
49 options = new BitmapFactory.Options(); |
|
50 options.inJustDecodeBounds = true; |
|
51 InputStream is = null; |
|
52 try { |
|
53 is = contentResolver.openInputStream(data.uri); |
|
54 BitmapFactory.decodeStream(is, null, options); |
|
55 } finally { |
|
56 Utils.closeQuietly(is); |
|
57 } |
|
58 calculateInSampleSize(data.targetWidth, data.targetHeight, options); |
|
59 } |
|
60 InputStream is = contentResolver.openInputStream(data.uri); |
|
61 try { |
|
62 return BitmapFactory.decodeStream(is, null, options); |
|
63 } finally { |
|
64 Utils.closeQuietly(is); |
|
65 } |
|
66 } |
|
67 } |