BaseImage.java revision 666ea1b28a76aeba74744148b15099254d918671
1/*
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;
25import android.net.Uri;
26import android.os.ParcelFileDescriptor;
27import android.provider.MediaStore.Images;
28import android.util.Log;
29
30import java.io.FileNotFoundException;
31import java.io.IOException;
32import java.io.InputStream;
33
34/**
35 * Represents a particular image and provides access to the underlying bitmap
36 * and two thumbnail bitmaps as well as other information such as the id, and
37 * the path to the actual image data.
38 */
39public abstract class BaseImage implements IImage {
40    private static final String TAG = "BaseImage";
41    private static final int UNKNOWN_LENGTH = -1;
42    protected ContentResolver mContentResolver;
43
44    // Database field
45    protected Uri mUri;
46    protected long mId;
47    protected String mDataPath;
48    protected long mMiniThumbMagic;
49    protected final int mIndex;
50    protected String mMimeType;
51    private final long mDateTaken;
52    private String mTitle;
53    private final String mDisplayName;
54
55    protected BaseImageList mContainer;
56
57    private int mWidth = UNKNOWN_LENGTH;
58    private int mHeight = UNKNOWN_LENGTH;
59
60    protected BaseImage(BaseImageList container, ContentResolver cr,
61            long id, int index, Uri uri, String dataPath, long miniThumbMagic,
62            String mimeType, long dateTaken, String title, String displayName) {
63        mContainer = container;
64        mContentResolver = cr;
65        mId = id;
66        mIndex = index;
67        mUri = uri;
68        mDataPath = dataPath;
69        mMiniThumbMagic = miniThumbMagic;
70        mMimeType = mimeType;
71        mDateTaken = dateTaken;
72        mTitle = title;
73        mDisplayName = displayName;
74    }
75
76    public String getDataPath() {
77        return mDataPath;
78    }
79
80    @Override
81    public boolean equals(Object other) {
82        if (other == null || !(other instanceof Image)) return false;
83        return mUri.equals(((Image) other).mUri);
84    }
85
86    @Override
87    public int hashCode() {
88        return mUri.hashCode();
89    }
90
91    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) {
92        return fullSizeBitmap(minSideLength, maxNumberOfPixels,
93                IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE);
94    }
95
96    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
97            boolean rotateAsNeeded, boolean useNative) {
98        Uri url = mContainer.contentUri(mId);
99        if (url == null) return null;
100
101        Bitmap b = Util.makeBitmap(minSideLength, maxNumberOfPixels,
102                url, mContentResolver, useNative);
103
104        if (b != null && rotateAsNeeded) {
105            b = Util.rotate(b, getDegreesRotated());
106        }
107
108        return b;
109    }
110
111    public InputStream fullSizeImageData() {
112        try {
113            InputStream input = mContentResolver.openInputStream(mUri);
114            return input;
115        } catch (IOException ex) {
116            return null;
117        }
118    }
119
120    public long fullSizeImageId() {
121        return mId;
122    }
123
124    public Uri fullSizeImageUri() {
125        return mUri;
126    }
127
128    public IImageList getContainer() {
129        return mContainer;
130    }
131
132    public long getDateTaken() {
133        return mDateTaken;
134    }
135
136    public int getDegreesRotated() {
137        return 0;
138    }
139
140    public String getMimeType() {
141        return mMimeType;
142    }
143
144    public String getTitle() {
145        return mTitle;
146    }
147
148    public String getDisplayName() {
149        return mDisplayName;
150    }
151
152    private void setupDimension() {
153        ParcelFileDescriptor input = null;
154        try {
155            input = mContentResolver.openFileDescriptor(mUri, "r");
156            BitmapFactory.Options options = new BitmapFactory.Options();
157            options.inJustDecodeBounds = true;
158            BitmapManager.instance().decodeFileDescriptor(
159                    input.getFileDescriptor(), options);
160            mWidth = options.outWidth;
161            mHeight = options.outHeight;
162        } catch (FileNotFoundException ex) {
163            mWidth = 0;
164            mHeight = 0;
165        } finally {
166            Util.closeSilently(input);
167        }
168    }
169
170    public int getWidth() {
171        if (mWidth == UNKNOWN_LENGTH) setupDimension();
172        return mWidth;
173    }
174
175    public int getHeight() {
176        if (mHeight == UNKNOWN_LENGTH) setupDimension();
177        return mHeight;
178    }
179
180    public Bitmap miniThumbBitmap() {
181        Bitmap b = null;
182        try {
183            long id = mId;
184            b = Images.Thumbnails.getThumbnail(mContentResolver, id,
185                    Images.Thumbnails.MICRO_KIND, null);
186        } catch (Throwable ex) {
187            Log.e(TAG, "miniThumbBitmap got exception", ex);
188            return null;
189        }
190        if (b != null) {
191            b = Util.rotate(b, getDegreesRotated());
192        }
193        return b;
194    }
195
196    protected void onRemove() {
197    }
198
199    @Override
200    public String toString() {
201        return mUri.toString();
202    }
203}
204