android 大图片处理方法,android 上传图片过大处理
在开发Android应用时,会经常上传图片到服务器,会经常出现上传图片失败和时间过长的问题
为了解决这个问题,决定把照片进行压缩后上保存后本地缓存文件夹后再上传
使用方法:
//压缩后得到新的图片路径
BitmapUtils.compressImageUpload
// 上传成功后删除缓存文件
BitmapUtils.deleteCacheFile()
public class BitmapUtils {
/**
* 质量压缩方法
*
* @param image
* @return
*/
private static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
/**
* 图片按比例大小压缩方法(根据路径获取图片并压缩)
*
* @param srcPath
* @return
*/
private static Bitmap getImage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;// 这里设置高度为800f
float ww = 480f;// 这里设置宽度为480f
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
}
/**
* 将压缩的bitmap保存到SDCard卡临时文件夹,用于上传
*
* @param filename
* @param bit
* @return
*/
private static String saveMyBitmap(String filename, Bitmap bit) {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/laopai/";
String filePath = baseDir + filename;
File dir = new File(baseDir);
if (!dir.exists()) {
dir.mkdir();
}
File f = new File(filePath);
try {
f.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(f);
bit.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return filePath;
}
/**
* 压缩上传路径
* @param path
* @return
*/
public static String compressImageUpload(String path) {
String filename = path.substring(path.lastIndexOf("/") + 1);
Bitmap image = getImage(path);
return saveMyBitmap(filename, image);
}
/**
* 清除缓存文件
*/
public static void deleteCacheFile(){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/laopai/");
RecursionDeleteFile(file);
}
/**
* 递归删除
*/
private static void RecursionDeleteFile(File file){
if(file.isFile()){
file.delete();
return;
}
if(file.isDirectory()){
File[] childFile = file.listFiles();
if(childFile == null || childFile.length == 0){
file.delete();
return;
}
for(File f : childFile){
RecursionDeleteFile(f);
}
file.delete();
}
}
}
android 大图片处理方法,android 上传图片过大处理相关推荐
- android 矢量图片使用方法,Android中的矢量图
概述 VectorDrawable是通过XML文件中的一系列点,线和曲线及其相关颜色信息定义的. 使用VectorDrawable的主要优点是图像可扩展性. 它可以缩放而不损耗显示质量,这意味着相同的 ...
- android颜色值的表示方法android:background=#FFFFFFFF的意思
android颜色值的表示方法 android:background="#FFFFFFFF"的意思 Android中的颜色值是通过红(Red).绿(Green).蓝(Blue)三原 ...
- android 自动相机 图片旋转问题,Android部分手机拍照后获取的图片被旋转问题的解决方法...
调用Android系统拍照功能后,三星手机拍摄后的照片被旋转了90度,横着拍给你变成竖的,竖的拍给你变成横的.其它品牌的手机都是正常的,就三星出现这个怪事. 在Android适配上,我原来一直以为国内 ...
- unity调用 Android 分享图片文字 方法 不需要第三方sdk 兼容android7.0+
直接说方法把,在Android Studio中 新建一个工程,新建时选择Add No Activity,包名跟unity中设置的一样比如这里用 come.demo.share 在res目录下新建个文件 ...
- Android 提取图片背景方法(纯色)
实现目标:提取随意一张图片的7种颜色(图片拥有7种以上的颜色),可作为背景色使用. 布局(自己增添toolbar): <?xml version="1.0" encoding ...
- android camera 图片被旋转,android 部分手机Camera 拍照 图片被旋转90度的解决方法
部分手机拍照完后 图片旋转角度90度,目前解决方法普遍都是通过图片路径,获取图片信息判断角度,然后转换角度.如下: /** * 读取图片属性:旋转的角度 * @param path 图片绝对路径 * ...
- android grideview 图片png透明,Android完美解决GridView异步加载图片和加载大量图片时出现Out Of Memory问题...
众所周知,我们在使用GridView或者ListView时,通常会遇到两个棘手的问题: 1.每个Item获取的数据所用的时间太长会导致程序长时间黑屏,更甚会导致程序ANR,也就是Application ...
- android微信图片选择框架,Android仿微信图片选择器ImageSelector使用详解
今天给大家介绍一个仿微信的图片选择器:ImageSelector.ImageSelector支持图片的单选.限数量的多选和不限数量的多选.支持图片预览和图片文件夹的切换.在上一篇文章 <Andr ...
- Android解析xml的方法,Android中解析XML格式数据的方法
XML介绍:Extensible Markup Language,即可扩展标记语言 一.概述 Android中解析XML格式数据大致有三种方法: SAX DOM PULL 二.详解 2.1 SAX S ...
最新文章
- 要懂得利用和筛选友情链接
- minecraft_MineCraft和堆外内存
- vb中filecopy拷贝文件
- FMDB数据库框架的是使用
- React-Native入门(2)-简单阐述跳转
- STL_string.vector中find到的iterator的序号
- (4)回归决策树_预测波士顿房价
- Android下ping的简单工具类
- 算法导论———归并排序(JAVA、Python)
- 在react-native fetch中 then res res.json 是什么意思
- tensorflow padded_batch的注意事项
- 行尸走肉第一季/全集The Walking Dead迅雷下载
- 【蓝桥杯每日一练:小z的序列游戏-k】
- 苹果手机数据能恢复吗
- 考研英语单词-近义词分类-Second Day
- python源码实例之根据生日预测星座和属相以及属相对应年份(附源码 可供学习)
- 使用公式使用户输入年利率,贷款额度和支付年数,计算支付额度
- 电脑桌面计算机打开很慢,我的电脑打开很慢怎么办
- Go十大常见错误第7篇:不使用-race选项做并发竞争检测
- iis 发生意外错误0x8ffe2740.