Android Bitmap Load getResizedBitmap(Uri imageUri, Activity activity)

Here you can find the source of getResizedBitmap(Uri imageUri, Activity activity)

Description

get Resized Bitmap

Declaration

public static Bitmap getResizedBitmap(Uri imageUri, Activity activity) 

Method Source Code

//package com.java2s;

import java.io.InputStream;
import android.app.Activity;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.util.DisplayMetrics;

public class Main {
    public static Bitmap getResizedBitmap(Uri imageUri, Activity activity) {

        Bitmap resizedBitmap = null;//from   w w  w  . j a v a  2  s  . c o m
        try {
            ContentResolver resolver = activity.getContentResolver();
            InputStream in = resolver.openInputStream(imageUri);
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            in.close();

            Matrix matrix = new Matrix();
            DisplayMetrics metrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay()
                    .getMetrics(metrics);
            float screenWidth = (float) metrics.widthPixels;
            float screenHeight = (float) metrics.heightPixels;

            int srcWidth = bitmap.getWidth();
            int srcHeight = bitmap.getHeight();

            float widthScale = screenWidth / srcWidth;
            float heightScale = screenHeight / srcHeight;
            if (widthScale > heightScale) {
                matrix.postScale(heightScale, heightScale);
            } else {
                matrix.postScale(widthScale, widthScale);
            }
            resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, srcWidth,
                    srcHeight, matrix, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resizedBitmap;
    }
}

Related

  1. decodeFromDescriptor( FileDescriptor descriptor, int reqWidth, int reqHeight)
  2. getBitmap(Context c, String uri, int minsizeDP)
  3. getImageFromAssetFile(Context context, String fileName)
  4. downloadImageToSd(String urlSource, String pathDest)
  5. downloadImageToSd(String urlSource, String pathDest, String referer)
  6. downSampleBitmap(Uri uri, Activity act, Boolean needRotate)
  7. getBitmap(Context context, int resId)
  8. getBitmap(String photoId)
  9. getBitmapFromAsset(String strName, Context context)