BitmapFactory.Options factory = new BitmapFactory.Options();
factory = AndroidUtil.setOptions(factory);
ByteBuffer byteBuffer = AndroidUtil.loadImage(appfilepath + "/" + path + "/"
+ card.getCardId() + "/" + Values.USER_UUID+ "/PHOTO/PHOTO.jpg");
byte[] byteData = byteBuffer.array();
Bitmap bm = BitmapFactory.decodeByteArray(byteData, 0, byteData.length, factory);
BitmapDrawable bd = new BitmapDrawable(bm);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {login_icon.setBackgroundDrawable(bd); }
else
{login_icon.setBackground(bd); }
//===========================================================================//
public static BitmapFactory.Options setOptions(BitmapFactory.Options opts) {
opts.inJustDecodeBounds = false;
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inSampleSize = 1;
return opts;
}
//====================================================================//
public static ByteBuffer loadImage(String imagePath) throws IOException, OutOfMemoryError {
FileInputStream fis = null;
ByteBuffer byteBuffer = null;
try {
File file = new File(imagePath);
fis = new FileInputStream(file);
int length = fis.available();
byteBuffer = ByteBuffer.allocate(length);
if (length != -1) {
byte[] temp = new byte[512];
int readLen = 0;
while ((readLen = fis.read(temp)) > 0) {
byteBuffer.put(temp, 0, readLen);
}
}
return byteBuffer;
}
catch(OutOfMemoryError e) {
System.gc();
e.printStackTrace();
byteBuffer = null;
throw e;
}
catch(IOException e) {
e.printStackTrace();
byteBuffer = null;
return null;
} finally {
if (fis != null) {
fis.close();
fis = null;
}
}
}
//====================导致内存溢出的代码============================//
BitmapFactory FileInputStream fis = new FileInputStream(userLogo);
Drawable drawable = new BitmapDrawable(null,BitmapFactory.decodeStream(fis));
login_icon.setImageDrawable(drawable);
图片 java.lang.OutOfMemoryError
最新推荐文章于 2022-09-15 00:05:34 发布