Home | History | Annotate | Download | only in gallery
      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.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";
     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 Bitmap thumbBitmap(boolean rotateAsNeeded) {
    120         return fullSizeBitmap(THUMBNAIL_TARGET_SIZE, THUMBNAIL_MAX_NUM_PIXELS,
    121                 rotateAsNeeded);
    122     }
    123 
    124     private BitmapFactory.Options snifBitmapOptions() {
    125         ParcelFileDescriptor input = getPFD();
    126         if (input == null) return null;
    127         try {
    128             BitmapFactory.Options options = new BitmapFactory.Options();
    129             options.inJustDecodeBounds = true;
    130             BitmapManager.instance().decodeFileDescriptor(
    131                     input.getFileDescriptor(), options);
    132             return options;
    133         } finally {
    134             Util.closeSilently(input);
    135         }
    136     }
    137 
    138     public String getMimeType() {
    139         BitmapFactory.Options options = snifBitmapOptions();
    140         return (options != null && options.outMimeType != null)
    141                 ? options.outMimeType
    142                 : "";
    143     }
    144 
    145     public int getHeight() {
    146         BitmapFactory.Options options = snifBitmapOptions();
    147         return (options != null) ? options.outHeight : 0;
    148     }
    149 
    150     public int getWidth() {
    151         BitmapFactory.Options options = snifBitmapOptions();
    152         return (options != null) ? options.outWidth : 0;
    153     }
    154 
    155     public IImageList getContainer() {
    156         return mContainer;
    157     }
    158 
    159     public long getDateTaken() {
    160         return 0;
    161     }
    162 
    163     public boolean isReadonly() {
    164         return true;
    165     }
    166 
    167     public boolean isDrm() {
    168         return false;
    169     }
    170 
    171     public boolean rotateImageBy(int degrees) {
    172         return false;
    173     }
    174 }
    175