国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程實現(xiàn)圖片的上傳和下載功能示例

2019-12-12 04:53:27
字體:
供稿:網(wǎng)友

本文實例講述了Android編程實現(xiàn)圖片的上傳和下載功能。分享給大家供大家參考,具體如下:

在實現(xiàn)一個Android的WEB服務(wù)客戶端,比如微博,論壇客戶端時,經(jīng)常會使用到圖片的上傳和下載。在這里介紹如何利用HttpClient實現(xiàn)圖片的上傳和下載功能。

1 圖片上傳:上傳圖片時,首先獲得圖片的路徑,創(chuàng)建文件,并將圖片轉(zhuǎn)化為字節(jié)流寫入到request,并發(fā)送該請求。

客戶端代碼:

File file = new File(imageUrl);String httpUrl = httpDomain+"AddImageServlet"+"?gid="+gid;HttpPost request = new HttpPost(httpUrl);HttpClient httpClient = new DefaultHttpClient();FileEntity entity = new FileEntity(file,"binary/octet-stream");HttpResponse response;try {  request.setEntity(entity);  entity.setContentEncoding("binary/octet-stream");  response = httpClient.execute(request);//如果返回狀態(tài)為200,獲得返回的結(jié)果if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){……//圖片上傳成功}}catch(Exception e){}

服務(wù)器端所做的工作則是接收該字節(jié)流,寫入文件中,并在服務(wù)器中相應(yīng)文件夾中保存該文件,并記錄該文件的路徑,將圖片文件路徑寫入到數(shù)據(jù)庫中保存。

服務(wù)器端代碼:

//獲得新聞idString gid = request.getParameter("gid");String filePath = getRealPath(request) + "http://userpic//";//   定義上載文件的最大字節(jié)int MAX_SIZE = 102400 * 102400;//   聲明文件讀入類DataInputStream in = null;FileOutputStream fileOut = null;//   取得客戶端上傳的數(shù)據(jù)類型String contentType = request.getContentType();if(contentType.indexOf("binary/octet-stream") >= 0){  //   讀入上傳的數(shù)據(jù)  in = new DataInputStream(request.getInputStream());  int formDataLength = request.getContentLength();  // 如果圖片過大  if(formDataLength > MAX_SIZE){    String errormsg=("上傳的文件字節(jié)數(shù)不可以超過" + MAX_SIZE);    out.println(errormsg);    return ;  }//   保存上傳文件的數(shù)據(jù)byte dataBytes[] = new byte[formDataLength];int byteRead = 0;int totalBytesRead = 0;//   上傳的數(shù)據(jù)保存在byte數(shù)組while(totalBytesRead < formDataLength){byteRead = in.read(dataBytes,totalBytesRead,formDataLength);totalBytesRead += byteRead; }String fileName = filePath + gid+".png"; //   檢查上載文件的目錄是否存在File fileDir = new File(filePath);if(!fileDir.exists()){fileDir.mkdirs();}//   創(chuàng)建文件的寫出類fileOut = new FileOutputStream(fileName);//   保存文件的數(shù)據(jù)fileOut.write(dataBytes);fileOut.close();//保存文件的路徑名……

2 圖片下載:首先獲得網(wǎng)絡(luò)圖片的圖片地址,發(fā)送請求后,服務(wù)器將會返回該圖片的字節(jié)流,利用BitmapFactory.decodeStream()方法將字節(jié)流轉(zhuǎn)化為圖片并返回。具體代碼如下:

//獲得網(wǎng)絡(luò)中的圖片public Bitmap getGossipImage(String gid){    String httpUrl = httpDomain+"userpic/"+gid+".png";    Bitmap bitmap = null;    HttpGet httpRequest = new HttpGet(httpUrl);    //取得HttpClient 對象    HttpClient httpclient = new DefaultHttpClient();    try {      //請求httpClient ,取得HttpRestponse      HttpResponse httpResponse = httpclient.execute(httpRequest);      if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){        //取得相關(guān)信息 取得HttpEntiy        HttpEntity httpEntity = httpResponse.getEntity();        InputStream is = httpEntity.getContent();        bitmap = BitmapFactory.decodeStream(is);        is.close();      }else{         Toast.makeText(context, "連接失敗!", Toast.LENGTH_SHORT).show();      }    } catch (ClientProtocolException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    return bitmap;}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 商洛市| 铜山县| 财经| 察隅县| 建宁县| 三台县| 安阳县| 博野县| 武城县| 富顺县| 盘锦市| 海南省| 麦盖提县| 贵德县| 当涂县| 彰武县| 文安县| 廉江市| 泰顺县| 宁晋县| 安达市| 绥江县| 游戏| 吉水县| 和平县| 常宁市| 昌江| 甘泉县| 富平县| 新泰市| 鱼台县| 石阡县| 大埔县| 广德县| 大石桥市| 武邑县| 达拉特旗| 米易县| 宜城市| 论坛| 德格县|