• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.internal.widget;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.net.Uri;
26 
27 import java.io.IOException;
28 import java.io.InputStream;
29 
30 /**
31  * A class to extract Bitmaps from a MessagingStyle message.
32  */
33 public class LocalImageResolver {
34 
35     private static final int MAX_SAFE_ICON_SIZE_PX = 480;
36 
37     @Nullable
resolveImage(Uri uri, Context context)38     public static Drawable resolveImage(Uri uri, Context context) throws IOException {
39         BitmapFactory.Options onlyBoundsOptions = getBoundsOptionsForImage(uri, context);
40         if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) {
41             return null;
42         }
43 
44         int originalSize =
45                 (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth)
46                         ? onlyBoundsOptions.outHeight
47                         : onlyBoundsOptions.outWidth;
48 
49         double ratio = (originalSize > MAX_SAFE_ICON_SIZE_PX)
50                         ? (originalSize / MAX_SAFE_ICON_SIZE_PX)
51                         : 1.0;
52 
53         BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
54         bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
55         InputStream input = context.getContentResolver().openInputStream(uri);
56         Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
57         input.close();
58         return new BitmapDrawable(context.getResources(), bitmap);
59     }
60 
getBoundsOptionsForImage(Uri uri, Context context)61     private static BitmapFactory.Options getBoundsOptionsForImage(Uri uri, Context context)
62             throws IOException {
63         InputStream input = context.getContentResolver().openInputStream(uri);
64         BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
65         onlyBoundsOptions.inJustDecodeBounds = true;
66         BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
67         input.close();
68         return onlyBoundsOptions;
69     }
70 
getPowerOfTwoForSampleRatio(double ratio)71     private static int getPowerOfTwoForSampleRatio(double ratio) {
72         int k = Integer.highestOneBit((int) Math.floor(ratio));
73         return Math.max(1, k);
74     }
75 }
76