android 防止bitmap 内存溢出
在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 内存溢出的更多相关文章
- android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题
android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题 android createbitmap函数内存溢出,求解怎样进行处理out of memor ...
- android 管理Bitmap内存 - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Managing Bitmap Memory 管理Bitmap内存 In additi ...
- android文件缓存,并SD卡创建目录未能解决和bitmap内存溢出解决
1.相关代码: 加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ...
- bitmap 内存溢出OOM的解决办法分享
昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...
- 缩放图片,解决bitmap 内存溢出out of memory的问题
很多人在android开发中都遇到了生成bitmap时候内存溢出,也就是out of memory(OOM)的问题,网上对这样的问题的的解决说法不一.笔者作为一个初级开发者,在这里向大家提供一种比较实 ...
- 在Android中解决内存溢出 – OutOfMemoryError
原文链接:http://riggaroo.co.za/fixing-memory-leaks-in-android-outofmemoryerror/ 注:本文在原文基础上在如何判断内存是否泄露方面进 ...
- Android O Bitmap 内存分配
我们知道,一般认为在Android进程的内存模型中,heap分为两部分,一部分是native heap,一部分是Dalvik heap(实际上也是native heap的一部分). Andro ...
- Android设置图片内存溢出(OOM)问题——Android开发进阶之路6
ImageView设置图片必备常识技术: Android设备会给每个应用分配16M的内存空间,如果你设置的图片的比较大且同一个页面有多个时,经常会报OOM错误导致程序奔溃.所以在这种情况下我们必须要对 ...
- Android系统Bitmap内存分配原理与优化
一.前言 笔者最近致力于vivo游戏中心稳定性维护,在分析线上异常时,发现有相当一部分是由OutOfMemory引起.谈及OOM,我们一般都会想到内存泄漏,其实,往往还有另外一个因素--图片,如果对图 ...
随机推荐
- Linux内核中kzalloc函数详解
**************************************************************************************************** ...
- Intel® Media Server Studio Support
复制自网址:https://software.intel.com/en-us/intel-media-server-studio-support/code-samples Code Samples M ...
- Opencv实现两幅图像融合
实现两幅图像线性(不同系数下)的融合涉及到Opencv中两个关键的方法,addWeighted()和createTrackbar() addWeighted方法: 函数原型: void addWeig ...
- SPOJ:Collecting Candies(不错的DP)
Jonathan Irvin Gunawan is a very handsome living person. You have to admit it to live in this world. ...
- [Selenium] Android HTML5 中 Application Cache
HTML5 中引入了 Application Cache,这意味着 Web 应用程序可以被缓存到本地,且可在没有网络的情况下也能访问该 Web 应用程序 Application Cache 在以下3个 ...
- [ZJOI 2008] 骑士
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1040 [算法] 首先 , 题目中互相讨厌的关系构成了一棵基环森林 用拓扑排序找出环 ...
- codeforces round 421 div2 补题 CF 820 A-E
A Mister B and Book Reading O(n)暴力即可 #include<bits/stdc++.h> using namespace std; typedef lon ...
- HBase之一:HBase原理和设计
一.简介 HBase —— Hadoop Database的简称,Google BigTable的另一种开源实现方式,从问世之初,就为了解决用大量廉价的机器高速存取海量数据.实现数据分布式存储提供可靠 ...
- excel的部分使用方法
第一行数据填充下面所有行的快捷键,ctrl+d 两个表关联替换:=VLOOKUP(H1576,Sheet3!$B$2:$C$315,2,0) 取消下拉框:数据>数据有效性>全部清除 快捷选 ...
- 任务13:在Core Mvc中使用Options
13 新建Controllers文件夹,在里面添加HomeController控制器 新建Views文件夹,再新建Home文件夹.再新建Index.cshtml的视图页面 注释上节课的代码,否则我们的 ...