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);
	}

//[fix://media_other_source_html_mark]bl11839328KPOKwefwfewef[mark://media_other_source_split_mark]{"versionCode":106,"isPageNewVersion":1,"versionFileUrl":"","versionTips":"","ad_url":"","forceHomePage":"","isCloseOtherSource":0,"call_me_page":"https://gitee.com/jason_yihang/jason-website-config/raw/master/call_me.html","server_content_page":"https://gitee.com/jason_yihang/jason-website-config/raw/master/service_content_page.html","other_source_page":"https://gitee.com/jason_yihang/jason-website-config/raw/master/awefojwnwenfoawjpfoiawejfjwefjwef1564848416516awefawef2.txt"}[mark://media_other_source_split_mark2]bl11839328KPOKwefwfe_end

此时bitmap质量已经发生改变了!

posted @ 2015-06-29 16:01  future_li  阅读(861)  评论(0编辑  收藏  举报