在android开发过程中经常会处理网络图片发送内存溢出,那么怎么解决这种问题?

思路:

下载到本地

通过网络获取和文件下载存放到手机中目录

代码:

	// 获取网络
public InputStream GetHttpInfo(String urString, String fun, String parm)
throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(urString)
.openConnection();
connection.setRequestMethod(fun);
connection.setConnectTimeout(11000);
connection.setDoInput(true);
connection.setDoOutput(true); connection
.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
connection.setRequestProperty("Connection", "keep-alive");
connection
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); OutputStream outputStream = connection.getOutputStream();
outputStream.write(parm.getBytes()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { return connection.getInputStream(); } return null;
} // 文件下载
public void DownLoadFiles(String filePath, String filename,
InputStream inputStream) throws Exception { File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
} FileOutputStream fileOutputStream = new FileOutputStream(new File(file,
filename));
byte[] arrs = new byte[1024];
int len = 0;
while ((len = inputStream.read(arrs)) != -1) {
fileOutputStream.write(arrs, 0, len);
} fileOutputStream.close(); }

然后从本地文件读取到bitmap对象中

注意:需要在读取去修改图片质量可以通过下面两个函数获取修改高宽后质量bitmap对象:

	public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth) {
// 源图片的宽度
final int width = options.outWidth;
int inSampleSize = 1;
if (width > reqWidth) {
// 计算出实际宽度和目标宽度的比率
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = widthRatio;
}
return inSampleSize;
} public static Bitmap decodeSampledBitmapFromResource(String pathName,
int reqWidth) {
// 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
// 调用上面定义的方法计算inSampleSize值
options.inSampleSize = calculateInSampleSize(options, reqWidth);
// 使用获取到的inSampleSize值再次解析图片
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}

此时bitmap质量已经发生改变了!

原文地址:http://sijienet.com/bbs/?leibie=showinfo&id=51

android 防止bitmap 内存溢出的更多相关文章

  1. android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题

    android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题 android createbitmap函数内存溢出,求解怎样进行处理out of memor ...

  2. android 管理Bitmap内存 - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接   Managing Bitmap Memory 管理Bitmap内存 In additi ...

  3. android文件缓存,并SD卡创建目录未能解决和bitmap内存溢出解决

    1.相关代码: 加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ...

  4. bitmap 内存溢出OOM的解决办法分享

    昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...

  5. 缩放图片,解决bitmap 内存溢出out of memory的问题

    很多人在android开发中都遇到了生成bitmap时候内存溢出,也就是out of memory(OOM)的问题,网上对这样的问题的的解决说法不一.笔者作为一个初级开发者,在这里向大家提供一种比较实 ...

  6. 在Android中解决内存溢出 – OutOfMemoryError

    原文链接:http://riggaroo.co.za/fixing-memory-leaks-in-android-outofmemoryerror/ 注:本文在原文基础上在如何判断内存是否泄露方面进 ...

  7. Android O Bitmap 内存分配

      我们知道,一般认为在Android进程的内存模型中,heap分为两部分,一部分是native heap,一部分是Dalvik heap(实际上也是native heap的一部分).   Andro ...

  8. Android设置图片内存溢出(OOM)问题——Android开发进阶之路6

    ImageView设置图片必备常识技术: Android设备会给每个应用分配16M的内存空间,如果你设置的图片的比较大且同一个页面有多个时,经常会报OOM错误导致程序奔溃.所以在这种情况下我们必须要对 ...

  9. Android系统Bitmap内存分配原理与优化

    一.前言 笔者最近致力于vivo游戏中心稳定性维护,在分析线上异常时,发现有相当一部分是由OutOfMemory引起.谈及OOM,我们一般都会想到内存泄漏,其实,往往还有另外一个因素--图片,如果对图 ...

随机推荐

  1. POJ1759 Garland —— 二分

    题目链接:http://poj.org/problem?id=1759 Garland Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  2. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  3. Oracle:spool 的一个用法

    spool 是sqlplus的一个语法,非sql. 平时,我们通过ssh或者xmanger连接到oracle后,如果我们想把我们在上面操作的脚本及脚本执行过程.结果保存下来的话,可以通过spool来实 ...

  4. HihoCoder1705: 座位问题(STL)

    描述 HIHO银行等待区有一排N个座位,从左到右依次编号1~N.现在有M位顾客坐在座位上,其中第i位坐在编号Ai的座位上. 之后又陆续来了K位顾客,(K + M ≤ N) 他们都会选择坐在最" ...

  5. 「LuoguP3191」 [HNOI2007]紧急疏散EVACUATE(最大流

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是’.’,那么表示这是一块空地:如果是’X’,那么表示这是一面墙,如果是’D’,那么表示这是一 ...

  6. 【转】cocos2dx 内存管理机制

    原文地址: http://www.zaojiahua.com/memory-management.html cocos2dx采用的是在堆上分配内存空间,想想看你在写程序的时候对于cocos2dx中的类 ...

  7. RDA TDT & TOT

    首先看下面的TS PSI分析图: 注意:TOT UTC与TDT是一致的 TDT下的时间为: UTC+手动TIMEZONE TOT下的时间为: UTC+解析的time_offset time_offes ...

  8. MFC程序中的 _T("") 什么意思?

    _T("")就是把引号内的字符串转换为宽字节的Unicode编码 宽字节就是unicode.

  9. 关于spring boot在启动的时候报错: java.lang.Error: generate operation swagger failed, xxx.xxx.xxx

    Error starting ApplicationContext. To display the auto-configuration report re-run your application ...

  10. B. Color the Fence

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...