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

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;
@ -81,8 +83,12 @@ public class FileBackend {
return originalBitmap; return originalBitmap;
} }
} }
public JingleFile copyImageToPrivateStorage(Message message, Uri image) throws ImageCopyException {
return this.copyImageToPrivateStorage(message, image,0);
}
public JingleFile copyImageToPrivateStorage(Message message, Uri image) 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 { BitmapFactory.Options options = new BitmapFactory.Options();
originalBitmap = BitmapFactory.decodeStream(is); int inSampleSize = (int) Math.pow(2, sampleSize);
is.close(); Log.d("xmppService","reading bitmap with sample size "+inSampleSize);
} catch (OutOfMemoryError e) { options.inSampleSize = inSampleSize;
is.close(); originalBitmap = BitmapFactory.decodeStream(is, null, options);
Log.d("xmppService","catched out of memory. try again"); is.close();
if (image != null) {
is = context.getContentResolver().openInputStream(image);
} else {
is = new FileInputStream(getIncomingFile());
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
originalBitmap = BitmapFactory.decodeStream(is, null, options);
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++);
} }
} }