当前位置:首页 » 安卓系统 » android压缩上传图片

android压缩上传图片

发布时间: 2023-04-23 08:07:05

1. Android图片压缩与优化的几种方式

1.使用的图片最好用png格式

2.如果你的图片仍然大的话可以在项目里面点击右键选择convert to Webp,转成webp格式图片可以变小很多!

2. android图片压缩避免OOM

简单吹下牛:很多app都会要加载图片,但是如果不压缩图片就很容易OOM,

个人看来OOM 出现原因总的来说分为两种:

一种是内存溢出(好像在扯淡,OOM本身就是内存溢出)

另一种是:图片过大,一个屏幕显示不完全造成,似乎也是一。。 如有错误纯属扯淡;

为了避免上面的情况:加载图片的时候可以进行压缩,上传的时候要可以进行压缩,在图片不可见的时候进行回收(onDetach()),再吹一句 用了fresco+压缩之后加载图片完全没问题了。

一、质量压缩方法:

privateBitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos =newByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG,100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中

intoptions =100;

while( baos.toByteArray().length /1024>100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中

options -=10;//每次都减少10

}

ByteArrayInputStream isBm =newByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中

Bitmap bitmap = BitmapFactory.decodeStream(isBm,null,null);//把ByteArrayInputStream数据生成图片

returnbitmap;

}

二、图片按比例大小压缩方法(根据Bitmap图片压缩)

privateBitmap comp(Bitmap image) {

ByteArrayOutputStream baos =newByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG,100, baos);

if( baos.toByteArray().length /1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG,50, baos);//这里压缩50%,把压缩后的数据存放到baos中

}

ByteArrayInputStream isBm =newByteArrayInputStream(baos.toByteArray());

BitmapFactory.Options newOpts =newBitmapFactory.Options();

//开始读入图片,此时把options.inJustDecodeBounds 设回true了

newOpts.inJustDecodeBounds =true;

Bitmap bitmap = BitmapFactory.decodeStream(isBm,null, newOpts);

newOpts.inJustDecodeBounds =false;

intw = newOpts.outWidth;

inth = newOpts.outHeight;

//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为

floathh = 800f;//这里设置高度为800f

floatww = 480f;//这里设置宽度为480f

//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

intbe =1;//be=1表示不缩放

if(w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放

be = (int) (newOpts.outWidth / ww);

}elseif(w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放

be = (int) (newOpts.outHeight / hh);

}

if(be <=0)

be =1;

newOpts.inSampleSize = be;//设置缩放比例

//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了

isBm =newByteArrayInputStream(baos.toByteArray());

bitmap = BitmapFactory.decodeStream(isBm,null, newOpts);

returncompressImage(bitmap);//压缩好比例大小后再进行质量压缩

}

3. android开发,上传图片时,怎样自动压缩成800x600的尺寸求指教~~

protected Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {
// 图森蚂纤片源
// Bitmap bm = BitmapFactory.decodeStream(getResources()
// .openRawResource(id));
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的物贺大小
int newWidth1 = newWidth;
int newHeight1 = newHeight;
// 计算缩放比此仿例
float scaleWidth = ((float) newWidth1) / width;
float scaleHeight = ((float) newHeight1) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;

}
你可以试下,如果不行,建议你还是先压缩在上传。

4. android 图片上传有什么方式可以压缩

非常简单,枫龄EYE网 导航栏右边有在散李轮线压缩。而且是无损解压,冲信不改变质量只变小。
点击后扰培,点upload上传,等待一会然后提示你download下载就可以了
支持批量解压,但是时间有点长。

5. android中压缩图片的几种方法比较

图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对 file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但 是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小 了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;

6. Android压缩图片的问题

