BaseImage.java revision b62254a185d04ecfe2ddc837b7e0e54f0273c478
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 final int mIndex;
49    protected String mMimeType;
50    private final long mDateTaken;
51    private String mTitle;
52    private final String mDisplayName;
53
54    protected BaseImageList mContainer;
55
56    private int mWidth = UNKNOWN_LENGTH;
57    private int mHeight = UNKNOWN_LENGTH;
58
59    protected BaseImage(BaseImageList container, ContentResolver cr,
60            long id, int index, Uri uri, String dataPath, String mimeType,
61            long dateTaken, String title, String displayName) {
62        mContainer = container;
63        mContentResolver = cr;
64        mId = id;
65        mIndex = index;
66        mUri = uri;
67        mDataPath = dataPath;
68        mMimeType = mimeType;
69        mDateTaken = dateTaken;
70        mTitle = title;
71        mDisplayName = displayName;
72    }
73
74    public String getDataPath() {
75        return mDataPath;
76    }
77
78    @Override
79    public boolean equals(Object other) {
80        if (other == null || !(other instanceof Image)) return false;
81        return mUri.equals(((Image) other).mUri);
82    }
83
84    @Override
85    public int hashCode() {
86        return mUri.hashCode();
87    }
88
89    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) {
90        return fullSizeBitmap(minSideLength, maxNumberOfPixels,
91                IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE);
92    }
93
94    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
95            boolean rotateAsNeeded, boolean useNative) {
96        Uri url = mContainer.contentUri(mId);
97        if (url == null) return null;
98
99        Bitmap b = Util.makeBitmap(minSideLength, maxNumberOfPixels,
100                url, mContentResolver, useNative);
101
102        if (b != null && rotateAsNeeded) {
103            b = Util.rotate(b, getDegreesRotated());
104        }
105
106        return b;
107    }
108
109    public InputStream fullSizeImageData() {
110        try {
111            InputStream input = mContentResolver.openInputStream(mUri);
112            return input;
113        } catch (IOException ex) {
114            return null;
115        }
116    }
117
118    public Uri fullSizeImageUri() {
119        return mUri;
120    }
121
122    public IImageList getContainer() {
123        return mContainer;
124    }
125
126    public long getDateTaken() {
127        return mDateTaken;
128    }
129
130    public int getDegreesRotated() {
131        return 0;
132    }
133
134    public String getMimeType() {
135        return mMimeType;
136    }
137
138    public String getTitle() {
139        return mTitle;
140    }
141
142    public String getDisplayName() {
143        return mDisplayName;
144    }
145
146    private void setupDimension() {
147        ParcelFileDescriptor input = null;
148        try {
149            input = mContentResolver.openFileDescriptor(mUri, "r");
150            BitmapFactory.Options options = new BitmapFactory.Options();
151            options.inJustDecodeBounds = true;
152            BitmapManager.instance().decodeFileDescriptor(
153                    input.getFileDescriptor(), options);
154            mWidth = options.outWidth;
155            mHeight = options.outHeight;
156        } catch (FileNotFoundException ex) {
157            mWidth = 0;
158            mHeight = 0;
159        } finally {
160            Util.closeSilently(input);
161        }
162    }
163
164    public int getWidth() {
165        if (mWidth == UNKNOWN_LENGTH) setupDimension();
166        return mWidth;
167    }
168
169    public int getHeight() {
170        if (mHeight == UNKNOWN_LENGTH) setupDimension();
171        return mHeight;
172    }
173
174    public Bitmap miniThumbBitmap() {
175        Bitmap b = null;
176        try {
177            long id = mId;
178            b = BitmapManager.instance().getThumbnail(mContentResolver, id,
179                    Images.Thumbnails.MICRO_KIND, null, false);
180        } catch (Throwable ex) {
181            Log.e(TAG, "miniThumbBitmap got exception", ex);
182            return null;
183        }
184        if (b != null) {
185            b = Util.rotate(b, getDegreesRotated());
186        }
187        return b;
188    }
189
190    protected void onRemove() {
191    }
192
193    @Override
194    public String toString() {
195        return mUri.toString();
196    }
197}
198