android 防止bitmap 内存溢出
2024-10-21 06:12:00
在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
最新文章
- 安装Impala
- hdu3087 LCA + 暴力
- Failed to apply plugin [id 'com.android.application'] 和 Could not find com.android.tools.build:gradle:2.XX的最正确的解决方法
- Microsoft SQL Server,错误: 229 解决方案
- highcharts联合jquery ajax 后端取数据
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
- C++ AfxBeginThread1
- 【Android】Android ListViewAnimations分析
- var 的用法
- yii [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Unable to resolve the request ";favicon.ico";.'
- perl return和break
- hdoj 2102 A计画 【BFS】
- PB导出规定格式DBF文件
- 转 Caffe学习系列(4):激活层(Activiation Layers)及参数
- 打印十字图 JAVA 递归实现
- 亚马逊AWS学习——VPC里面几个概念的关系
- java数据结构之链表(java核心卷Ⅰ读书笔记)
- python 获取list的下标
- razor视图使用三元表达式
- Raspbian开启root账户