java">//可能是裁剪方法或存储方法的原因下面的源码希望能帮到你
/**
*裁剪图片方法实现
*
*@paramuri
*/
publicvoidstartPhotoZoom(Uriuri){
Intentintent=newIntent("com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
//设置裁剪
intent.putExtra("crop","true");
//aspectXaspectY是宽高的比例
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
//outputXoutputY是裁剪图片宽高
intent.putExtra("outputX",180);
intent.putExtra("outputY",180);
intent.putExtra("return-data",true);
startActivityForResult(intent,2);
}
(Stringpath,BitmapmBitmap)throwsIOException{
Filefile=newFile(path);
if(!file.exists()){
file.createNewFile();
}
FileOutputStreamfOut=null;
猛信首try{
坦扮fOut=newFileOutputStream(file);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.JPEG,100,fOut);
try{
fOut.flush();
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
try{
fOut.close();
}catch(IOExceptione){
枝数e.printStackTrace();
returnfalse;
}
returntrue;
}

7. Android黑科技,图片终极压缩

一、支持自定义配置、不失真和批量处理

二、图片上传为什么要压缩
1、图片服务器空间限制,磁盘昂贵
2、网络不稳定,大文件需要断点续传
3、尽可能避免安卓OOM异常
4、后台约定的规则<200KB
5、需要上传原图的应用有医院临床项目、金融银行

三、图片压缩流程
1、递归每张图片
2、设置图片格式 Bitmap.CompressFormat.JPG
png, jpg,webp
3、质量压缩bitmap.compress(format,quality,baos)
由于png是无损压缩,所以设置quality无效(不适合作为缩略图)
采样率压缩BitmapFactory.Options.inSampleSize
缩小图片分辨率,减少所占用磁盘空间和内存大小
缩放压缩canvas.drawBitmap(bitmap, null,rectF,null)
减少图片的像素,降低所占用磁盘空间大小和内存大小,可用于缓存缩略图
JNI调用JPEG库
Android的图片引擎使用的是阉割版的skia引擎,去掉了图片压缩中的哈夫曼算法
4、像素修复
5、返回压缩
6、完成压缩

demo: https://github.com/ApeCold/Learn_Compress_Sample

参考:
Luban框架 https://github.com/Curzibn/Luban
缺点
1、当没有设定压缩路径时,抛异常无闪退
2、源码中,压缩比率固定值60,无法修改
3、压缩配置,参数不太适应真实项目需求
4、不能指定压缩大小,比如100KB以内
https://github.com/zettsu/Compressor

8. android 图片压缩

在文件管理里面长按图片会出现设置面板,选中压缩图片即可以。

9. 说说在 Android 中如何压缩图片

1:ByteArrayOutputStream 压缩图片质量,不会改变图片大小

privateBitmapcompressImage(Bitmapimage){

ByteArrayOutputStreambaos=newByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,100,baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
intoptions=100;while(baos.toByteArray().length/1024>100){//循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG,options,baos);//这里压缩options%,把压缩后的数据存放到baos中
options-=10;//每次都减少10}
ByteArrayInputStreamisBm=newByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmapbitmap=BitmapFactory.decodeStream(isBm,null,null);//把ByteArrayInputStream数据生成图片
returnbitmap;
}

2:就是改变图片大小

privateBitmapgetimage(StringsrcPath){
BitmapFactory.OptionsnewOpts=newBitmapFactory.Options();//开始读入图片,此时把options.inJustDecodeBounds设回true了
newOpts.inJustDecodeBounds=true;
Bitmapbitmap=BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds=false;intw=newOpts.outWidth;inth=newOpts.outHeight;//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
floathh=800f;//这里设置高度为800f
floatww=480f;//这里设置宽度为480f//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
intbe=1;//be=1表示不缩放
if(w>h&&w>ww){//如果宽度大的话根据宽度固定大小缩放
be=(int)(newOpts.outWidth/ww);
}elseif(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);returncompressImage(bitmap);//压缩好比例大小后再进行质量压缩}
热点内容
随机启动脚本 发布:2025-07-05 16:10:30 浏览:527
微博数据库设计 发布:2025-07-05 15:30:55 浏览:25
linux485 发布:2025-07-05 14:38:28 浏览:305
php用的软件 发布:2025-07-05 14:06:22 浏览:756
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:432
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:704
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:241
java的开发流程 发布:2025-07-05 12:45:11 浏览:686
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:285
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:835