1 /* 2 * Copyright (C) 2013 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 package com.android.photos.data; 17 18 import android.graphics.Bitmap; 19 import android.media.ExifInterface; 20 import android.net.Uri; 21 import android.util.Log; 22 import android.webkit.MimeTypeMap; 23 24 import com.android.gallery3d.common.BitmapUtils; 25 26 import java.io.File; 27 import java.io.IOException; 28 29 public class FileRetriever implements MediaRetriever { 30 private static final String TAG = FileRetriever.class.getSimpleName(); 31 32 @Override 33 public File getLocalFile(Uri contentUri) { 34 return new File(contentUri.getPath()); 35 } 36 37 @Override 38 public MediaSize getFastImageSize(Uri contentUri, MediaSize size) { 39 if (isVideo(contentUri)) { 40 return null; 41 } 42 return MediaSize.TemporaryThumbnail; 43 } 44 45 @Override 46 public byte[] getTemporaryImage(Uri contentUri, MediaSize fastImageSize) { 47 48 try { 49 ExifInterface exif = new ExifInterface(contentUri.getPath()); 50 if (exif.hasThumbnail()) { 51 return exif.getThumbnail(); 52 } 53 } catch (IOException e) { 54 Log.w(TAG, "Unable to load exif for " + contentUri); 55 } 56 return null; 57 } 58 59 @Override 60 public boolean getMedia(Uri contentUri, MediaSize imageSize, File tempFile) { 61 if (imageSize == MediaSize.Original) { 62 return false; // getLocalFile should always return the original. 63 } 64 if (imageSize == MediaSize.Thumbnail) { 65 File preview = MediaCache.getInstance().getCachedFile(contentUri, MediaSize.Preview); 66 if (preview != null) { 67 // Just downsample the preview, it is faster. 68 return MediaCacheUtils.downsample(preview, imageSize, tempFile); 69 } 70 } 71 File highRes = new File(contentUri.getPath()); 72 boolean success; 73 if (!isVideo(contentUri)) { 74 success = MediaCacheUtils.downsample(highRes, imageSize, tempFile); 75 } else { 76 // Video needs to extract the bitmap. 77 Bitmap bitmap = BitmapUtils.createVideoThumbnail(highRes.getPath()); 78 if (bitmap == null) { 79 return false; 80 } else if (imageSize == MediaSize.Thumbnail 81 && !MediaCacheUtils.needsDownsample(bitmap, MediaSize.Preview) 82 && MediaCacheUtils.writeToFile(bitmap, tempFile)) { 83 // Opportunistically save preview 84 MediaCache mediaCache = MediaCache.getInstance(); 85 mediaCache.insertIntoCache(contentUri, MediaSize.Preview, tempFile); 86 } 87 // Now scale the image 88 success = MediaCacheUtils.downsample(bitmap, imageSize, tempFile); 89 } 90 return success; 91 } 92 93 @Override 94 public Uri normalizeUri(Uri contentUri, MediaSize size) { 95 return contentUri; 96 } 97 98 @Override 99 public MediaSize normalizeMediaSize(Uri contentUri, MediaSize size) { 100 return size; 101 } 102 103 private static boolean isVideo(Uri uri) { 104 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); 105 String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); 106 String mimeType = mimeTypeMap.getMimeTypeFromExtension(extension); 107 return (mimeType != null && mimeType.startsWith("video/")); 108 } 109 } 110