blob: 0db1633cc22f987688f907eae6822cdfb172217c [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;
Chih-Chung Chang0f1e5802009-08-28 16:21:16 +080036 private final IImageList mContainer;
Owen Lind30b5872009-04-16 18:42:03 +080037 private final ContentResolver mContentResolver;
Chih-Chung Chang0a475e12009-04-16 11:42:12 +080038
Chih-Chung Chang0f1e5802009-08-28 16:21:16 +080039 UriImage(IImageList container, ContentResolver cr, Uri uri) {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080040 mContainer = container;
41 mContentResolver = cr;
42 mUri = uri;
43 }
44
Ray Chen012d0f32009-07-20 16:33:41 +080045 public int getDegreesRotated() {
46 return 0;
47 }
48
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080049 public String getDataPath() {
50 return mUri.getPath();
51 }
52
Chih-Chung Chang0a475e12009-04-16 11:42:12 +080053 private InputStream getInputStream() {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080054 try {
55 if (mUri.getScheme().equals("file")) {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080056 return new java.io.FileInputStream(mUri.getPath());
57 } else {
58 return mContentResolver.openInputStream(mUri);
59 }
60 } catch (FileNotFoundException ex) {
61 return null;
62 }
63 }
64
Chih-Chung Chang0a475e12009-04-16 11:42:12 +080065 private ParcelFileDescriptor getPFD() {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080066 try {
67 if (mUri.getScheme().equals("file")) {
68 String path = mUri.getPath();
69 return ParcelFileDescriptor.open(new File(path),
70 ParcelFileDescriptor.MODE_READ_ONLY);
71 } else {
72 return mContentResolver.openFileDescriptor(mUri, "r");
73 }
74 } catch (FileNotFoundException ex) {
75 return null;
76 }
77 }
78
Chih-Chung Changce033a52009-07-27 13:03:29 +080079 public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) {
80 return fullSizeBitmap(minSideLength, maxNumberOfPixels,
81 IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE);
Chih-Chung Chang4250e212009-07-24 10:58:40 +080082 }
83
Chih-Chung Changce033a52009-07-27 13:03:29 +080084 public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
Ray Chen012d0f32009-07-20 16:33:41 +080085 boolean rotateAsNeeded) {
86 return fullSizeBitmap(minSideLength, maxNumberOfPixels,
87 rotateAsNeeded, IImage.NO_NATIVE);
88 }
89
90 public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
Chih-Chung Changce033a52009-07-27 13:03:29 +080091 boolean rotateAsNeeded, boolean useNative) {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080092 try {
93 ParcelFileDescriptor pfdInput = getPFD();
Chih-Chung Changce033a52009-07-27 13:03:29 +080094 Bitmap b = Util.makeBitmap(minSideLength, maxNumberOfPixels,
95 pfdInput, useNative);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080096 return b;
97 } catch (Exception ex) {
Chih-Chung Chang4250e212009-07-24 10:58:40 +080098 Log.e(TAG, "got exception decoding bitmap ", ex);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +080099 return null;
100 }
101 }
102
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800103 public Uri fullSizeImageUri() {
104 return mUri;
105 }
106
107 public InputStream fullSizeImageData() {
108 return getInputStream();
109 }
110
111 public Bitmap miniThumbBitmap() {
Ray Chen012d0f32009-07-20 16:33:41 +0800112 return thumbBitmap(IImage.ROTATE_AS_NEEDED);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800113 }
114
115 public String getTitle() {
116 return mUri.toString();
117 }
118
119 public String getDisplayName() {
120 return getTitle();
121 }
122
Ray Chen012d0f32009-07-20 16:33:41 +0800123 public Bitmap thumbBitmap(boolean rotateAsNeeded) {
Wei-Ta Chen3c210082009-08-12 21:00:23 +0800124 return fullSizeBitmap(THUMBNAIL_TARGET_SIZE, THUMBNAIL_MAX_NUM_PIXELS,
Ray Chen012d0f32009-07-20 16:33:41 +0800125 rotateAsNeeded);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800126 }
127
128 private BitmapFactory.Options snifBitmapOptions() {
129 ParcelFileDescriptor input = getPFD();
130 if (input == null) return null;
131 try {
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800132 BitmapFactory.Options options = new BitmapFactory.Options();
133 options.inJustDecodeBounds = true;
134 BitmapManager.instance().decodeFileDescriptor(
Owen Lind30b5872009-04-16 18:42:03 +0800135 input.getFileDescriptor(), options);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800136 return options;
137 } finally {
Chih-Chung Chang724e9b32009-05-19 14:35:15 +0800138 Util.closeSilently(input);
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800139 }
140 }
141
142 public String getMimeType() {
143 BitmapFactory.Options options = snifBitmapOptions();
Chih-Chung Chang10df8d22009-08-05 16:17:38 +0800144 return (options != null && options.outMimeType != null)
145 ? options.outMimeType
146 : "";
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800147 }
148
149 public int getHeight() {
150 BitmapFactory.Options options = snifBitmapOptions();
151 return (options != null) ? options.outHeight : 0;
152 }
153
154 public int getWidth() {
155 BitmapFactory.Options options = snifBitmapOptions();
156 return (options != null) ? options.outWidth : 0;
157 }
158
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800159 public long fullSizeImageId() {
160 return 0;
161 }
162
163 public IImageList getContainer() {
164 return mContainer;
165 }
166
167 public long getDateTaken() {
168 return 0;
169 }
170
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800171 public boolean isReadonly() {
172 return true;
173 }
174
175 public boolean isDrm() {
176 return false;
177 }
178
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800179 public boolean rotateImageBy(int degrees) {
180 return false;
181 }
Chih-Chung Chang2b82c4a2009-04-14 18:00:10 +0800182}