public static Bitmap getBitmapFromPath(Context context, String path) { //強制的にGC System.gc(); //pathをURI変換 File image_file = new File(path); final Uri image_uri = Uri.fromFile(image_file); Uri uri2 = fileUri2contentUri(context, image_uri); int deg = 0; Bitmap imageBitmap = null; Bitmap rotatedBitmap = null; try { ContentResolver contentResolver = context.getContentResolver(); InputStream inputStream; BitmapFactory.Options imageOptions; // メモリ上に画像を読み込まず、画像サイズ情報のみを取得する inputStream = contentResolver.openInputStream(uri2); imageOptions = new BitmapFactory.Options(); imageOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, imageOptions); inputStream.close(); //画面サイズの取得 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int view_width = (int)(display.getWidth() / 2); int view_height = (int)(display.getHeight() / 2); //画像を縮小して読み込む inputStream = contentResolver.openInputStream(uri2); // 画像の元サイズ final int height = imageOptions.outHeight; final int width = imageOptions.outWidth; int inSampleSize = 1; if (height > view_height || width > view_width) { if (width > height) { inSampleSize = Math.round((float)height / (float)view_height); } else { inSampleSize = Math.round((float)width / (float)view_width); } } imageOptions = new BitmapFactory.Options(); imageOptions.inSampleSize = inSampleSize; imageBitmap = BitmapFactory.decodeStream(inputStream, null, imageOptions); //画像を回転させる deg = GetImageOrientation(context, uri2); Matrix mat = new Matrix(); mat.postRotate( deg ); int sw = imageBitmap.getWidth(); int sh = imageBitmap.getHeight(); rotatedBitmap = Bitmap.createBitmap( imageBitmap, 0, 0, sw, sh, mat, true); //inputStream.close(); } catch (Exception e) { System.out.println("e:" + e); } imageBitmap = null; return rotatedBitmap; }
fileUri2contentUri()メソッドについてはこちら
GetImageOrientation()メソッドについてはこちら
コメントを残す