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 17 package com.android.camera.gallery; 18 19 import com.android.camera.BitmapManager; 20 import com.android.camera.Util; 21 22 import android.content.ContentResolver; 23 import android.graphics.Bitmap; 24 import android.graphics.BitmapFactory; 25 import android.net.Uri; 26 import android.os.ParcelFileDescriptor; 27 import android.provider.MediaStore.Images; 28 import android.util.Log; 29 30 import java.io.FileNotFoundException; 31 import java.io.IOException; 32 import 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 */ 39 public 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 BaseImage(BaseImageList container, ContentResolver cr, long id, int index, Uri uri, String dataPath, long miniThumbMagic, String mimeType, long dateTaken, String title, String displayName)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 getDataPath()76 public String getDataPath() { 77 return mDataPath; 78 } 79 80 @Override equals(Object other)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 hashCode()87 public int hashCode() { 88 return mUri.hashCode(); 89 } 90 fullSizeBitmap(int minSideLength, int maxNumberOfPixels)91 public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) { 92 return fullSizeBitmap(minSideLength, maxNumberOfPixels, 93 IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE); 94 } 95 fullSizeBitmap(int minSideLength, int maxNumberOfPixels, boolean rotateAsNeeded, boolean useNative)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 fullSizeImageData()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 fullSizeImageId()120 public long fullSizeImageId() { 121 return mId; 122 } 123 fullSizeImageUri()124 public Uri fullSizeImageUri() { 125 return mUri; 126 } 127 getContainer()128 public IImageList getContainer() { 129 return mContainer; 130 } 131 getDateTaken()132 public long getDateTaken() { 133 return mDateTaken; 134 } 135 getDegreesRotated()136 public int getDegreesRotated() { 137 return 0; 138 } 139 getMimeType()140 public String getMimeType() { 141 return mMimeType; 142 } 143 getTitle()144 public String getTitle() { 145 return mTitle; 146 } 147 getDisplayName()148 public String getDisplayName() { 149 return mDisplayName; 150 } 151 setupDimension()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 getWidth()170 public int getWidth() { 171 if (mWidth == UNKNOWN_LENGTH) setupDimension(); 172 return mWidth; 173 } 174 getHeight()175 public int getHeight() { 176 if (mHeight == UNKNOWN_LENGTH) setupDimension(); 177 return mHeight; 178 } 179 miniThumbBitmap()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 onRemove()196 protected void onRemove() { 197 } 198 199 @Override toString()200 public String toString() { 201 return mUri.toString(); 202 } 203 } 204