之前從網上獲取圖片並保存到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();
}
使用下面的這個方法之后圖片可能正常下載並顯示