zetbaitsu / compressor Goto Github PK
View Code? Open in Web Editor NEWAn android image compression library.
An android image compression library.
as title
Does this keep the exif information from the original file? Like rotation on some Samsung devices?
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at id.zelory.compressor.ImageUtil.getScaledBitmap(ImageUtil.java:96)
at id.zelory.compressor.ImageUtil.compressImage(ImageUtil.java:128)
at id.zelory.compressor.Compressor.compressToFile(Compressor.java:43)
at com.rxjr.rxcd.beta.utils.CompressUtil.doInBackground(CompressUtil.java:48)
at com.rxjr.rxcd.beta.utils.CompressUtil.doInBackground(CompressUtil.java:21)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
//压缩
// compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex:rxandroid:1.2.1'
// compile 'io.reactivex.rxjava2:rxjava:2.0.1'
Below is how I'm using the compression of image taken from camera intent
final File compressedOutput1 = new Compressor.Builder(getApplicationContext()) .setMaxWidth(1024) .setMaxHeight(768) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.JPEG) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .build() .compressToFile(output1);
And after the compression this file is uploaded to server, and I'm deleting the file using
File fDelete2 = FileUtils.getFile(mContext, Uri.fromFile(compressedOutput1)); if (fDelete2.exists()) { if (fDelete2.delete()) { Toast.makeText(mContext, "Upload complete, Local copy of uploaded file has been deleted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(mContext, "Could not delete file", Toast.LENGTH_SHORT).show(); } }
But when I'm checking the external storage pictures directory, the file exists there.
Why delete() not worked?
Please help.
i m Getting error in this line:
Compressor.getDefault(this).compressToFileAsObservable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())
i m using android studio 2.3.1
and my project using this lib.
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
.
It is in function calculateInSampleSize. Why totalReqPixelsCap = reqWidth * reqHeight * 2; but not totalReqPixelsCap = reqWidth * reqHeight. Thanks.
I want to know :With the "Factory. DecodeFile ()" compression many very large images, will outofmemeryError?
I want to compress Bitmap to Bitmap, please help me ?
👍 👍
I'm trying to decode a file, and always getting this error.
Hi,
I used custom compressor (same as given in your example) but it returned an image of 0 bytes.
When i removed .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath())
It worked! I am using android 4.4.2
What can be the possible reasons for this?
Thanks
I have double checked the file I'm using for compressToFile() is indeed an image file. I have succeeded using the exact same file for uploading it as is.
Though compressToFile() throws an
java.lang.IllegalArgumentException:
width and height must be > 0`
This is the calling function
File compressedFile = Compressor.getDefault(this).compressToFile(new File(uri));
Where uri is a string representing the location.
It should also accept Uri as a parameter. I tried converting my Uri into file and then passed it. It shows error "Image not found". Another line was saying "image width and height should not be less than 0".
Library shouldn't crash if someone is passing bad file. It should be capable to handle the same if it suggests possible root cause then that would be appreciable.
Hi,this is also my question,can you tell me how to fix it?
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:967)
at android.graphics.Bitmap.createBitmap(Bitmap.java:946)
at android.graphics.Bitmap.createBitmap(Bitmap.java:913)
at id.zelory.compressor.ImageUtil.getScaledBitmap(ImageUtil.java:116)
at id.zelory.compressor.ImageUtil.compressImage(ImageUtil.java:161)
at id.zelory.compressor.Compressor.compressToFile(Compressor.java:48)
receiving in
DEVICE config
Android 6.01
Family samsung
Model on7xeltedd (SM-G610F)
Architecture armv8l
Orientation portrait
screen_resolution 1920x1080
compressing the image from the web
Sometimes get "width and height must be > 0" error, any know why this happen?
tanks!
I am using RxJava 2.0.1 in my project and when i import Compressor it wont compile complaining i have multiple versions of RxJava.
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties
File1: /Users/envative/.gradle/caches/modules-2/files-2.1/io.reactivex/rxjava/1.1.6/2586312cd2b8a511e4c6236736f5a039fc0f2273/rxjava-1.1.6.jar
File2: /Users/envative/.gradle/caches/modules-2/files-2.1/io.reactivex.rxjava2/rxjava/2.0.1/57f850a6b317e5582f1dbaff10a9e7d7e1fcdcfb/rxjava-2.0.1.jar
I'm using for the example:
new Compressor.Builder(this)
.setMaxWidth(320)
.setMaxHeight(640)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.JPEG)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.build()
.compressToFile(new File("my file path"));
when the code execute to "compressToFile",throw the NullPointerException,My file path is exist, I want to know why?(On Android 4.4.2 occurred,Android 5.0 or 6.0 is ok!)
When I choose some pictures,it may happen,how can i fix it?
at id.zelory.compressor.ImageUtil.getScaledBitmap(ImageUtil.java:62)
actualWidth = bitmap2.getWidth();
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
I compress the same picture, no problem just one.If I compress two picture,the quality is not good
You should probably mention that the library also does a Rotate on the image during the rescale process.
The library also catches any exception possible (OOM, ...) it makes it quite hard to debug when using it.
Hi i use the Compressor and after i need to encodeTobase64. I think i´m compress the image twice times. How could i encondetobase64 without compress two times? :
bitmap = new Compressor.Builder(getContext())
.setMaxWidth(940)
.setMaxHeight(780)
.setQuality(85)
.setCompressFormat(Bitmap.CompressFormat.PNG)
.build()
.compressToBitmap(imgFile);
String photoBase64 = encodeTobase64(bitmap);
And here receive to encodetobase64:
public static String encodeTobase64(Bitmap image) {
Bitmap bitmap = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
Thank you!
provide public function to set file name rule.
If I don't use RXJAVA, I have to write asynchronous method myself?
How do you set the file destination if you are using the rx java method of compression?
at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
at android.graphics.Bitmap.nativeCreate(Bitmap.java)
at android.graphics.Bitmap.createBitmap(Bitmap.java:831)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:728)
at id.zelory.compressor.ImageUtil.getScaledBitmap(ImageUtil.java:144)
at id.zelory.compressor.ImageUtil.compressImage(ImageUtil.java:161)
at id.zelory.compressor.Compressor.compressToFile(Compressor.java:47)
Currently library uses Bitmap.Config.ARGB_8888
for creating bitmap. I want to use, for example Bitmap.Config.RGB_565
. How I can achieve that?
Or, if not, could you please provide setter in Builder to have ability to choose Bitmap.Config
for my aims?
when there is no storage permission to read/write. better through the exception stating
in current version this library is throwing "width and height must be > 0" which is not correct
Below is the log
FATAL EXCEPTION: main
Process: com.yellowpages.et.directory, PID: 30682
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:6263 flg=0x1 }} to activity {com.yellowpages.et.directory/com.yellowpages.et.directory.Activities.NewPostActivity}: java.lang.IllegalArgumentException: prefix must be at least 3 characters
at android.app.ActivityThread.deliverResults(ActivityThread.java:4672)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4715)
at android.app.ActivityThread.access$1500(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.IllegalArgumentException: prefix must be at least 3 characters
at java.io.File.createTempFile(File.java:996)
at java.io.File.createTempFile(File.java:966)
at id.zelory.compressor.FileUtil.from(FileUtil.java:38)
at com.yellowpages.et.directory.Activities.NewPostActivity.onActivityResult(NewPostActivity.java:175)
at android.app.Activity.dispatchActivityResult(Activity.java:6758)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4668)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4715)
at android.app.ActivityThread.access$1500(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
what is meaning of
if (bmp == null) { InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (FileNotFoundException exception) { exception.printStackTrace(); } catch (IOException exception) { exception.printStackTrace(); } }
why add that after Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
how replace original file with new compress file.?
i review compress source code, but i can't find it recycle bitmap. Is it a bug?
The code below in the libary's manifest.xml is unnecessary and make mistake
<application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true" > </application>
When I build gradle, it will tell me the
Attribute application@label value=(@string/application_name) from AndroidManifest.xml:30:9 is also present at {id.zelory:compressor} value=(@string/app_name) Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:26:5 to override
When I follow the direction I fix this error, however I think maybe removing the tag from library is a better solution for others.
Distinctly, context leak
private static volatile Compressor INSTANCE;
private Context context;
my project use RxJava2.0.1, and i use this lib to handler image,.
Then when i run my project like this:
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties
File1: /Users/envative/.gradle/caches/modules-2/files-2.1/io.reactivex/rxjava/1.1.6/2586312cd2b8a511e4c6236736f5a039fc0f2273/rxjava-1.1.6.jar
File2: /Users/envative/.gradle/caches/modules-2/files-2.1/io.reactivex.rxjava2/rxjava/2.0.1/57f850a6b317e5582f1dbaff10a9e7d7e1fcdcfb/rxjava-2.0.1.jar
I'm build the APK using proguard, and it says that I've to make some proguard config using your library. Could you explain me how to add a proguard config for your lib ?
When a Image(like panoramic photograph) is too long ,I set setMaxWidth(800) and setMaxHeight(800) ,the out put will be too small and indistinct
Which type of compression is there, is it lossless or lossy?
And can can we compress image in WEBP format?
Thanks
Hi, I use this library a few years ago.
And works very well.
Do you think to do in NodeJs?
ty
My application have to compress image to defined size (1080 * 1080)
Even the original image size is smaller than desired size I have to scale it to large resolution.
So it would be nice if it has "Force size" or "Minimum size" option.
Thanks.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isMutable()' on a null object reference
at android.graphics.Canvas.(Canvas.java:149)
at id.zelory.compressor.ImageUtil.getScaledBitmap(SourceFile:94)
at id.zelory.compressor.Compressor.compressToBitmap(SourceFile:47)
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.