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 inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, 480, 800);// Decode bitmap with inSampleSize setoptions.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 imagefinal 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// widthfinal 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
完整的demo下载地址:
http://download.csdn.net/detail/abc13939746593/7076025
转载于:https://www.cnblogs.com/hsx514/p/3615418.html
Android的图片压缩并上传相关推荐
- Android实现图片压缩并上传到服务器
最近公司又叫开发了一个新项目,这个项目中上传图片用的蛮多的,于是整理一下,记录自己的心得体验 刚入手的时候,对于图片的大小还没有概念,(以前上传图片都是用户头像,对大小没什么要求),心想之间上传就是了 ...
- js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传
js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传 图片上传目录 js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传 html代码 js代码 html代码 <input type ...
- android 快速实现图片压缩与上传
由于最近项目更新功能比较的忙,也没时间去整理自己的知识点和管理自己的博客.在android对手机相册中的图片的压缩和上传到服务器上,这样的功能在每个app开发中都会有这样的需求.所以今天就对andro ...
- Bmob+Luban(鲁班)压缩图片实现相册选择图片压缩后上传到Bmob后台Glide加载图片显示到本地
源代码已上传CSDN:https://download.csdn.net/download/qq_16519957/11068345 因为本章需要跟前面的知识结合起来看所以就做了一个前面链接方便大家查 ...
- 返回图片_Vue 图片压缩并上传至服务器
日常开发中经常会遇到上传图片的需求,随着手机的蓬勃发展,现在拍出来的照片分辨率越来越高,随之带来的问题就是图片占用空间越来越大,如果我们直接上传图片可能就会浪费很大一笔资源,本文主要讲解基于 Vue ...
- Vue 图片压缩并上传至服务器
本文主要讲解基于 Vue + Vant ,实现移动端图片选择,并用 Canvas 压缩图片,最后上传至服务器.还会封装一个工具类,方便直接调用. 一.工具类封装 废话不多说先上代码,封装一个 Comp ...
- js图片压缩java上传,JS实现异步上传压缩图片
摘要: 使用iframe来处理异步上传图片,在现在这个时代来说,多多少少都有点落后了!单单就凭AJAX和JS就不能做到异步上传图片了吗? 先看调用页面: 选择图片 var img; $("i ...
- 前端获取图片压缩后上传给后台
在做移动端图片上传的时候,用户传的都是手机本地图片,而本地图片一般都相对比较大,拿iphone6来说,平时拍很多图片都是一两M的,如果直接这样上传,那图片就太大了,如果用户用的是移动流量,完全把图片上 ...
- js图片压缩并上传?
js: var eleFile = document.querySelector('#file'); // 压缩图片需要的一些元素和对象 var reader = new FileReader(); ...
最新文章
- 来自damon的zencart二次开发教程-2.2登录模块分析
- 【论文解读】NLP重铸篇之Word2vec
- 任务发布页面html,HTML5 Todo List(待办事项/任务列表管理界面)
- 获取文件的MIME类型
- JavaScript 你必须了解的主流趋势!
- 摆脱臃肿--Unity3D安卓包减肥秘笈
- UnityShader14.1:透明效果实现(下)
- java的编程规范_JAVA编程规范-OOP规范
- 黑马程序员---java基础------------------基础中的基础学习
- 《Python编程快速上手》8.9 实践项目
- mfc radio group 设置
- Atitit 财政支出学习心得 attilax总结
- 内网穿透和路由器端口映射什么区别?
- http://bt.neu6.edu.cn/forum.php,分享一些教育网访问较快的站点~
- Android 没有出现menu 按键显示 解决
- php匹配ubb,UBB类 php UBB 解析实现代码
- python派森编程软件_《派森》(Python)
- 大学计算机专业找对象,单身率最高的大学专业是什么?这5个专业为什么成脱单最难专业...
- SQL Server基础操作(此随笔仅作为本人学习进度记录七 !--函数)
- 机器视觉-工业相机篇
热门文章
- Mysql报错Fatal error occurred in the transaction branch - check your data for consistency
- 《Android Studio开发实战 从零基础到App上线(第2版)》出版后记
- C# 封装的功能强大的中国农历日历操作类的代码
- TeamCity : .NET Core 插件
- 《机器学习系统设计:Python语言实现》一2.2 IPython控制台
- 点击页面的悬浮窗口实现随意拖动
- sqoop 把 hdfs 和关系型数据库 (mysql等)互导
- WPF入门教程系列十六——WPF中的数据绑定(二)
- About Undefined Behavior[译文]
- Linux中的进程调度(六)