nanchen2251 / compresshelper Goto Github PK
View Code? Open in Web Editor NEW:fire: 压缩文件,压缩图片,压缩Bitmap,Compress, CompressImage, CompressFile, CompressBitmap:https://github.com/nanchen2251/AiYaCompressHelper
:fire: 压缩文件,压缩图片,压缩Bitmap,Compress, CompressImage, CompressFile, CompressBitmap:https://github.com/nanchen2251/AiYaCompressHelper
保存的后缀名是jpeg,但我想保存为jpg,另外在做其它项目的时候因为不想图片被相册看到,有时会保存为“xxx._jpg”这样,所有提出此需求,请作者百忙之中抽空看下。
例如我想将一张原图大小为5405678,如果指定将他等比压缩为72075701;设置使用
.setMaxWidth(720).setMaxHeight(75701)这两个参数压缩完查看尺寸并没有压缩到我要的这个尺寸。
请问作者我该如何达到我的需求呢?
自定义压缩报错
java.lang.IllegalArgumentException: width and height must be > 0
是不是不支持 矫正图片方向?
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:867)
at android.graphics.Bitmap.createBitmap(Bitmap.java:846)
at android.graphics.Bitmap.createBitmap(Bitmap.java:813)
at com.nanchen.compresshelper.ImageUtil.getScaledBitmap(ImageUtil.java:129)
at com.nanchen.compresshelper.ImageUtil.compressImage(ImageUtil.java:175)
at com.nanchen.compresshelper.CompressHelper.compressToFile(CompressHelper.java:65)
at com.yctech.kindomgarden.qiniu.FileSyncService.synUpLoadFile(FileSyncService.java:342)
at com.yctech.kindomgarden.qiniu.FileSyncService.syncContent(FileSyncService.java:255)
at com.yctech.kindomgarden.qiniu.FileSyncService.onHandleIntent(FileSyncService.java:86)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.os.HandlerThread.run(HandlerThread.java:61)
异常类型:java.lang.NullPointerException
异常信息:Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
关键方法:com.nanchen.compresshelper.BitmapUtil.compressImage(BitmapUtil.java:177)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.nanchen.compresshelper.BitmapUtil.compressImage(BitmapUtil.java:177)
at com.nanchen.compresshelper.CompressHelper.compressToFile(CompressHelper.java:78)
at com.yctech.kindomgarden.qiniu.FileSyncService.synUpLoadFile(FileSyncService.java:343)
at com.yctech.kindomgarden.qiniu.FileSyncService.syncContent(FileSyncService.java:256)
at com.yctech.kindomgarden.qiniu.FileSyncService.onHandleIntent(FileSyncService.java:87)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.os.HandlerThread.run(HandlerThread.java:61)
// 采用 ExitInterface 设置图片旋转方向
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(),
matrix, true);
} catch (IOException e) {
e.printStackTrace();
}
这是在缩放法压缩后 又生成了一个scaledBitmap 但是 目前并没有发现他有什么用处,请指教
Hey i used android 6 and o ram app and check button to Caused Error
Image Photo http://upload.i4th.in.th/th/download.php?id=591534801
File Logcat
Caused by: java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:452)
at com.nanchen.compresshelper.CompressHelper.compressToFile(CompressHelper.java:78)
at com.nanchen.compressimage.MainActivity.compress(MainActivity.java:52)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
加上本库之后,调用相机拍照,第一次能返回,走onactivityresult,但是,再调用相机拍照,拍完照之后选择确定,就不回去了,选取消才能返回,确定是回不去的,不知为何
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.nanchen.compresshelper.BitmapUtil.compressImage(BitmapUtil.java:177)
at com.nanchen.compresshelper.CompressHelper.compressToFile(CompressHelper.java:78)
at com.yctech.kindomgarden.qiniu.FileSyncService.synUpLoadFile(FileSyncService.java:343)
at com.yctech.kindomgarden.qiniu.FileSyncService.syncContent(FileSyncService.java:141)
at com.yctech.kindomgarden.qiniu.FileSyncService.onHandleIntent(FileSyncService.java:87)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.os.HandlerThread.run(HandlerThread.java:61)
width and height must be > 0 怎么解决
怎样压缩文件为.zip ,怎样解压?
只能对jpg进行压缩?png和gif不支持吗
荣耀V9图片压缩4M左右的,压缩只能压缩到1M左右
我的项目的清单文件里的application有自己的android:label
你的清单文件里也有
我编译的过程中偶尔会报android:label的merge异常,会停止项目的运行
是否考虑去除掉你的library里的这个属性以避免打包过程中自动merge带来的覆盖可能性,虽然目前看来我这会自动merge
Gif压缩后不再是gif
好像图片不能等比例压缩,哪个方法是等比例压缩的了
看代码中并未用到Rx,Rx普遍已经更新到2.x,引入该库会冲突,请要么去掉Rx,要么请更新到2.x。 @nanchen2251
压缩图片是否可以支持直接bitmap输入
java.lang.RuntimeException: Unable to resume activity {com.izhuixin.app.xinyi/com.izhuixin.app.xinyi.ui.driver.OrderReceiptActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=26, result=-1, data=Intent { VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)}(has extras) }} to activity {com.izhuixin.app.xinyi/com.izhuixin.app.xinyi.ui.driver.OrderReceiptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4222)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4324)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3427)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5473)
at android.app.ActivityThread.access$1200(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7326)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=26, result=-1, data=Intent { VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)}(has extras) }} to activity {com.izhuixin.app.xinyi/com.izhuixin.app.xinyi.ui.driver.OrderReceiptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4999)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4208)
... 11 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at java.io.File.fixSlashes(File.java:183)
at java.io.File.(File.java:130)
at com.izhuixin.app.xinyi.ui.driver.OrderReceiptActivity.compressPhoto(OrderReceiptActivity.java:1224)
at com.izhuixin.app.xinyi.ui.driver.OrderReceiptActivity.onActivityResult(OrderReceiptActivity.java:640)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4995)
... 12 more
问题一
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
这里options里面的宽度和高度可能没有值,你需要再次设值
if (actualWidth > 0 && actualHeight > 0) {
options.outHeight = actualHeight;
options.outWidth = actualWidth;
}
问题二
bitmap2这个对象没有调用recycle()
压缩完之后,图片严重模糊
如题 压缩长图很模糊
https://github.com/zetbaitsu/Compressor 这个库犯了的问题, 你的这个也犯了.
单例静态把持Context.
标题取错了, 应该是"Context Leak"
复现环境:华为荣耀10,系统为8.1.0;
复现场景:调取相机拍照后,点击“√ ”(确认)后出现以下异常:
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:1209)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1176)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1126)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1087)
at com.nanchen.compresshelper.ImageUtil.getScaledBitmap(ImageUtil.java:131)
at com.nanchen.compresshelper.ImageUtil.compressImage(ImageUtil.java:177)
为什么文件的大小压缩下去了,可是转回bitmap大小好像没有什么改变,这支持bitmap的压缩么??
点击压缩后,size显示为0,也没有报错或toast提示错误。我把app module
中的build.gradle
的targetSdkVersion
改成22就可以了,23,24,25都不行,设置里面权限也给了。最后发现注释掉.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath())
就可以了
比如,压缩宽高<-1 的时候,自动计算合适的压缩宽高;
压缩质量< -1 的时候,自动计算合适的压缩质量;
都 < -1 那就像微信一样全自动压缩到合适的分辨率和体积。
因为有的图片,不好限制宽高去压缩,比如太宽或者太长的。
有的图,quality 设置80,压了还是很大。
可以增加开始压缩、压缩成功、压缩失败这些回调吗?方便提出提示。
HM 2A 系统 : 4.4.4
操作步骤
1,启动app,点击照片。
2,有的照片可以,有的击有一些照片点击选择就会闪退(闪退的是大点的照片大概6M—8M)
我在微博上保存一张图片到手机,然后选择这张图片进行压缩报读取图片数据失败,我试着对该张图片重命名保存后就可以了,这是什么原因,我的手机努比亚Z11mini
我原来压缩统一图片都是.jpg后缀的.现在用这个框架把相册的导入压缩完了之后变成.jpeg了.
java.lang.NullPointerException
at com.nanchen.compresshelper.BitmapUtil.compressImage(BitmapUtil.java:177)
at com.nanchen.compresshelper.CompressHelper.compressToFile(CompressHelper.java:78)
at com.yctech.kindomgarden.qiniu.FileSyncService.synUpLoadFile(FileSyncService.java:343)
at com.yctech.kindomgarden.qiniu.FileSyncService.syncContent(FileSyncService.java:256)
at com.yctech.kindomgarden.qiniu.FileSyncService.onHandleIntent(FileSyncService.java:87)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.