Skip to content

Commit 367e629

Browse files
committed
1. Added ImageCompressor for reducing the size of the images.
1 parent 1bc3af8 commit 367e629

File tree

5 files changed

+383
-2
lines changed

5 files changed

+383
-2
lines changed
0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ android {
2525
sourceCompatibility JavaVersion.VERSION_1_8
2626
targetCompatibility JavaVersion.VERSION_1_8
2727
}
28+
29+
packagingOptions {
30+
exclude 'META-INF/proguard/androidx-annotations.pro'
31+
}
2832
}
2933

3034
repositories {
@@ -37,7 +41,12 @@ ext {
3741

3842
dependencies {
3943
implementation fileTree(include: ['*.jar'], dir: 'libs')
40-
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
44+
45+
// Android library dependencies
4146
implementation "com.android.support:design:$supportLibraryVersion"
4247
implementation "com.android.support:cardview-v7:$supportLibraryVersion"
48+
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
49+
50+
// RxJava dependencies
51+
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
4352
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.amit.iv;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
6+
import com.amit.utilities.ImageUtils;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.concurrent.Callable;
11+
12+
import io.reactivex.Flowable;
13+
14+
/**
15+
* Created by AMIT JANGID on 27/12/2018.
16+
**/
17+
@SuppressWarnings({"unused", "WeakerAccess"})
18+
public class ImageCompressor
19+
{
20+
// max width and height values of the compressed image is taken as 612 x 816
21+
private int mQuality = 80;
22+
private int mMaxWidth = 612;
23+
private int mMaxHeight = 816;
24+
25+
private String mDestinationDirectoryPath;
26+
private Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
27+
28+
public ImageCompressor(Context context)
29+
{
30+
mDestinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
31+
}
32+
33+
public ImageCompressor setMaxWidth(int maxWidth)
34+
{
35+
this.mMaxWidth = maxWidth;
36+
return this;
37+
}
38+
39+
public ImageCompressor setMaxHeight(int maxHeight)
40+
{
41+
this.mMaxHeight = maxHeight;
42+
return this;
43+
}
44+
45+
public ImageCompressor setCompressFormat(Bitmap.CompressFormat compressFormat)
46+
{
47+
this.mCompressFormat = compressFormat;
48+
return this;
49+
}
50+
51+
public ImageCompressor setQuality(int quality)
52+
{
53+
this.mQuality = quality;
54+
return this;
55+
}
56+
57+
public ImageCompressor setDestinationDirectoryPath(String destinationDirectoryPath)
58+
{
59+
this.mDestinationDirectoryPath = destinationDirectoryPath;
60+
return this;
61+
}
62+
63+
public File compressToFile(File imageFile) throws IOException
64+
{
65+
return compressToFile( imageFile, imageFile.getName() );
66+
}
67+
68+
public File compressToFile(File imageFile, String compressedFileName) throws IOException
69+
{
70+
return ImageUtils.compressImage( imageFile, mMaxWidth, mMaxHeight, mCompressFormat, mQuality,
71+
mDestinationDirectoryPath + File.separator + compressedFileName );
72+
}
73+
74+
public Bitmap compressToBitmap(File imageFile) throws IOException
75+
{
76+
return ImageUtils.decodeSampledBitmapFromFile( imageFile, mMaxWidth, mMaxHeight );
77+
}
78+
79+
public Flowable<File> compressToFileAsFlowable(final File imageFile)
80+
{
81+
return compressToFileAsFlowable( imageFile, imageFile.getName() );
82+
}
83+
84+
public Flowable<File> compressToFileAsFlowable(final File imageFile, final String compressedFileName)
85+
{
86+
return Flowable.defer((Callable<Flowable<File>>) () ->
87+
{
88+
try
89+
{
90+
return Flowable.just( compressToFile( imageFile, compressedFileName ) );
91+
}
92+
catch (IOException e)
93+
{
94+
return Flowable.error( e );
95+
}
96+
});
97+
}
98+
99+
public Flowable<Bitmap> compressToBitmapAsFlowable(final File imageFile)
100+
{
101+
return Flowable.defer((Callable<Flowable<Bitmap>>) () ->
102+
{
103+
try
104+
{
105+
return Flowable.just( compressToBitmap( imageFile ) );
106+
}
107+
catch (IOException e)
108+
{
109+
return Flowable.error( e );
110+
}
111+
});
112+
}
113+
}

app/src/main/java/com/amit/utilities/FilePath.java

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,148 @@
88
import android.os.Environment;
99
import android.provider.DocumentsContract;
1010
import android.provider.MediaStore;
11+
import android.provider.OpenableColumns;
12+
import android.util.Log;
13+
14+
import java.io.File;
15+
import java.io.FileOutputStream;
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.io.OutputStream;
1119

1220
/**
1321
* Created by AmitS on 15-05-2017.
1422
**/
15-
@SuppressWarnings("unused")
23+
@SuppressWarnings({"unused", "UnusedReturnValue"})
1624
public class FilePath
1725
{
26+
private static final String TAG = FilePath.class.getSimpleName();
27+
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
28+
private static final int EOF = -1;
29+
30+
public static File from(Context context, Uri uri) throws IOException
31+
{
32+
String fileName = getFileName(context, uri);
33+
String[] splitName = splitFileName(fileName);
34+
35+
File tempFile = File.createTempFile(splitName[0], splitName[1]);
36+
tempFile = rename(tempFile, fileName);
37+
tempFile.deleteOnExit();
38+
39+
FileOutputStream fileOutputStream = null;
40+
InputStream inputStream = context.getContentResolver().openInputStream(uri);
41+
42+
try
43+
{
44+
fileOutputStream = new FileOutputStream(tempFile);
45+
}
46+
catch (Exception e)
47+
{
48+
Log.e(TAG, "from: exception while getting file:\n");
49+
e.printStackTrace();
50+
}
51+
52+
if (inputStream != null)
53+
{
54+
copy(inputStream, fileOutputStream);
55+
inputStream.close();
56+
}
57+
58+
if (fileOutputStream != null)
59+
{
60+
fileOutputStream.close();
61+
}
62+
63+
return tempFile;
64+
}
65+
66+
private static String[] splitFileName(String fileName)
67+
{
68+
String name = fileName;
69+
String extension = "";
70+
71+
int i = fileName.lastIndexOf(".");
72+
73+
if (i != -1)
74+
{
75+
name = fileName.substring(0, i);
76+
extension = fileName.substring(i);
77+
}
78+
79+
return new String[]{name, extension};
80+
}
81+
82+
private static String getFileName(Context context, Uri uri)
83+
{
84+
String result = null;
85+
86+
if (uri.getScheme().equalsIgnoreCase("content"))
87+
{
88+
89+
try (Cursor cursor = context.getContentResolver().query(
90+
uri, null, null, null, null))
91+
{
92+
if (cursor != null && cursor.moveToFirst())
93+
{
94+
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
95+
}
96+
}
97+
catch (Exception e)
98+
{
99+
Log.e(TAG, "getFileName: exception while getting file name:\n");
100+
e.printStackTrace();
101+
}
102+
}
103+
104+
if (result == null)
105+
{
106+
result = uri.getPath();
107+
int cut = result.lastIndexOf(File.separator);
108+
109+
if (cut != -1)
110+
{
111+
result = result.substring(cut + 1);
112+
}
113+
}
114+
115+
return result;
116+
}
117+
118+
private static File rename(File file, String newName)
119+
{
120+
File newFile = new File(file.getParent(), newName);
121+
122+
if (!newFile.equals(file))
123+
{
124+
if (newFile.exists() && newFile.delete())
125+
{
126+
Log.e(TAG, "rename: Deleted old + " + newName + " file");
127+
}
128+
129+
if (file.renameTo(newFile))
130+
{
131+
Log.e(TAG, "rename: Renamed file to " + newFile);
132+
}
133+
}
134+
135+
return newFile;
136+
}
137+
138+
private static long copy(InputStream inputStream, OutputStream outputStream) throws IOException
139+
{
140+
int n;
141+
long count = 0;
142+
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
143+
144+
while (EOF != (n = inputStream.read(buffer)))
145+
{
146+
outputStream.write(buffer, 0, n);
147+
count += n;
148+
}
149+
150+
return count;
151+
}
152+
18153
/**
19154
* Method for return file path of Gallery image/ Document / Video / Audio
20155
*

0 commit comments

Comments
 (0)