summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorezio84 <brabus84@gmail.com>2016-10-20 00:42:11 +0200
committerEzio Lacandia Bijelkic <brabus84@gmail.com>2016-10-20 17:57:13 -0400
commit9c9ea26bcf76c292c4c5c75558d29f3bffedd5fb (patch)
treedae91c8c023987b2dd89489dcffcf7f5a205714f
parent5656e120f3fcde002989537c32e5407981a87435 (diff)
DUI: fix SysUI FC when chosing a big img as navbar iconn7.0
Change-Id: I7ef4c5d6e997ab3f7b9f0ce00f24ea9cf033f187 Reference: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
-rw-r--r--src/com/android/internal/utils/du/DUActionUtils.java63
1 files changed, 61 insertions, 2 deletions
diff --git a/src/com/android/internal/utils/du/DUActionUtils.java b/src/com/android/internal/utils/du/DUActionUtils.java
index 58ebd8f..1e8534a 100644
--- a/src/com/android/internal/utils/du/DUActionUtils.java
+++ b/src/com/android/internal/utils/du/DUActionUtils.java
@@ -56,6 +56,7 @@ import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
@@ -448,19 +449,77 @@ public final class DUActionUtils {
}
public static Drawable getDrawable(Context context, Uri uri) {
+ //set inputs here so we can clean up them in the finally
+ InputStream inputStream = null;
try {
+ //get the inputstream
+ inputStream = context.getContentResolver().openInputStream(uri);
+
+ //get available bitmapfactory options
BitmapFactory.Options options = new BitmapFactory.Options();
+ //query the bitmap to decode the stream but don't allocate pixels in memory yet
+ options.inJustDecodeBounds = true;
+ //decode the bitmap with calculated bounds
+ Bitmap b1 = BitmapFactory.decodeStream(inputStream, null, options);
+ //check if the bitmap is too big, if yes scale the quality
+ options.inSampleSize = calculateInSampleSize(options);
+
+ //We need to close and load again the inputstream to avoid null
+ try {
+ inputStream.close();
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ inputStream = context.getContentResolver().openInputStream(uri);
+
+ //decode the stream again, with the calculated SampleSize option,
+ //and allocate the memory. Also add some metrics options to take a proper density
+ options.inJustDecodeBounds = false;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity = metrics.densityDpi;
options.inDensity = DisplayMetrics.DENSITY_DEFAULT;
- InputStream inputStream = context.getContentResolver().openInputStream(uri);
- Bitmap b1 = BitmapFactory.decodeStream(inputStream, null, options);
+ b1 = BitmapFactory.decodeStream(inputStream, null, options);
return new BitmapDrawable(context.getResources(), b1);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
+ //clean up the system resources
+ } finally {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ //Automate the quality scaling process
+ public static int calculateInSampleSize(BitmapFactory.Options options) {
+ //we do not scale quality if size is not more than 512x512
+ final int reqHeight = 512;
+ final int reqWidth = 512;
+ // get raw height and width of image
+ int height = options.outHeight;
+ int width = options.outWidth;
+ //do not scale quality for now
+ int inSampleSize = 1;
+
+ // If the img is too big, calculate the largest inSampleSize value that is a power of 2
+ // and keeps both height and width larger than the requested height and width.
+ if (height > reqHeight || width > reqWidth) {
+ final int halfHeight = height / 2;
+ final int halfWidth = width / 2;
+ while ((halfHeight / inSampleSize) >= reqHeight
+ && (halfWidth / inSampleSize) >= reqWidth) {
+ inSampleSize *= 2;
+ }
}
+ return inSampleSize;
}
public static Drawable getDrawableFromComponent(PackageManager pm, String activity) {