auto retry image compression with decreasing sample size

This commit is contained in:
Daniel Gultsch 2014-05-21 00:39:45 +02:00
parent fa9cbeb746
commit 9d2ce5ff98
1 changed files with 15 additions and 17 deletions

View File

@ -8,6 +8,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import org.bouncycastle.crypto.engines.ISAACEngine;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -82,7 +84,11 @@ public class FileBackend {
} }
} }
public JingleFile copyImageToPrivateStorage(Message message, Uri image) public JingleFile copyImageToPrivateStorage(Message message, Uri image) throws ImageCopyException {
return this.copyImageToPrivateStorage(message, image,0);
}
private JingleFile copyImageToPrivateStorage(Message message, Uri image, int sampleSize)
throws ImageCopyException { throws ImageCopyException {
try { try {
InputStream is; InputStream is;
@ -95,22 +101,12 @@ public class FileBackend {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
file.createNewFile(); file.createNewFile();
Bitmap originalBitmap; Bitmap originalBitmap;
try {
originalBitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (OutOfMemoryError e) {
is.close();
Log.d("xmppService","catched out of memory. try again");
if (image != null) {
is = context.getContentResolver().openInputStream(image);
} else {
is = new FileInputStream(getIncomingFile());
}
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; int inSampleSize = (int) Math.pow(2, sampleSize);
Log.d("xmppService","reading bitmap with sample size "+inSampleSize);
options.inSampleSize = inSampleSize;
originalBitmap = BitmapFactory.decodeStream(is, null, options); originalBitmap = BitmapFactory.decodeStream(is, null, options);
is.close(); is.close();
}
if (originalBitmap == null) { if (originalBitmap == null) {
throw new ImageCopyException(R.string.error_not_an_image_file); throw new ImageCopyException(R.string.error_not_an_image_file);
} }
@ -138,6 +134,8 @@ public class FileBackend {
} catch (SecurityException e) { } catch (SecurityException e) {
throw new ImageCopyException( throw new ImageCopyException(
R.string.error_security_exception_during_image_copy); R.string.error_security_exception_during_image_copy);
} catch (OutOfMemoryError e) {
return copyImageToPrivateStorage(message, image, sampleSize++);
} }
} }