频繁setImageBitmap引起oom问题解决方法
Glide.with(gsewmimg).load(getCodeBitmap(response.data.skip, R.mipmap.zhifuicon)).into(gsewmimg);
压缩前后。图片大小 2.22MB——>200KB

1、图片压缩方法:
Bitmap bitmap;
byte[] buff;
buff = Bitmap2Bytes(bitmap);
BitmapFactory.Options ontain = new BitmapFactory.Options();
ontain.inSampleSize = 7;//值1为原图。值越大,压缩图片越小1-20
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length, ontain);
压缩方法2:
int height = (int) ( mybitmap.getHeight() * (512.0 / mybitmap.getWidth()) );
mybitmap = Bitmap.createScaledBitmap(mybitmap, 512, height, true);
2、bitmap转byte
private byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
3、byte转bitmap
private Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
方法二,bitmap 转byte
Bitmap frame = mCamera != null ? mCamera.Snapshot(mSelectedChannel) : null;
frame = rotate(frame, 90);
byte[] snapshot = getByteArrayFromBitmap(frame);
if (snapshot!=null){
String imageString = new String(Base64.encode(snapshot,Base64.DEFAULT));
// LgqLogPlus.e("获取到imgstring保存了===== "+imageString);
SharedPreUtil.putString(mDevUID,imageString);
}
public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
return bos.toByteArray();
} else {
return null;
}
}
byte转bitmap
String imgstring = SharedPreUtil.getString(this,mDevUID);
// LgqLogPlus.e("获取到imgstring==== "+imgstring);
if (imgstring!=null){
byte[] bytsSnapshot = Base64.decode(imgstring.getBytes(), Base64.DEFAULT);
Bitmap snapshot = (bytsSnapshot != null && bytsSnapshot.length > 0) ? getBitmapFromByteArray(bytsSnapshot) : null;
bgimg.setImageBitmap(snapshot);
}
public static Bitmap getBitmapFromByteArray(byte[] byts) {
InputStream is = new ByteArrayInputStream(byts);
return BitmapFactory.decodeStream(is, null, getBitmapOptions(2));
}
public static BitmapFactory.Options getBitmapOptions(int scale) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
options.inSampleSize = scale;
try {
BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options, true);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return options;
}
旋转方法:
//Rotate Bitmap
public final static Bitmap rotate(Bitmap b, float degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2,
(float) b.getHeight() / 2);
Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), m, true);
if (b != b2) {
b.recycle();
b = b2;
}
}
return b;
}
调用
bitmap = rotate(bitmap,90);
效果
选图

Android报错:Throwing OutOfMemoryError failed to allocate a 42793710 byte allocation with 52488 free by
在<application>里加上android:hardwareAccelerated="false" 和 android:largeHeap="true"成功解决

Bitmap与String互转
Bitmap frame = BitmapFactory.decodeResource(getResources(),R.mipmap.iv_charge2 );
byte[] snapshot = getByteArrayFromBitmap(frame);
if (snapshot!=null){
String imageString = new String(Base64.encode(snapshot,Base64.DEFAULT));
if (imageString!=null){
byte[] bytsSnapshot = Base64.decode(imageString.getBytes(), Base64.DEFAULT);
Bitmap snapshot2 = (bytsSnapshot != null && bytsSnapshot.length > 0) ? getBitmapFromByteArray(bytsSnapshot) : null;
imageView.setImageBitmap(snapshot2);
}
}
工具:
public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
return bos.toByteArray();
} else {
return null;
}
}
public static Bitmap getBitmapFromByteArray(byte[] byts) {
InputStream is = new ByteArrayInputStream(byts);
return BitmapFactory.decodeStream(is, null, getBitmapOptions(2));
}
public static BitmapFactory.Options getBitmapOptions(int scale) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
options.inSampleSize = scale;
try {
BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options, true);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return options;
}
private static void readBitmapScale(Context context, Uri uri, BitmapFactory.Options options) {
if (uri == null) {
return;
}
String scheme = uri.getScheme();
if (ContentResolver.SCHEME_CONTENT.equals(scheme) ||
ContentResolver.SCHEME_FILE.equals(scheme)) {
InputStream stream = null;
try {
stream = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(stream, null, options);
} catch (Exception e) {
Log.w("readBitmapScale", "Unable to open content: " + uri, e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
Log.e("readBitmapScale", "Unable to close content: " + uri, e);
}
}
}
} else if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
Log.e("readBitmapScale", "Unable to close content: " + uri);
} else {
Log.e("readBitmapScale", "Unable to close content: " + uri);
}
}
private static Bitmap readBitmapData(Context context, Uri uri, BitmapFactory.Options options) {
if (uri == null) {
return null;
}
Bitmap bitmap = null;
String scheme = uri.getScheme();
if (ContentResolver.SCHEME_CONTENT.equals(scheme) ||
ContentResolver.SCHEME_FILE.equals(scheme)) {
InputStream stream = null;
try {
stream = context.getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(stream, null, options);
} catch (Exception e) {
Log.e("readBitmapData", "Unable to open content: " + uri, e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
Log.e("readBitmapData", "Unable to close content: " + uri, e);
}
}
}
} else if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
Log.e("readBitmapData", "Unable to close content: " + uri);
} else {
Log.e("readBitmapData", "Unable to close content: " + uri);
}
return bitmap;
}
博客围绕Android图片处理展开,介绍了图片压缩方法,可将2.22MB图片压缩至200KB,还包含bitmap与byte[]、Bitmap与String的相互转换方法,以及图片旋转方法。同时针对频繁setImageBitmap引起的oom问题,给出了在<application>里添加相关属性的解决办法。
1万+





