1 /* 2 * Copyright (C) 2014 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.example.android.mediabrowserservice.utils; 17 18 import android.graphics.Bitmap; 19 import android.graphics.BitmapFactory; 20 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.net.HttpURLConnection; 24 import java.net.URL; 25 26 public class BitmapHelper { 27 28 // Bitmap size for album art in media notifications when there are more than 3 playback actions 29 public static final int MEDIA_ART_SMALL_WIDTH=64; 30 public static final int MEDIA_ART_SMALL_HEIGHT=64; 31 32 // Bitmap size for album art in media notifications when there are no more than 3 playback actions 33 public static final int MEDIA_ART_BIG_WIDTH=128; 34 public static final int MEDIA_ART_BIG_HEIGHT=128; 35 scaleBitmap(int scaleFactor, InputStream is)36 public static final Bitmap scaleBitmap(int scaleFactor, InputStream is) { 37 // Get the dimensions of the bitmap 38 BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 39 40 // Decode the image file into a Bitmap sized to fill the View 41 bmOptions.inJustDecodeBounds = false; 42 bmOptions.inSampleSize = scaleFactor; 43 44 Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmOptions); 45 return bitmap; 46 } 47 findScaleFactor(int targetW, int targetH, InputStream is)48 public static final int findScaleFactor(int targetW, int targetH, InputStream is) { 49 // Get the dimensions of the bitmap 50 BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 51 bmOptions.inJustDecodeBounds = true; 52 BitmapFactory.decodeStream(is, null, bmOptions); 53 int actualW = bmOptions.outWidth; 54 int actualH = bmOptions.outHeight; 55 56 // Determine how much to scale down the image 57 return Math.min(actualW/targetW, actualH/targetH); 58 } 59 fetchAndRescaleBitmap(String uri, int width, int height)60 public static final Bitmap fetchAndRescaleBitmap(String uri, int width, int height) 61 throws IOException { 62 URL url = new URL(uri); 63 HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 64 httpConnection.setDoInput(true); 65 httpConnection.connect(); 66 InputStream inputStream = httpConnection.getInputStream(); 67 int scaleFactor = findScaleFactor(width, height, inputStream); 68 69 httpConnection = (HttpURLConnection) url.openConnection(); 70 httpConnection.setDoInput(true); 71 httpConnection.connect(); 72 inputStream = httpConnection.getInputStream(); 73 Bitmap bitmap = scaleBitmap(scaleFactor, inputStream); 74 return bitmap; 75 } 76 77 } 78