zoukankan      html  css  js  c++  java
  • (转)Android学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能


    文章转载自:
    作者:RyaneLee
    链接:http://www.jianshu.com/p/940fc7ba39e1

    让我头疼一个星期的图片批量上传服务器的问题最后受这篇文章的作者启发而解决,自己之前一直执着于通过uri地址找到图片然后上传图片,却没想过直接上传图片本身。感谢作者的博客和启发。

    前言

    (转载请注明出处,谢谢!)

    最近在做一个小项目,项目中要实现上传图片到服务器,而这个例子是实现图片的尺寸压缩,将获取到的压缩图片转为流,然后使用Async-Http开源框架实现图片流的上传,然后在服务器端将流写入本地。

    效果图


    安卓端

    服务器端

    解决

    安卓端

    一、点击加载本地相册选择图片,然后加载到ImageView中去,并获取图片地址以供后面图片上传时使用。

     1         //点击图片打开选择图片界面
     2     photo.setOnClickListener(new OnClickListener() {
     3 
     4         @Override
     5         public void onClick(View v) {
     6             //使用Intent触发选择Action
     7             Intent intent = new Intent(Intent.ACTION_PICK, null);
     8             //打开系统提供的图片选择界面
     9             intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    10             //传参以在返回本界面时触发加载图片的功能
    11             startActivityForResult(intent, 0x1);
    12         }
    13     });
     

     1 //在选择图片后返回本界面调用此方法
     2 @Override
     3 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     4     if (requestCode == 0x1 && resultCode == RESULT_OK) {
     5         if (data != null) {
     6 
     7             ContentResolver resolver = getContentResolver();
     8             try {
     9                 // 获取圖片URI
    10                 Uri uri = data.getData();
    11                 // 将URI转换为路径:
    12                 String[] proj = { MediaStore.Images.Media.DATA };
    13                 Cursor cursor = managedQuery(uri, proj, null, null, null);
    14                 //  这个是获得用户选择的图片的索引值
    15                 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    16                 cursor.moveToFirst();
    17                 // 最后根据索引值获取图片路径
    18                 photoPath = cursor.getString(column_index);
    19 
    20                 // 压缩成800*480
    21                 bitmap = BitmapUtils.decodeSampledBitmapFromFd(photoPath, 480, 800);
    22                 // 设置imageview显示图片
    23                 photo.setImageBitmap(bitmap);
    24                 // 设置textview显示路径
    25                 path.setText(photoPath);
    26             } catch (Exception e) {
    27                 e.printStackTrace();
    28             }
    29 
    30         }
    31     }
    32 }
     

    二、图片压缩。在我们需要上传图片时,我们不可能直接把原图上传到服务器,因为一般图片的大小都超过3,4M,所以我们在上传之前需要对图片进行压缩,我是把图片压缩放到了一个工具类,主要是对图片进行尺寸压缩。

     1 public class BitmapUtils {
     2 
     3     private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
     4         final int height = options.outHeight;
     5         final int width = options.outWidth;
     6         int inSampleSize = 1;
     7         if (height > reqHeight || width > reqWidth) {
     8             //首先获取原图高度和宽度的一半
     9             final int halfHeight = height / 2;
    10             final int halfWidth = width / 2;
    11             //循环,如果halfHeight和halfWidth同时大于最小宽度和最小高度时,inSampleSize乘2,
    12             //最后得到的宽或者高都是最接近最小宽度或者最小高度的
    13             while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
    14                 inSampleSize *= 2;
    15             }
    16         }
    17         return inSampleSize;
    18     }
    19 
    20     /**
    21      * 根据Resources压缩图片
    22      * 
    23      * @param res
    24      * @param resId
    25      * @param reqWidth
    26      * @param reqHeight
    27      * @return
    28      */
    29     public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
    30         final BitmapFactory.Options options = new BitmapFactory.Options();
    31         options.inJustDecodeBounds = true;
    32         BitmapFactory.decodeResource(res, resId, options);
    33         options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    34         options.inJustDecodeBounds = false;
    35         Bitmap src = BitmapFactory.decodeResource(res, resId, options);
    36         return src;
    37     }
    38 
    39     /**
    40      * 根据地址压缩图片
    41      * 
    42      * @param pathName
    43      * @param reqWidth 最小宽度
    44      * @param reqHeight 最小高度
    45      * @return
    46      */
    47     public static Bitmap decodeSampledBitmapFromFd(String pathName, int reqWidth, int reqHeight) {
    48         final BitmapFactory.Options options = new BitmapFactory.Options();
    49         // 若要对图片进行压缩,必须先设置Option的inJustDecodeBounds为true才能进行Option的修改
    50         options.inJustDecodeBounds = true;
    51         // 第一次decodeFile是获取到options.outHeight和options.outWidth
    52         BitmapFactory.decodeFile(pathName, options);
    53         // options.inSampleSize是图片的压缩比,例如原来大小是100*100,options.inSampleSize为1,则不变,
    54         // options.inSampleSize为2,则压缩成50*50
    55         options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    56         // 重新设置options.inJustDecodeBounds为false,不能修改option
    57         options.inJustDecodeBounds = false;
    58         // 根据options重新加载图片
    59         Bitmap src = BitmapFactory.decodeFile(pathName, options);
    60         return src;
    61     }
    62 }
     

    三、将Bitmap转为String,同时使用BASE64加密,使传输更加安全,这是我的工具类,里面再一次进行压缩是质量压缩,压缩比例是30%,具体自己调

     1 public class Utils {
     2     // 将bitmap转成string类型通过Base64
     3     public static String BitmapToString(Bitmap bitmap) {
     4 
     5         ByteArrayOutputStream bao = new ByteArrayOutputStream();
     6         // 将bitmap压缩成30%
     7         bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bao);
     8         // 将bitmap转化为一个byte数组
     9         byte[] bs = bao.toByteArray();
    10         // 将byte数组用BASE64加密
    11         String photoStr = Base64.encodeToString(bs, Base64.DEFAULT);
    12         // 返回String
    13         return photoStr;
    14     }
    15 }
     

    四、通过Async-Http实现图片上传

     1  public void upload(String url) {
     2         // 将bitmap转为string,并使用BASE64加密
     3         String photo = Utils.BitmapToString(bitmap);
     4         // 获取到图片的名字
     5         String name = photoPath.substring(photoPath.lastIndexOf("/")).substring(1);
     6         // new一个请求参数
     7         RequestParams params = new RequestParams();
     8         // 将图片和名字添加到参数中
     9         params.put("photo", photo);
    10         params.put("name", name);
    11         AsyncHttpClient client = new AsyncHttpClient();
    12         // 调用AsyncHttpClient的post方法
    13         client.post(url, params, new AsyncHttpResponseHandler() {
    14 
    15             @Override
    16             public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
    17                 Toast.makeText(getApplicationContext(), "上传失败!", Toast.LENGTH_SHORT).show();
    18             }
    19 
    20             @Override
    21             public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
    22                 Toast.makeText(getApplicationContext(), "上传成功!", Toast.LENGTH_SHORT).show();
    23             }
    24         });
    25     }

    服务器端

    一、创建一个新的Servlet,在doget方法里面实现

     1 public class UpLoadPhotoServlet extends HttpServlet {
     2     /**
     3      * 
     4      */
     5     private static final long serialVersionUID = 1L;
     6 
     7     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     8         request.setCharacterEncoding("utf-8");
     9         response.setCharacterEncoding("utf-8");
    10         response.setContentType("text/html");
    11         // 获取文件名
    12         String name = request.getParameter("name");
    13         // 获取图片
    14         String photo = request.getParameter("photo");
    15         // 将传进来的图片的string格式进行处理
    16         byte[] bs = new BASE64Decoder().decodeBuffer(photo);
    17         // 写到E盘Img文件夹下的a.jpg文件。注:Img文件夹一定要存在
    18         FileOutputStream fos = new FileOutputStream("E:/Img/" + name);
    19         fos.write(bs);
    20         fos.flush();
    21         fos.close();
    22 
    23         PrintWriter writer = response.getWriter();
    24         writer.print("上传成功");
    25     }
    26 
    27     @Override
    28     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    29         doGet(req, resp);
    30     }
    31 }
     

    Demo地址

    http://download.csdn.net/detail/ljcitworld/9548619



  • 相关阅读:
    云计算安全概述
    快照技术
    存储可靠性技术之--备份
    存储可靠性技术之 --RAID
    存储方式
    存储技术
    CentOS安装setup
    CentOS7安装iptables防火墙
    CentOS 7.0下使用yum安装MySQL
    The APR based Apache Tomcat Native library
  • 原文地址:https://www.cnblogs.com/1925yiyi/p/7531307.html
Copyright © 2011-2022 走看看