|
|
@ -21,11 +21,11 @@ import android.graphics.BitmapFactory; |
|
|
|
import android.net.Uri; |
|
|
|
import android.net.Uri; |
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.os.AsyncTask; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Reduces the file size of images to fit under a given limit by resizing them, maintaining both |
|
|
|
* Reduces the file size of images to fit under a given limit by resizing them, maintaining both |
|
|
@ -35,22 +35,23 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> { |
|
|
|
private int sizeLimit; |
|
|
|
private int sizeLimit; |
|
|
|
private ContentResolver contentResolver; |
|
|
|
private ContentResolver contentResolver; |
|
|
|
private Listener listener; |
|
|
|
private Listener listener; |
|
|
|
private List<byte[]> resultList; |
|
|
|
private File tempFile; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param sizeLimit the maximum number of bytes each image can take |
|
|
|
* @param sizeLimit the maximum number of bytes each image can take |
|
|
|
* @param contentResolver to resolve the specified images' URIs |
|
|
|
* @param contentResolver to resolve the specified images' URIs |
|
|
|
|
|
|
|
* @param tempFile the file where the result will be stored |
|
|
|
* @param listener to whom the results are given |
|
|
|
* @param listener to whom the results are given |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public DownsizeImageTask(int sizeLimit, ContentResolver contentResolver, Listener listener) { |
|
|
|
public DownsizeImageTask(int sizeLimit, ContentResolver contentResolver, File tempFile, Listener listener) { |
|
|
|
this.sizeLimit = sizeLimit; |
|
|
|
this.sizeLimit = sizeLimit; |
|
|
|
this.contentResolver = contentResolver; |
|
|
|
this.contentResolver = contentResolver; |
|
|
|
|
|
|
|
this.tempFile = tempFile; |
|
|
|
this.listener = listener; |
|
|
|
this.listener = listener; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected Boolean doInBackground(Uri... uris) { |
|
|
|
protected Boolean doInBackground(Uri... uris) { |
|
|
|
resultList = new ArrayList<>(); |
|
|
|
|
|
|
|
for (Uri uri : uris) { |
|
|
|
for (Uri uri : uris) { |
|
|
|
InputStream inputStream; |
|
|
|
InputStream inputStream; |
|
|
|
try { |
|
|
|
try { |
|
|
@ -65,8 +66,6 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> { |
|
|
|
IOUtils.closeQuietly(inputStream); |
|
|
|
IOUtils.closeQuietly(inputStream); |
|
|
|
// Get EXIF data, for orientation info.
|
|
|
|
// Get EXIF data, for orientation info.
|
|
|
|
int orientation = MediaUtils.getImageOrientation(uri, contentResolver); |
|
|
|
int orientation = MediaUtils.getImageOrientation(uri, contentResolver); |
|
|
|
// Then use that information to determine how much to compress.
|
|
|
|
|
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
|
|
|
|
|
|
|
/* Unfortunately, there isn't a determined worst case compression ratio for image |
|
|
|
/* Unfortunately, there isn't a determined worst case compression ratio for image |
|
|
|
* formats. So, the only way to tell if they're too big is to compress them and |
|
|
|
* formats. So, the only way to tell if they're too big is to compress them and |
|
|
|
* test, and keep trying at smaller sizes. The initial estimate should be good for |
|
|
|
* test, and keep trying at smaller sizes. The initial estimate should be good for |
|
|
@ -74,7 +73,12 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> { |
|
|
|
* sure it gets downsized to below the limit. */ |
|
|
|
* sure it gets downsized to below the limit. */ |
|
|
|
int scaledImageSize = 1024; |
|
|
|
int scaledImageSize = 1024; |
|
|
|
do { |
|
|
|
do { |
|
|
|
stream.reset(); |
|
|
|
OutputStream stream; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
stream = new FileOutputStream(tempFile); |
|
|
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
|
inputStream = contentResolver.openInputStream(uri); |
|
|
|
inputStream = contentResolver.openInputStream(uri); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
} catch (FileNotFoundException e) { |
|
|
@ -109,9 +113,8 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> { |
|
|
|
reorientedBitmap.compress(format, 85, stream); |
|
|
|
reorientedBitmap.compress(format, 85, stream); |
|
|
|
reorientedBitmap.recycle(); |
|
|
|
reorientedBitmap.recycle(); |
|
|
|
scaledImageSize /= 2; |
|
|
|
scaledImageSize /= 2; |
|
|
|
} while (stream.size() > sizeLimit); |
|
|
|
} while (tempFile.length() > sizeLimit); |
|
|
|
|
|
|
|
|
|
|
|
resultList.add(stream.toByteArray()); |
|
|
|
|
|
|
|
if (isCancelled()) { |
|
|
|
if (isCancelled()) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -122,7 +125,7 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void onPostExecute(Boolean successful) { |
|
|
|
protected void onPostExecute(Boolean successful) { |
|
|
|
if (successful) { |
|
|
|
if (successful) { |
|
|
|
listener.onSuccess(resultList); |
|
|
|
listener.onSuccess(tempFile); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
listener.onFailure(); |
|
|
|
listener.onFailure(); |
|
|
|
} |
|
|
|
} |
|
|
@ -131,7 +134,7 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> { |
|
|
|
|
|
|
|
|
|
|
|
/** Used to communicate the results of the task. */ |
|
|
|
/** Used to communicate the results of the task. */ |
|
|
|
public interface Listener { |
|
|
|
public interface Listener { |
|
|
|
void onSuccess(List<byte[]> contentList); |
|
|
|
void onSuccess(File file); |
|
|
|
void onFailure(); |
|
|
|
void onFailure(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|