public Bitmap getimage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options();// 开始读入图片 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth;//获取屏幕宽高 int h = newOpts.outHeight; newOpts.inSampleSize = ImageUtils.calculateInSampleSize(newOpts,w,h);// 设置缩放比例 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return bitmap; }
public static int calculateInSampleSize(final BitmapFactory.Options options, final int maxWidth, final int maxHeight) { int height = options.outHeight; int width = options.outWidth; int inSampleSize = 1; while ((width >>= 1) >= maxWidth && (height >>= 1) >= maxHeight) { inSampleSize <<= 1; } return inSampleSize; } private static byte[] input2Byte(final InputStream is) { if (is == null) return null; try { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] b = new byte[MemoryConstants.KB]; int len; while ((len = is.read(b, 0, MemoryConstants.KB)) != -1) { os.write(b, 0, len); } return os.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } public static Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight) { // 获取这个图片的宽和高 float width = bgimage.getWidth(); float height = bgimage.getHeight(); // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 计算宽高缩放率 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width, (int) height, matrix, true); return bitmap; } } 问题解决,抬走