新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
android開(kāi)發(fā)——獲取網(wǎng)絡(luò)圖片
//通過(guò)地址獲取bitmap public Bitmap getBitmap(String path) { Bitmap bitmap = null; try { //封裝url URL url = new URL(path); //獲得httpurl連接對(duì)象 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //設(shè)置連接超時(shí) connection.setConnectTimeout(5*1000); //設(shè)置請(qǐng)求方法 connection.setRequestMethod("GET"); //判斷請(qǐng)求狀態(tài)碼是否連接成功 if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); //bitmapfactory可以有多種生存bitmap的方法 bitmap = BitmapFactory.decodeStream(is); } } catch (Exception e) { e.printStackTrace(); } return bitmap; } //獲得字節(jié)數(shù)組 public void getBytesByIs(InputStream is) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len ; byte[] data = new byte[1024]; while((len = is.read(data))!=-1){ bos.write(data, 0, len); } byte[] bytes = bos.toByteArray(); }
網(wǎng)站標(biāo)題:android開(kāi)發(fā)——獲取網(wǎng)絡(luò)圖片
標(biāo)題鏈接:http://www.dlmjj.cn/article/ipiiio.html