1,httpClient
// 获取二维码
// 返回bitmap或null
public static Bitmap get2DCodePic(String box_id) {
// todo
// Toast.makeText(getApplicationContext(), "todo\n", Toast.LENGTH_LONG)
// .show();
String result = null;
BufferedReader reader = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(Tools.GET_2DCODE_PIC + box_id));
HttpResponse response = client.execute(request);
reader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer strBuffer = new StringBuffer("");
String line = null;
while ((line = reader.readLine()) != null) {
strBuffer.append(line);
}
result = strBuffer.toString();
// Toast.makeText(getApplicationContext(), result,
// Toast.LENGTH_LONG)
// .show();
Bitmap bitmap = Tools.getHttpBitmap(result);
if (null != bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(0.7f, 0.7f); // 长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2,httpURLConnection
/*
* function: input:图片url output:图片bitmap
*/
public static Bitmap getHttpBitmap(String url) {
Log.d(Tools.LOG_TAG, url);
URL myFileURL;
Bitmap bitmap = null;
try {
myFileURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) myFileURL
.openConnection();
// 设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
conn.setConnectTimeout(6000);
// 连接设置获得数据流
conn.setDoInput(true);
// 不使用缓存
conn.setUseCaches(false);
// 这句可有可无,没有影响
// conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
3,第三方库(imageloader,volley)
待续。。。