关于android图片的传输,android图…

Android图片上传的两种方式 图片上传,以及带参数的图片上传是Android开发中,很常见的需求。但也是接口联调难度相对比较大的技术实现,本文介绍两种可靠的图片上传方式。一是通过 MultipartBody 来实现;二是通过图片转字符串来实现。 一、通过 MultipartBody 来实现 注意事项和重点,都在注释中说明了,就不再啰嗦: // 定义图片文件解析,下面的 * 代表的是要上传的图片的格式,比如:png、jpg、JPEG等等 MediaType MEDIA_TYPE_PNG = Medi 阅读详情

android客服端上传图片到服务器,使用的xml来传输base64编码后的图片
我使用的是android自带的httpclient来发送post请求的,我也想过自己使用post方式来发送数据,但是,数据在服务器端进行base64解码的时候保存,我也没找出原因,所以就没写出来了

发送post请求就是因为post允许一次传输的数据量大,因为图片经过base64编码后,数据量大,如果采用get或者其他的方式来传输数据,传输效率不过,而且数据量大小受到限制

1.获取android客服端图片

Java代码 复制代码 收藏代码
  1. //对文件的操作
  2. FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
  3. byte buffer[] = StreamUtil.read(in);//把图片文件流转成byte数组
  4. byte[] encod = Base64.encode(buffer,Base64.DEFAULT);//使用base64编码
  1. //对文件的操作   
  2. FileInputStream in new FileInputStream(Environment.getExternalStorageDirectory() "/images/musicmax.png");  
  3. byte buffer[] StreamUtil.read(in);//把图片文件流转成byte数组   
  4. byte[] encod Base64.encode(buffer,Base64.DEFAULT);//使用base64编码  
  //对文件的操作
  FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
  byte buffer[] = StreamUtil.read(in);//把图片文件流转成byte数组
  byte[] encod = Base64.encode(buffer,Base64.DEFAULT);//使用base64编码


2.发送post请求,注意android客服端访问网络记得要加访问网络的权限

Java代码 复制代码 收藏代码
  1. String path ="http://192.168.1.173:7999/videonews/TestServlet";
  2. Map params = new HashMap();//定义一个保存key-value的Map用于保存需要传输的数据
  3. params.put("value", new String(encod));//保存数据到map对象
  4. Log.i(TAG,new String(encod));
  5. if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求
  6. Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
  7. }
  1. String path ="http://192.168.1.173:7999/videonews/TestServlet";   
  2. Map params new HashMap();//定义一个保存key-value的Map用于保存需要传输的数据   
  3.   
  4. params.put("value", new String(encod));//保存数据到map对象   
  5. Log.i(TAG,new String(encod));  
  6. if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求   
  7.  Log.i(TAG, "success :" path "----:decode:----" new String(Base64.decode(encod, Base64.DEFAULT)));  
  8.  
 String path ="http://192.168.1.173:7999/videonews/TestServlet"; 
 Map params = new HashMap();//定义一个保存key-value的Map用于保存需要传输的数据
 
 params.put("value", new String(encod));//保存数据到map对象
 Log.i(TAG,new String(encod));
 if(StreamUtil.sendHttpClientPOSTRequest(path, params, "utf-8")){//使用帮助类来发送HttpClient来发送post请求
  Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
 }


2.服务器端的代码

Java代码 复制代码 收藏代码
  1. String value = request.getParameter("value");//获取value的值
  2. FileOutputStream fileout = new FileOutputStream("c:/music.png");//设置文件保存在服务器的什么位置
  3. fileout.write(com.sun.org.apache.xml.internal.security.utils.Base64.decode(value.getBytes()));//使用base64解码
  4. fileout.close();
  1. String value request.getParameter("value");//获取value的值   
  2.  FileOutputStream fileout new FileOutputStream("c:/music.png");//设置文件保存在服务器的什么位置   
  3.  fileout.write(com.sun.org.apache.xml.internal.security.utils.Base64.decode(value.getBytes()));//使用base64解码   
  4.  fileout.close();  
String value = request.getParameter("value");//获取value的值
 FileOutputStream fileout = new FileOutputStream("c:/music.png");//设置文件保存在服务器的什么位置
 fileout.write(com.sun.org.apache.xml.internal.security.utils.Base64.decode(value.getBytes()));//使用base64解码
 fileout.close();


StreamUtil帮助类里面完整代码

