Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 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; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 25 | import android.net.Uri; |
| 26 | import android.os.ParcelFileDescriptor; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import java.io.File; |
| 30 | import java.io.FileNotFoundException; |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | class UriImage implements IImage { |
| 34 | private static final String TAG = "UriImage"; |
Owen Lin | d30b587 | 2009-04-16 18:42:03 +0800 | [diff] [blame] | 35 | private final Uri mUri; |
Chih-Chung Chang | 0f1e580 | 2009-08-28 16:21:16 +0800 | [diff] [blame] | 36 | private final IImageList mContainer; |
Owen Lin | d30b587 | 2009-04-16 18:42:03 +0800 | [diff] [blame] | 37 | private final ContentResolver mContentResolver; |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 38 | |
Chih-Chung Chang | 0f1e580 | 2009-08-28 16:21:16 +0800 | [diff] [blame] | 39 | UriImage(IImageList container, ContentResolver cr, Uri uri) { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 40 | mContainer = container; |
| 41 | mContentResolver = cr; |
| 42 | mUri = uri; |
| 43 | } |
| 44 | |
Ray Chen | 012d0f3 | 2009-07-20 16:33:41 +0800 | [diff] [blame] | 45 | public int getDegreesRotated() { |
| 46 | return 0; |
| 47 | } |
| 48 | |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 49 | public String getDataPath() { |
| 50 | return mUri.getPath(); |
| 51 | } |
| 52 | |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 53 | private InputStream getInputStream() { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 54 | try { |
| 55 | if (mUri.getScheme().equals("file")) { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 56 | 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 Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 65 | private ParcelFileDescriptor getPFD() { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 66 | 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 Chang | ce033a5 | 2009-07-27 13:03:29 +0800 | [diff] [blame] | 79 | public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) { |
| 80 | return fullSizeBitmap(minSideLength, maxNumberOfPixels, |
| 81 | IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE); |
Chih-Chung Chang | 4250e21 | 2009-07-24 10:58:40 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Chih-Chung Chang | ce033a5 | 2009-07-27 13:03:29 +0800 | [diff] [blame] | 84 | public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels, |
Ray Chen | 012d0f3 | 2009-07-20 16:33:41 +0800 | [diff] [blame] | 85 | boolean rotateAsNeeded) { |
| 86 | return fullSizeBitmap(minSideLength, maxNumberOfPixels, |
| 87 | rotateAsNeeded, IImage.NO_NATIVE); |
| 88 | } |
| 89 | |
| 90 | public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels, |
Chih-Chung Chang | ce033a5 | 2009-07-27 13:03:29 +0800 | [diff] [blame] | 91 | boolean rotateAsNeeded, boolean useNative) { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 92 | try { |
| 93 | ParcelFileDescriptor pfdInput = getPFD(); |
Chih-Chung Chang | ce033a5 | 2009-07-27 13:03:29 +0800 | [diff] [blame] | 94 | Bitmap b = Util.makeBitmap(minSideLength, maxNumberOfPixels, |
| 95 | pfdInput, useNative); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 96 | return b; |
| 97 | } catch (Exception ex) { |
Chih-Chung Chang | 4250e21 | 2009-07-24 10:58:40 +0800 | [diff] [blame] | 98 | Log.e(TAG, "got exception decoding bitmap ", ex); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 99 | return null; |
| 100 | } |
| 101 | } |
| 102 | |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 103 | public Uri fullSizeImageUri() { |
| 104 | return mUri; |
| 105 | } |
| 106 | |
| 107 | public InputStream fullSizeImageData() { |
| 108 | return getInputStream(); |
| 109 | } |
| 110 | |
| 111 | public Bitmap miniThumbBitmap() { |
Ray Chen | 012d0f3 | 2009-07-20 16:33:41 +0800 | [diff] [blame] | 112 | return thumbBitmap(IImage.ROTATE_AS_NEEDED); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public String getTitle() { |
| 116 | return mUri.toString(); |
| 117 | } |
| 118 | |
| 119 | public String getDisplayName() { |
| 120 | return getTitle(); |
| 121 | } |
| 122 | |
Ray Chen | 012d0f3 | 2009-07-20 16:33:41 +0800 | [diff] [blame] | 123 | public Bitmap thumbBitmap(boolean rotateAsNeeded) { |
Wei-Ta Chen | 3c21008 | 2009-08-12 21:00:23 +0800 | [diff] [blame] | 124 | return fullSizeBitmap(THUMBNAIL_TARGET_SIZE, THUMBNAIL_MAX_NUM_PIXELS, |
Ray Chen | 012d0f3 | 2009-07-20 16:33:41 +0800 | [diff] [blame] | 125 | rotateAsNeeded); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private BitmapFactory.Options snifBitmapOptions() { |
| 129 | ParcelFileDescriptor input = getPFD(); |
| 130 | if (input == null) return null; |
| 131 | try { |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 132 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 133 | options.inJustDecodeBounds = true; |
| 134 | BitmapManager.instance().decodeFileDescriptor( |
Owen Lin | d30b587 | 2009-04-16 18:42:03 +0800 | [diff] [blame] | 135 | input.getFileDescriptor(), options); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 136 | return options; |
| 137 | } finally { |
Chih-Chung Chang | 724e9b3 | 2009-05-19 14:35:15 +0800 | [diff] [blame] | 138 | Util.closeSilently(input); |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | public String getMimeType() { |
| 143 | BitmapFactory.Options options = snifBitmapOptions(); |
Chih-Chung Chang | 10df8d2 | 2009-08-05 16:17:38 +0800 | [diff] [blame] | 144 | return (options != null && options.outMimeType != null) |
| 145 | ? options.outMimeType |
| 146 | : ""; |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 147 | } |
| 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 Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 159 | 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 Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 171 | public boolean isReadonly() { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | public boolean isDrm() { |
| 176 | return false; |
| 177 | } |
| 178 | |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 179 | public boolean rotateImageBy(int degrees) { |
| 180 | return false; |
| 181 | } |
Chih-Chung Chang | 2b82c4a | 2009-04-14 18:00:10 +0800 | [diff] [blame] | 182 | } |