Android下的图片压缩及图片和视频的上传
来源:互联网 发布:手机淘宝宝贝怎么分类 编辑:程序博客网 时间:2024/05/18 17:57
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-图片的上传及加载
- 二叉树的遍历(非递归和递归实现)
- BeeFramework环境搭建
- 设置tabBar背景颜色
- 跳板机&攻击方式
- HDU1536 S-Nim(博弈入门暴力打SG表)
- Android下的图片压缩及图片和视频的上传
- Android EventBus源码解析
- 使用vs2010创建一个简单c程序过程
- spring mvc 检查添加的某个字段是否在数据库里已经存在
- 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较
- 用Maven构建Mahout项目
- doc简单命令备注
- 关于android studio中标题栏的隐藏和显现实现。
- 防止不记得