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

转载于:https://www.cnblogs.com/futureli/p/4607692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值