Android下的图片压缩及图片和视频的上传

来源:互联网 发布:汉语网络在线老师招聘 编辑:程序博客网 时间:2024/06/07 17:19

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+"\n");            }            br.close();                      return sb.toString();        } catch (IOException e) {            e.printStackTrace();        }        return null;    }

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

下载:http://download.csdn.net/detail/jdsjlzx/9496302

0 0