android 图片加载内存溢出

图片说明图片说明

https://blog.csdn.net/JavaLive09/article/details/52869732
为android 应用申请更多内存

https://blog.csdn.net/l_215851356/article/details/52413462这里有很好的分析

压缩图片像素 和显示区域

不要一次性加载太多图片,可以分屏显示,同时限制上传入口,图片做裁剪后上传

集成xUtils3.0框架,图片内存溢出的问题迎刃而解

一次加载的图片过大导致的,可以和服务端协商,在需要小图的时候返回小图,在需要大图的时候适当对图片进行压缩。

压缩图片,或者用别的方法,例如限定请求图片的大小

压缩图片在操作,使用第三方加载库例如glide/picaso等,如果要自己封装工具的话,下载图片后先压缩质量在操作就不会出现内存溢出了

/**
* 压缩图片(质量压缩)
*
* @param bitmap bitmap
*/
public File compressImage(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 500) { //循环判断如果压缩后图片是否大于500kb,大于继续压缩
baos.reset();//重置baos即清空baos
options -= 80;//每次都压缩80%
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
// long length = baos.toByteArray().length;
}
String filename = TimeUtil.longToDate(System.currentTimeMillis());
File file = new File(cropIconDir, filename + ".png");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
recycleBitmap(bitmap);
return file;
}

    望采纳!