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 17 package com.android.gallery3d.ingest.data; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.mtp.MtpDevice; 23 import android.mtp.MtpObjectInfo; 24 import android.util.DisplayMetrics; 25 import android.view.WindowManager; 26 27 import com.android.camera.Exif; 28 import com.android.photos.data.GalleryBitmapPool; 29 30 public class MtpBitmapFetch { 31 private static int sMaxSize = 0; 32 33 public static void recycleThumbnail(Bitmap b) { 34 if (b != null) { 35 GalleryBitmapPool.getInstance().put(b); 36 } 37 } 38 39 public static Bitmap getThumbnail(MtpDevice device, MtpObjectInfo info) { 40 byte[] imageBytes = device.getThumbnail(info.getObjectHandle()); 41 if (imageBytes == null) { 42 return null; 43 } 44 BitmapFactory.Options o = new BitmapFactory.Options(); 45 o.inJustDecodeBounds = true; 46 BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, o); 47 if (o.outWidth == 0 || o.outHeight == 0) { 48 return null; 49 } 50 o.inBitmap = GalleryBitmapPool.getInstance().get(o.outWidth, o.outHeight); 51 o.inMutable = true; 52 o.inJustDecodeBounds = false; 53 o.inSampleSize = 1; 54 try { 55 return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, o); 56 } catch (IllegalArgumentException e) { 57 // BitmapFactory throws an exception rather than returning null 58 // when image decoding fails and an existing bitmap was supplied 59 // for recycling, even if the failure was not caused by the use 60 // of that bitmap. 61 return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 62 } 63 } 64 65 public static BitmapWithMetadata getFullsize(MtpDevice device, MtpObjectInfo info) { 66 return getFullsize(device, info, sMaxSize); 67 } 68 69 public static BitmapWithMetadata getFullsize(MtpDevice device, MtpObjectInfo info, int maxSide) { 70 byte[] imageBytes = device.getObject(info.getObjectHandle(), info.getCompressedSize()); 71 if (imageBytes == null) { 72 return null; 73 } 74 Bitmap created; 75 if (maxSide > 0) { 76 BitmapFactory.Options o = new BitmapFactory.Options(); 77 o.inJustDecodeBounds = true; 78 BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, o); 79 int w = o.outWidth; 80 int h = o.outHeight; 81 int comp = Math.max(h, w); 82 int sampleSize = 1; 83 while ((comp >> 1) >= maxSide) { 84 comp = comp >> 1; 85 sampleSize++; 86 } 87 o.inSampleSize = sampleSize; 88 o.inJustDecodeBounds = false; 89 created = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, o); 90 } else { 91 created = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 92 } 93 if (created == null) { 94 return null; 95 } 96 97 return new BitmapWithMetadata(created, Exif.getOrientation(imageBytes)); 98 } 99 100 public static void configureForContext(Context context) { 101 DisplayMetrics metrics = new DisplayMetrics(); 102 WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 103 wm.getDefaultDisplay().getMetrics(metrics); 104 sMaxSize = Math.max(metrics.heightPixels, metrics.widthPixels); 105 } 106 } 107