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

    }
複製代碼

 

修改之後的 (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();      
    }
複製代碼

 

使用下面的這個方法之後圖片可能正常下載並顯示

 

發佈了6 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章