在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. Tomcat调优策略

    Jmeter压力测试工具 JMeter是一款在国外非常流行和受欢迎的开源性能测试工具,像LoadRunner 一样,它也提供了一个利用本地Proxy Server(代理服务器)来录制生成测试脚本的功能 ...

  2. java基础知识一

    1.计算机基础知识概述 (1) 计算机 计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能.是能够按照程序运行,自动.高速处 ...

  3. silverlight 图片引入代码

    private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e) { string str = t ...

  4. Oracle安装:silent安装方式

    之前一直是通过图形界面来安装oracle,这次上机考试说用silent (静默)形式安装.一点头绪都没有,虽然当时提供了oracle官方文档. 遂查找资料,安装了一下: 一.准备工作: 1.系统参数调 ...

  5. hdu-2066 一个人的旅行(最短路spfa)

    题目链接: 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Pr ...

  6. SPOJ:Stack Overflow(并查集)

    Stack is a basic data structure. Where 3 operation can be done- Push: You can push object to the sta ...

  7. POJ1474:Video Surveillance(求多边形的核)(占位)

    A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...

  8. 【总结——HTTP协议】

    一.HTTP协议简介 什么是HTTP?全称是HyperText Transfer Protocal,即:超文本传输协议,从1990年开始就在WWW上广泛应用,是现今在WWW上应用最多的协议,目前版本是 ...

  9. Laravel配置nginx环境

    前言: 之前坑的!一直在尝试配置,但都失败了,只能用着apache,但是最近想整合swoole到laravel,无奈当前测试服务器是nginx,我只能再尝试在nginx上搭laravel环境 方法如下 ...

  10. ASP.NET Core:WebAppCoreRESTful

    ylbtech-ASP.NET Core:WebAppCoreRESTFul 1.返回顶部 1. 2. 3.         4. 2. Controllers返回顶部 1.HomeControlle ...