blob: 88f67f668e9350187525ae45ebdec04e03380058 [file] [log] [blame]
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +08001/*
2 * Copyright (C) 2009 The Android Open Source Project
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
17package com.android.camera.gallery;
18
19import com.android.camera.BitmapManager;
20import com.android.camera.Util;
21
22import android.content.ContentResolver;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080025import android.net.Uri;
26import android.os.ParcelFileDescriptor;
27import android.util.Log;
28
29import java.io.File;
30import java.io.FileNotFoundException;
31import java.io.InputStream;
32
33class UriImage implements IImage {
34 private static final String TAG = "UriImage";
Owen Lind30b5872009-04-16 18:42:03 +080035 private final Uri mUri;
36 private final BaseImageList mContainer;
37 private final ContentResolver mContentResolver;
Chih-Chung Chang0a475e12009-04-16 11:42:12 +080038
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080039 UriImage(BaseImageList container, ContentResolver cr, Uri uri) {
40 mContainer = container;
41 mContentResolver = cr;
42 mUri = uri;
43 }
44
45 public String getDataPath() {
46 return mUri.getPath();
47 }
48
Chih-Chung Chang0a475e12009-04-16 11:42:12 +080049 private InputStream getInputStream() {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080050 try {
51 if (mUri.getScheme().equals("file")) {
52 String path = mUri.getPath();
53 return new java.io.FileInputStream(mUri.getPath());
54 } else {
55 return mContentResolver.openInputStream(mUri);
56 }
57 } catch (FileNotFoundException ex) {
58 return null;
59 }
60 }
61
Chih-Chung Chang0a475e12009-04-16 11:42:12 +080062 private ParcelFileDescriptor getPFD() {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080063 try {
64 if (mUri.getScheme().equals("file")) {
65 String path = mUri.getPath();
66 return ParcelFileDescriptor.open(new File(path),
67 ParcelFileDescriptor.MODE_READ_ONLY);
68 } else {
69 return mContentResolver.openFileDescriptor(mUri, "r");
70 }
71 } catch (FileNotFoundException ex) {
72 return null;
73 }
74 }
75
76 public Bitmap fullSizeBitmap(int targetWidthHeight) {
Chih-Chung Chang4250e212009-07-24 10:58:40 +080077 return fullSizeBitmap(targetWidthHeight, IImage.ROTATE_AS_NEEDED,
78 IImage.NO_NATIVE);
79 }
80
81 public Bitmap fullSizeBitmap(int targetWidthHeight, boolean rotateAsNeeded,
82 boolean useNative) {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080083 try {
84 ParcelFileDescriptor pfdInput = getPFD();
Chih-Chung Chang4250e212009-07-24 10:58:40 +080085 Bitmap b = Util.makeBitmap(targetWidthHeight, pfdInput,
86 useNative);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080087 return b;
88 } catch (Exception ex) {
Chih-Chung Chang4250e212009-07-24 10:58:40 +080089 Log.e(TAG, "got exception decoding bitmap ", ex);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080090 return null;
91 }
92 }
93
94 final class LoadBitmapCancelable extends BaseCancelable<Bitmap> {
95 ParcelFileDescriptor mPfdInput;
Wei-Ta Chend5d74642009-06-11 16:13:13 +080096 BitmapFactory.Options mOptions;
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080097 int mTargetWidthOrHeight;
98
Wei-Ta Chend5d74642009-06-11 16:13:13 +080099 public LoadBitmapCancelable(ParcelFileDescriptor pfd,
100 int targetWidthOrHeight, BitmapFactory.Options options) {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800101 mPfdInput = pfd;
102 mTargetWidthOrHeight = targetWidthOrHeight;
Wei-Ta Chend5d74642009-06-11 16:13:13 +0800103 if (options != null) {
104 mOptions = options;
105 } else {
106 mOptions = new BitmapFactory.Options();
107 }
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800108 }
109
110 @Override
Owen Lin41a85782009-04-20 15:59:57 +0800111 public boolean requestCancel() {
112 if (super.requestCancel()) {
113 mOptions.requestCancelDecode();
114 return true;
115 }
116 return false;
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800117 }
118
Owen Lin41a85782009-04-20 15:59:57 +0800119 @Override
120 protected Bitmap execute() {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800121 try {
Chih-Chung Changc1c20e92009-04-15 18:06:14 +0800122 Bitmap b = Util.makeBitmap(mTargetWidthOrHeight,
123 fullSizeImageUri(), mContentResolver, mPfdInput,
124 mOptions);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800125 return b;
126 } catch (Exception ex) {
127 return null;
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800128 }
129 }
130 }
131
Owen Lin41a85782009-04-20 15:59:57 +0800132 public Cancelable<Bitmap> fullSizeBitmapCancelable(
Wei-Ta Chend5d74642009-06-11 16:13:13 +0800133 int targetWidthOrHeight, BitmapFactory.Options options) {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800134 try {
135 ParcelFileDescriptor pfdInput = getPFD();
136 if (pfdInput == null) return null;
Wei-Ta Chend5d74642009-06-11 16:13:13 +0800137 return new LoadBitmapCancelable(pfdInput,
138 targetWidthOrHeight, options);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800139 } catch (UnsupportedOperationException ex) {
140 return null;
141 }
142 }
143
144 public Uri fullSizeImageUri() {
145 return mUri;
146 }
147
148 public InputStream fullSizeImageData() {
149 return getInputStream();
150 }
151
152 public Bitmap miniThumbBitmap() {
153 return thumbBitmap();
154 }
155
156 public String getTitle() {
157 return mUri.toString();
158 }
159
160 public String getDisplayName() {
161 return getTitle();
162 }
163
164 public Bitmap thumbBitmap() {
Chih-Chung Chang0a475e12009-04-16 11:42:12 +0800165 return fullSizeBitmap(THUMBNAIL_TARGET_SIZE);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800166 }
167
168 private BitmapFactory.Options snifBitmapOptions() {
169 ParcelFileDescriptor input = getPFD();
170 if (input == null) return null;
171 try {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800172 BitmapFactory.Options options = new BitmapFactory.Options();
173 options.inJustDecodeBounds = true;
174 BitmapManager.instance().decodeFileDescriptor(
Owen Lind30b5872009-04-16 18:42:03 +0800175 input.getFileDescriptor(), options);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800176 return options;
177 } finally {
Chih-Chung Chang724e9b32009-05-19 14:35:15 +0800178 Util.closeSilently(input);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800179 }
180 }
181
182 public String getMimeType() {
183 BitmapFactory.Options options = snifBitmapOptions();
184 return (options != null) ? options.outMimeType : "";
185 }
186
187 public int getHeight() {
188 BitmapFactory.Options options = snifBitmapOptions();
189 return (options != null) ? options.outHeight : 0;
190 }
191
192 public int getWidth() {
193 BitmapFactory.Options options = snifBitmapOptions();
194 return (options != null) ? options.outWidth : 0;
195 }
196
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800197 public long fullSizeImageId() {
198 return 0;
199 }
200
201 public IImageList getContainer() {
202 return mContainer;
203 }
204
205 public long getDateTaken() {
206 return 0;
207 }
208
209 public int getRow() {
Chih-Chung Changf32ca412009-05-05 18:30:29 +0800210 return 0;
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800211 }
212
213 public boolean isReadonly() {
214 return true;
215 }
216
217 public boolean isDrm() {
218 return false;
219 }
220
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800221 public boolean rotateImageBy(int degrees) {
222 return false;
223 }
224
225 public void setTitle(String name) {
226 throw new UnsupportedOperationException();
227 }
228
229 public Uri thumbUri() {
230 throw new UnsupportedOperationException();
231 }
232}