不少人在調用圖庫選擇圖片時會在onActivityResult中用Media.getBitmap來獲取返回的圖片,以下:html
- Uri mImageCaptureUri = data.getData();
- Bitmap photoBmp = null;
- if (mImageCaptureUri != null) {
- photoBmp = MediaStore.Images.Media.getBitmap(ac.getContentResolver(), mImageCaptureUri);
- }
可是Media.getBitmap這個方法獲取已知uri圖片的方式並不可取,咱來看看Media.getBitmap()方法的源碼:java
- public static final Bitmap getBitmap(ContentResolver cr, Uri url)
- throws FileNotFoundException, IOException {
- InputStream input = cr.openInputStream(url);
- Bitmap bitmap = BitmapFactory.decodeStream(input);
- input.close();
- return bitmap;
- }
其實它很簡單很粗暴,返回的是原始大小的bitmap,當圖庫選擇的圖片很大時程序極有可能會報OOM。數據庫
爲了不OOM,我們須要改進該方法,在 BitmapFactory.decodeStream 以前壓縮圖片,如下是我改進後的代碼:post
在onActivityResult中調用url
- Uri mImageCaptureUri = data.getData();
-
- Bitmap photoBmp = null;
-
- if (mImageCaptureUri != null) {
-
- photoBmp = getBitmapFormUri(ac, mImageCaptureUri);
-
- }
- public static Bitmap getBitmapFormUri(Activity ac, Uri uri) throws FileNotFoundException, IOException {
- InputStream input = ac.getContentResolver().openInputStream(uri);
- BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
- onlyBoundsOptions.inJustDecodeBounds = true;
- onlyBoundsOptions.inDither = true;
- onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
- BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
- input.close();
- int originalWidth = onlyBoundsOptions.outWidth;
- int originalHeight = onlyBoundsOptions.outHeight;
- if ((originalWidth == -1) || (originalHeight == -1))
- return null;
-
- float hh = 800f;
- float ww = 480f;
-
- int be = 1;
- if (originalWidth > originalHeight && originalWidth > ww) {
- be = (int) (originalWidth / ww);
- } else if (originalWidth < originalHeight && originalHeight > hh) {
- be = (int) (originalHeight / hh);
- }
- if (be <= 0)
- be = 1;
-
- BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
- bitmapOptions.inSampleSize = be;
- bitmapOptions.inDither = true;
- bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
- input = ac.getContentResolver().openInputStream(uri);
- Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
- input.close();
-
- return compressImage(bitmap);
- }
- public static Bitmap compressImage(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- int options = 100;
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
-
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- options -= 10;
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
OOM的問題解決了,可是又碰到另一個問題,用三星手機拍照或者選擇照片後返回來的圖片竟然轉了90度,接着改。spa
在onActivityResult中的代碼進行改進:.net
- Uri originalUri = null;
- File file = null;
- if (null != data && data.getData() != null) {
- originalUri = data.getData();
- file = getFileFromMediaUri(ac, originalUri);
- }
- Bitmap photoBmp = getBitmapFormUri(ac, Uri.fromFile(file));
- int degree = getBitmapDegree(file.getAbsolutePath());
-
- Bitmap newbitmap = rotateBitmapByDegree(photoBmp, degree);
- public static File getFileFromMediaUri(Context ac, Uri uri) {
- if(uri.getScheme().toString().compareTo("content") == 0){
- ContentResolver cr = ac.getContentResolver();
- Cursor cursor = cr.query(uri, null, null, null, null);
- if (cursor != null) {
- cursor.moveToFirst();
- String filePath = cursor.getString(cursor.getColumnIndex("_data"));
- cursor.close();
- if (filePath != null) {
- return new File(filePath);
- }
- }
- }else if(uri.getScheme().toString().compareTo("file") == 0){
- return new File(uri.toString().replace("file://",""));
- }
- return null;
- }
- public static int getBitmapDegree(String path) {
- int degree = 0;
- try {
-
- ExifInterface exifInterface = new ExifInterface(path);
-
- int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
- ExifInterface.ORIENTATION_NORMAL);
- switch (orientation) {
- case ExifInterface.ORIENTATION_ROTATE_90:
- degree = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- degree = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- degree = 270;
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return degree;
- }
- public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
- Bitmap returnBm = null;
-
-
- Matrix matrix = new Matrix();
- matrix.postRotate(degree);
- try {
-
- returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
- } catch (OutOfMemoryError e) {
- }
- if (returnBm == null) {
- returnBm = bm;
- }
- if (bm != returnBm) {
- bm.recycle();
- }
- return returnBm;
- }
來自:http://www.cnblogs.com/popqq520/p/5404738.htmlcode