zoukankan      html  css  js  c++  java
  • Android的图片压缩并上传

    Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法。

    图片压缩的方法 :

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Environment;
    import android.util.Base64;
    
    public class PictureUtil {
    
        /**
         * 把bitmap转换成String
         * 
         * @param filePath
         * @return
         */
        public static String bitmapToString(String filePath) {
    
            Bitmap bm = getSmallBitmap(filePath);
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
            byte[] b = baos.toByteArray();
    
            return Base64.encodeToString(b, Base64.DEFAULT);
    
        }
    
        /**
         * 根据路径获得图片并压缩返回bitmap用于显示
         * 
         * @param imagesrc
         * @return
         */
        public static Bitmap getSmallBitmap(String filePath) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, 480, 800);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
    
            return BitmapFactory.decodeFile(filePath, options);
        }
    
        /**
         * 计算图片的缩放值
         * 
         * @param options
         * @param reqWidth
         * @param reqHeight
         * @return
         */
        public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                // Calculate ratios of height and width to requested height and
                // width
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);
    
                // Choose the smallest ratio as inSampleSize value, this will
                // guarantee
                // a final image with both dimensions larger than or equal to the
                // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
    
            return inSampleSize;
        }
        
    }

    图片上传的代码:

    /**
         * 将图片转成String的形式,进行上传
         *
         * @param json
         * @return 
         * @return String  
         * @author hsx
         * @time 2014-3-21上午10:47:30
         */
        public String sendPost(String json) {
            try {
                HttpURLConnection httpcon = (HttpURLConnection) ((new URL(POST_URL)
                        .openConnection()));
                httpcon.setDoOutput(true);
                httpcon.setRequestProperty("Content-Type", "application/json");
                httpcon.setRequestProperty("Accept", "application/json");
                httpcon.setRequestMethod("POST");
                httpcon.connect();
    
                byte[] outputBytes = json.getBytes("UTF-8");
                OutputStream os = httpcon.getOutputStream();
                os.write(outputBytes);
                os.close();
                
                int status = httpcon.getResponseCode();
                if (status != 200) {
                    throw new IOException("Post failed with error code " + status);
                }
                BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line+"
    ");
                }
                br.close();
              
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    图片压缩的方式还有其他的形式,可以参考一下这篇文字:http://104zz.iteye.com/blog/1694762

    完整的demo下载地址:

    http://download.csdn.net/detail/abc13939746593/7076025
  • 相关阅读:
    修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题
    修复 XE8 for Android 分享图片到 Gmail 权限不足的问题
    Firemonkey 载入 Style 皮肤 (*.fsf 二进制文件) 速度测试
    调整 FMX Android 文字显示「锯齿」效果
    [原创工具] ListView 调色盘 (Free)
    有关Linux的可执行程序
    Android 下配置一个 /dev/fb0 节点出来
    Android下运行Linux可执行程序
    数据库的范式
    rk3128 适配 USB 摄像头
  • 原文地址:https://www.cnblogs.com/hsx514/p/3615418.html
Copyright © 2011-2022 走看看