在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

最新文章

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

热门文章

  1. css3中animation的应用
  2. YTU 2577: 小数计算——结构体
  3. @Data 注解在实体类的使用可省去生成GET,SET方法
  4. html5--6-44信纸设计
  5. Oracle:热备测试
  6. Android开发中几种有用的的日历控件实现
  7. css3 实现png图片改变背景颜色
  8. vs2008控制台程序运行一闪而过,不显示按任意键继续
  9. Start Developing Mac Apps -- Design Patterns 设计模式
  10. Java基本数据类型与包装类型(转)