亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Volley -- 使用volley上傳圖片

標簽:
Android

/* 

 实现思路

 1.将要上传的图片转为 Bitmap 类型 

 2.将Bitmap类型的图片编译成 Base64 的字节流

 3.通过 Volley 的post方法上传上去

 */

 // 实现第一步 将存储卡中的文件转为Bitmap 类型

 FileInputStream fis = new FileInputStream("图片的路径");

 Bitmap bitmap = BitmapFactory.decodeStream(fis); 

 // 实现第二步 Bitmap 转 Base64 的字节流 

 public static String bitmapToBase64(Bitmap bitmap) {

 String result = null; ByteArrayOutputStream baos = null;

 try {

 if (bitmap != null) { 

 baos = new ByteArrayOutputStream();

 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 

 baos.flush(); baos.close(); byte[] bitmapBytes = baos.toByteArray();

 result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT); 

 }

 } catch (IOException e) { 

 e.printStackTrace(); 

 } finally {

 try { 

 if (baos != null) {

 baos.flush(); baos.close(); 

 } 

 } catch (IOException e) {

 e.printStackTrace();

 } 

 }

 return result;

 }

 // 然后使用上一个博客提到的Volley post上传参数一样上传上去就好了 

 String user_img =bitmapToBase64(bitmap); 

 requestQueue = Volley.newRequestQueue(this); 

 stringRequest = new StringRequest(Request.Method.POST, "请求地址", new Response.Listener<String>() { 

 @Override 

 public void onResponse(String response) {

 // 返回的json参数 response 

 } 

 }, new Response.ErrorListener() { 

 @Override 

 public void onErrorResponse(VolleyError error) {

 // 请求错误就会进入这里 

 } }) {

 //////////////// 在这里可以获取Cookie或者是设置Cookie

 // 像服务器post提交参数的方法

 @Override 

 protected Map<String, String> getParams() {

 // 在这里设置需要post的参数 

 HashMap<String, String> map = new HashMap<String, String>(); 

 map.put("user_img", user_img); return map; 

 } 

 };

 requestQueue.add(stringRequest);

原文链接:http://www.apkbus.com/blog-722618-72771.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消