Java代码 复制代码 收藏代码
  1. public class StreamUtil {
  2. public static byte[] read(InputStream in) throws Exception {
  3. ByteArrayOutputStream out = new ByteArrayOutputStream();
  4. if (in != null) {
  5. byte[] buffer = new byte[1024];
  6. int length = 0;
  7. while ((length = in.read(buffer)) != -1) {
  8. out.write(buffer, 0, length);
  9. }
  10. out.close();
  11. in.close();
  12. return out.toByteArray();
  13. }
  14. return null;
  15. }
  16. public static boolean sendHttpClientPOSTRequest(String path, Map params, String encoding) throws Exception{
  17. List param = new ArrayList();
  18. if(params!=null && !params.isEmpty()){
  19. for(Map.Entry entry : params.entrySet()){
  20. param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  21. }
  22. }
  23. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, encoding);
  24. HttpPost post = new HttpPost(path);
  25. // HttpGet get = new HttpGet();
  26. post.setEntity(entity);
  27. DefaultHttpClient client = new DefaultHttpClient();
  28. HttpResponse response = client.execute(post);
  29. if(response.getStatusLine().getStatusCode() == 200){
  30. // response.getEntity().getContent();//获取服务器返回的数据
  31. return true;
  32. }
  33. return false;
  34. }
  35. }
  1. public class StreamUtil  
  2.    
  3.   
  4.  public static byte[] read(InputStream in) throws Exception  
  5.   ByteArrayOutputStream out new ByteArrayOutputStream();  
  6.   if (in != null)  
  7.    byte[] buffer new byte[1024];  
  8.    int length 0;  
  9.    while ((length in.read(buffer)) != -1)  
  10.     out.write(buffer, 0, length);  
  11.     
  12.    out.close();  
  13.    in.close();  
  14.    return out.toByteArray();  
  15.    
  16.   return null;  
  17.   
  18.    
  19.    
  20.  public static boolean sendHttpClientPOSTRequest(String path, Map params, String encoding) throws Exception{  
  21.   List param new ArrayList();  
  22.   if(params!=null && !params.isEmpty()){  
  23.    for(Map.Entry entry params.entrySet()){  
  24.     param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
  25.     
  26.    
  27.   UrlEncodedFormEntity entity new UrlEncodedFormEntity(param, encoding);  
  28.   HttpPost post new HttpPost(path);  
  29. //  HttpGet get new HttpGet();   
  30.   post.setEntity(entity);  
  31.   DefaultHttpClient client new DefaultHttpClient();  
  32.   HttpResponse response client.execute(post);  
  33.   if(response.getStatusLine().getStatusCode() == 200){  
  34. //   response.getEntity().getContent();//获取服务器返回的数据   
  35.    return true;  
  36.    
  37.   return false;  
  38.   
  39.  
public class StreamUtil {
 

 public static byte[] read(InputStream in) throws Exception {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  if (in != null) {
   byte[] buffer = new byte[1024];
   int length = 0;
   while ((length = in.read(buffer)) != -1) {
    out.write(buffer, 0, length);
   }
   out.close();
   in.close();
   return out.toByteArray();
  }
  return null;
 }
 
 
 public static boolean sendHttpClientPOSTRequest(String path, Map params, String encoding) throws Exception{
  List param = new ArrayList();
  if(params!=null && !params.isEmpty()){
   for(Map.Entry entry : params.entrySet()){
    param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
   }
  }
  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(param, encoding);
  HttpPost post = new HttpPost(path);
//  HttpGet get = new HttpGet();
  post.setEntity(entity);
  DefaultHttpClient client = new DefaultHttpClient();
  HttpResponse response = client.execute(post);
  if(response.getStatusLine().getStatusCode() == 200){
//   response.getEntity().getContent();//获取服务器返回的数据
   return true;
  }
  return false;
 }
}

关于自己写post请求的代码,这个代码我测试过,在服务器对传输过来的数据进行base64解码的时候总报错,具体的原因我也没找出来,下面我贴出来代码,希望朋友们帮我找找原因

Java代码 复制代码 收藏代码
  1. /*//对文件的操作 注:此方法测试有问题
  2. FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/images/musicmax.png");
  3. byte buffer[] = StreamUtil.read(in);
  4. byte[] encod = Base64.encode(buffer,Base64.DEFAULT);
  5. StringBuffer sb = new StringBuffer("value=");
  6. URL url = new URL(path);
  7. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  8. conn.setConnectTimeout(5 * 1000);
  9. conn.setRequestMethod("POST");
  10. conn.setDoOutput(true);//允许对外输出数据
  11. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  12. conn.setRequestProperty("Content-Length", (sb.toString().getBytes().length + encod.length) + "");
  13. OutputStream outs = conn.getOutputStream();
  14. outs.write(sb.toString().getBytes());
  15. outs.write(encod);
  16. outs.close();
  17. Log.i(TAG,new String(encod));
  18. if(conn.getResponseCode() == 200){
  19. Log.i(TAG, "success :" + path + "----:decode:----" + new String(Base64.decode(encod, Base64.DEFAULT)));
  20. //下面的代码是测试是否解码后能生成对应的图片没
  21. // FileOutputStream fileout = new FileOutputStream(Environment.getExternalStorageDirectory() + "/images/musicmax1.png");
  22. // fileout.write(Base64.decode(encod, Base64.DEFAULT));
  23. // fileout.close();
  24. }
  1. /*//对文件的操作  注:此方法测试有问题   
  2.                     FileInputStream in new FileInputStream(Environment.getExternalStorageDirectory() "/images/musicmax.png");  
  3.                     byte buffer[] StreamUtil.read(in);  
  4.                     byte[] encod Base64.encode(buffer,Base64.DEFAULT);  
  5.                       
  6.                     StringBuffer sb new StringBuffer("value=");  
  7.                     URL url new URL(path);  
  8.                     HttpURLConnection conn (HttpURLConnection) url.openConnection();  
  9.                       
  10.                     conn.setConnectTimeout(5 1000);  
  11.                     conn.setRequestMethod("POST");  
  12.                     conn.setDoOutput(true);//允许对外输出数据   
  13.                     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
  14.                     conn.setRequestProperty("Content-Length", (sb.toString().getBytes().length encod.length) "");  
  15.                     OutputStream outs conn.getOutputStream();  
  16.                                         outs.write(sb.toString().getBytes());  
  17.                     outs.write(encod);  
  18.                     outs.close();  
  19.                     Log.i(TAG,new String(encod));  
  20.                     if(conn.getResponseCode() == 200){  
  21.                                                 Log.i(TAG, "success :" path "----:decode:----" new String(Base64.decode(encod, Base64.DEFAULT)));  
  22. //下面的代码是测试是否解码后能生成对应的图片没   
  23. //                      FileOutputStream fileout new FileOutputStream(Environment.getExternalStorageDirectory() "/images/musicmax1.png");   
  24. //                      fileout.write(Base64.decode(encod, Base64.DEFAULT));   
  25. //                      fileout.close();   
  26.                     }  
android拍照获得图片及获得图片后剪切设置到ImageView ok,这次的项目需要用到设置头像功能,所以做了个总结,直接进入主题吧。 先说说怎么 使用android内置的相机拍照然后获取到这张照片吧 直接上代码: Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri imageUri = Uri.fromFile(new File(Envir 阅读详情

相关推荐

TP手机端多上传功能完整Demo实战

上传,看起来只是个不起眼的功能模块,但它串联起了客户端交互、性能优化、网络通信、服务端架构、云服务集成与安全保障的完整链条。做好它,不只是让图片传上去那么简单,更是对产品稳定性、用户体验和工程能力的一次全面考验。下次当你轻轻一点“发布”按钮,看着那些照片一张张飞向云端时,不妨想想背后的这整套精密协作的系统——正是这些看不见的努力,才让数字生活如此自然流畅 🌈“伟大的产品,往往藏在最平凡的细节里。—— 一个每天都在修 Bug 的工程师 😄。

weixin_34520664的博客 373

android图片上传服务器demo

利用GridView实现图片批量上传服务器的功能,demo下载下来直接可以运行。其中存在一个bug,bug解决方案请看博客:http://www.cnblogs.com/1925yiyi/p/7419021.html

android实时图片传输

利用socket实时在线传输接收图片,代码简练,实用,仅作技术参考,勿用于商业用途

Android上传手机图片到服务器(这篇你要是看不懂,全网没你可以看懂的了!!!)

Android上传手机图片到服务器,通过okhttp传输到后端,附前后端代码,详细教程

baiqi123456的博客 9735

android 传递图片,Android中传递图片的3种方法

方法零:网上有人想到一种方法,就是先把图片变小,再传递,最后在接收端把图片放大。。这种方法或许可行,但是我认为这很扯,所以无视!方法一:基本思路是先把bitmap转化为byte数组,用Intent传递数组,在将数组转化为bitmapbitmap转化为byte数组的方法:private byte[] Bitmap2Bytes(Bitmap bm){ByteArrayOutputStream baos...

weixin_42134038的博客 985

Android Intent 传输图片

Android中的Intent作为四大组件间通讯的桥梁,支持传输基本数据类型、序列化对象等等 但是要传大图片呢?能不能传呢?下面开始做个试验,先准备一张近500k大小的图片,存放在mipmap下 直接通过Intent的putExtra方法传输,并且打印bitmap大小。 Intent intent = new Intent(this, Main2Activity.class); bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.

优了个秀 3650

android 传递图片,Android用Intent传递图片

1.直接在bundle里面传递drawable图片这个我就不说了,有局限性,只能传递drawable,因为drawable实现了parcelable,bitmap类型的不行。2.图片转成byte数组传递主要说一下这个方法:如果直接是资源bitmap,可以用下面的方法:private byte[] Bitmap2Bytes(Bitmap bm){ByteArrayOutputStream baos ...

weixin_39664456的博客 749

android socket 传输图片,android socket 从客户端到服务器传图片的奇葩现象,求大神指导下解决方法...

当前位置:我的异常网» Android»android socket 从客户端到服务器传图片的奇葩现象android socket 从客户端到服务器传图片的奇葩现象,求大神指导下解决方法www.myexceptions.net网友分享于:2015-08-26浏览:154次android socket 从客户端到服务器传图片的奇葩现象,求大神指导下android客户端代码:public...

weixin_42517649的博客 246

photo transfer app android,Photo Transfer

“秒速传”是一款跨平台图片传输工具,让您在安卓、iPhone、以及电脑间快速无线传。操作简易,传输极速稳定。跟数据线说拜拜~!? 当你的手机储存空间快爆炸时,可以用“秒传”快速备份到电脑,更加方便地整理? 需要在手机上使用电脑图片时,一键拖放即可发送到手机? 聚会活动时,能快速分享高清无损照片给亲朋好友? 更换手机时,轻点一下,就能把旧手机照片批量转移到新手机有些时候,云端硬盘、网络相册也曲折...

weixin_42507868的博客 274

java socket接收图片_java socket传输图片

转自http://chwshuang.iteye.com/blog/1073715服务器端:import java.io.BufferedInputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;impor...

weixin_31062533的博客 944

记录android.util.Base64编码的图片到服务端后解析失败的问题。

android.util.Base64编码的图片到服务端后解析失败; 原因在于使用了 DEFAULT的参数,编码出来的字符串中含有换行(经打印出来看,还是蛮整齐的换行,挺好看); 解决方案也简单,直接用replace("\r\n","");替换掉换行即可。 但最好是在编码的时候使用Base64.NO_WRAP参数,此参数的数值是2; 其注释为: * Encoder flag bit to...

格物穷理 1829

Android图片裁剪终极解决方案

来自:http://www.linuxidc.com/Linux/2012-11/73940.htm 约几个月前,我正为公司的APP在Android手机上实现拍照截而烦恼不已。 上网搜索,确实有不少的例子,大多都是抄来抄去,而且水平多半处于demo的样子,可以用来讲解知识点,但是一碰到实际项目,就漏洞百出。 当时我用大众化的解决方案,暂时性的做了一个拍照截的功能,似乎看起

我以我名,行我道! 1092

Android端上传图片至服务器改变方向的问题

第一次写博客有点紧张,主要是遇到了一个非常有趣的问题,想记录一下。 问题描述 Android端拍个照片,然后上传到服务器上进行处理,没想到出了bug,在服务器接受到照片的时候,这个照片突然就改变了位置,本来竖着的,到服务器上突然横过来了,电脑上查看的时候还没有问题,Android系统8.0,服务器linux,电脑win10。 问题根源 问题的根源很明显就出在系统对于照片的处理上,比如说张三就会横着...

qq_42301464的博客 576

Android 拍照内存闪烁问题

android拍照,传输图片,经常出现内存溢出问题,造成app闪退。 提供如下解决方案。 [java] view plaincopyprint? import java.io.File;  import java.io.FileInputStream;  import java.io.FileNotFoundException;  import j

518

Glide加载http图片

在manifest的application标签中加入android:usesCleartextTraffic=“true”用glide加载图片的时候发现 第一张图片加载不出来 但是第二张可以。或者加入建一个network_security_config.xml文件。对比发现是前缀http和https的不同。

qq_40970620的博客 423

android fresco 流程,GitHub - ly-android/fresco: An Android library for managing images and the memory ...

FrescoFresco is a powerful system for displaying images in Android applications.Fresco takes care of image loading and display, so you don't have to. It will load images from the network, local storag...

weixin_36290287的博客 128

基于Android的IM即时通讯聊天P2P应用设计与实现(源码)

安卓Android IM即时通讯聊天、基于P2P的局域网即时通信应用

swEngineer16的博客 573

android tcp通信传输图片,Android Socket 实现批量图片传输

实现的原理其实也不难,苦于网上没有现成的例子,所以就自己实现了一个。就是在socket 进行图片数据传输的时候,自己去构建一个数据头header ,然后另外一端在读取数据的时候,以这个数据头"start-xxxx"为依据实现image 像的byte 数据读取。然后解析出来。本例子是实现1s 发送一次read 请求。package com.example.zhouyong0701.socketco...

weixin_31046947的博客 905
上一篇: Base64编码在网络图片传输中的应用…
下一篇: android上传图片至服务器
bensantan
博客等级 码龄19年 30粉丝 1037原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值