UriImage.java revision 7d2d5ec1e9d6c19278e41c103eacd8090be24406
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.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";
35    private final Uri mUri;
36    private final IImageList mContainer;
37    private final ContentResolver mContentResolver;
38
39    UriImage(IImageList container, ContentResolver cr, Uri uri) {
40        mContainer = container;
41        mContentResolver = cr;
42        mUri = uri;
43    }
44
45    public int getDegreesRotated() {
46        return 0;
47    }
48
49    public String getDataPath() {
50        return mUri.getPath();
51    }
52
53    private InputStream getInputStream() {
54        try {
55            if (mUri.getScheme().equals("file")) {
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
65    private ParcelFileDescriptor getPFD() {
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
79    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels) {
80        return fullSizeBitmap(minSideLength, maxNumberOfPixels,
81                IImage.ROTATE_AS_NEEDED, IImage.NO_NATIVE);
82    }
83
84    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
85            boolean rotateAsNeeded) {
86        return fullSizeBitmap(minSideLength, maxNumberOfPixels,
87                rotateAsNeeded, IImage.NO_NATIVE);
88    }
89
90    public Bitmap fullSizeBitmap(int minSideLength, int maxNumberOfPixels,
91            boolean rotateAsNeeded, boolean useNative) {
92        try {
93            ParcelFileDescriptor pfdInput = getPFD();
94            Bitmap b = Util.makeBitmap(minSideLength, maxNumberOfPixels,
95                    pfdInput, useNative);
96            return b;
97        } catch (Exception ex) {
98            Log.e(TAG, "got exception decoding bitmap ", ex);
99            return null;
100        }
101    }
102
103    public Uri fullSizeImageUri() {
104        return mUri;
105    }
106
107    public InputStream fullSizeImageData() {
108        return getInputStream();
109    }
110
111    public Bitmap miniThumbBitmap() {
112        return thumbBitmap(IImage.ROTATE_AS_NEEDED);
113    }
114
115    public String getTitle() {
116        return mUri.toString();
117    }
118
119    public String getDisplayName() {
120        return getTitle();
121    }
122
123    public Bitmap thumbBitmap(boolean rotateAsNeeded) {
124        return fullSizeBitmap(THUMBNAIL_TARGET_SIZE, THUMBNAIL_MAX_NUM_PIXELS,
125                rotateAsNeeded);
126    }
127
128    private BitmapFactory.Options snifBitmapOptions() {
129        ParcelFileDescriptor input = getPFD();
130        if (input == null) return null;
131        try {
132            BitmapFactory.Options options = new BitmapFactory.Options();
133            options.inJustDecodeBounds = true;
134            BitmapManager.instance().decodeFileDescriptor(
135                    input.getFileDescriptor(), options);
136            return options;
137        } finally {
138            Util.closeSilently(input);
139        }
140    }
141
142    public String getMimeType() {
143        BitmapFactory.Options options = snifBitmapOptions();
144        return (options != null && options.outMimeType != null)
145                ? options.outMimeType
146                : "";
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
159    public IImageList getContainer() {
160        return mContainer;
161    }
162
163    public long getDateTaken() {
164        return 0;
165    }
166
167    public boolean isReadonly() {
168        return true;
169    }
170
171    public boolean isDrm() {
172        return false;
173    }
174
175    public boolean rotateImageBy(int degrees) {
176        return false;
177    }
178}
179