android中使用BitmapFactory的decodeStream()方法解码图片失败问题
之前从网上获取图片并保存到Sd卡中是用的BitmapFactory的decodeStream()方法,在2.3版及以上版本下没有问题,但是底于2.3的版本就会出问题.
代码debug的时候不出问题,但是直接运行就是出错,从网上查了查,有的说是网速不太好的情况下,会获取不了图片,有的说是低版本的API上会出现解码问题
之前的代码 (BitmapFactory.decodeStream):
Bitmap bitmapFact = BitmapFactory.decodeStream(getImageStream(picUrl));
saveFile(bitmapFact, pic.getPicName(),
pic.getModifyTime());
/* *
* 从网络上获取图片,并返回输入流
*
* @param path
* 图片的完整地址
* @return InputStream
* @throws Exception
*/
public InputStream getImageStream(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout( 10 * 1000);
conn.setConnectTimeout( 10 * 1000);
conn.setRequestMethod( " GET ");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return conn.getInputStream();
}
return null;
}
/* *
* 保存文件
*
* @param bm
* 位图
* @param fileName
* 文件名
* @param modifyTime
* 修改时间
* @throws IOException
*/
public void saveFile(Bitmap bm, String fileName, String modifyTime)
throws IOException {
File myCaptureFile = new File(ALBUM_PATH + fileName); // 创建文件
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}
saveFile(bitmapFact, pic.getPicName(),
pic.getModifyTime());
/* *
* 从网络上获取图片,并返回输入流
*
* @param path
* 图片的完整地址
* @return InputStream
* @throws Exception
*/
public InputStream getImageStream(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout( 10 * 1000);
conn.setConnectTimeout( 10 * 1000);
conn.setRequestMethod( " GET ");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return conn.getInputStream();
}
return null;
}
/* *
* 保存文件
*
* @param bm
* 位图
* @param fileName
* 文件名
* @param modifyTime
* 修改时间
* @throws IOException
*/
public void saveFile(Bitmap bm, String fileName, String modifyTime)
throws IOException {
File myCaptureFile = new File(ALBUM_PATH + fileName); // 创建文件
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}
修改之后的 (BitmapFactory.decodeByteArray):
Bitmap bitmapFact =
this.getImage(picUrl);
saveFile(bitmapFact, pic.getPicName(),pic.getModifyTime());
public Bitmap getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout( 10 * 1000);
conn.setConnectTimeout( 10 * 1000);
conn.setRequestMethod( " GET ");
InputStream is = null;
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = conn.getInputStream();
} else {
is = null;
}
if ( is == null){
throw new RuntimeException( " stream is null ");
} else {
try {
byte[] data=readStream( is);
if(data!= null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
is.close();
return null;
}
}
/*
* 得到图片字节流 数组大小
* */
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[ 1024];
int len = 0;
while( (len=inStream.read(buffer)) != - 1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
saveFile(bitmapFact, pic.getPicName(),pic.getModifyTime());
public Bitmap getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout( 10 * 1000);
conn.setConnectTimeout( 10 * 1000);
conn.setRequestMethod( " GET ");
InputStream is = null;
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = conn.getInputStream();
} else {
is = null;
}
if ( is == null){
throw new RuntimeException( " stream is null ");
} else {
try {
byte[] data=readStream( is);
if(data!= null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
is.close();
return null;
}
}
/*
* 得到图片字节流 数组大小
* */
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[ 1024];
int len = 0;
while( (len=inStream.read(buffer)) != - 1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
使用下面的这个方法之后图片可能正常下载并显示