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
- Android下的图片压缩及图片和视频的上传
- Android下的图片压缩及图片和视频的上传
- android下的图片裁剪和压缩
- android中图片上传时的图片压缩
- Android webview实现上传图片的效果(图片压缩)
- iOS 视频图片压缩上传
- 拍照上传图片的压缩
- android图片压缩上传
- Android 图片压缩上传
- android 图片压缩上传
- Android 图片压缩上传
- Android 下实用的图片压缩
- android 的图片压缩
- Android 下获取视频和图片的缩略图
- Android 图片的三级缓存 及 图片压缩
- Android 图片的三级缓存 及 图片压缩
- android选取本地图片及关于图片压缩上传问题
- Android-图片的上传及加载
- 程序员编码大赛第三届编码大赛第三题
- linux的查询时间实现秒定时+gettimeofday()函数的用法
- POJ 3295 Tautology(递归、构造)
- C++中map容器的说明和使用技巧
- 1003 of search
- Android下的图片压缩及图片和视频的上传
- Android API Guides---Text and Input
- Android 自定义控件onTouch事件浅析和个人总结
- 何时使用MINA
- LIS LCS LCIS (主要过一遍,重在做题)
- 需求传递
- linux常用命令
- oracle学习 第四章 单行函数 ——01
- lwip C/S模型调试 抓包看到常见的错误状态