possible fix for #232
This commit is contained in:
parent
dcb21cb9e6
commit
121fb58d5e
|
@ -11,6 +11,8 @@ import java.io.OutputStream;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.Matrix;
|
||||||
|
import android.media.ExifInterface;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.LruCache;
|
import android.util.LruCache;
|
||||||
|
@ -85,19 +87,29 @@ public class FileBackend {
|
||||||
return originalBitmap;
|
return originalBitmap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JingleFile copyImageToPrivateStorage(Message message, Uri image) throws ImageCopyException {
|
public Bitmap rotate(Bitmap bitmap, int degree) {
|
||||||
return this.copyImageToPrivateStorage(message, image,0);
|
int w = bitmap.getWidth();
|
||||||
|
int h = bitmap.getHeight();
|
||||||
|
Matrix mtx = new Matrix();
|
||||||
|
mtx.postRotate(degree);
|
||||||
|
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JingleFile copyImageToPrivateStorage(Message message, Uri image, int sampleSize)
|
public JingleFile copyImageToPrivateStorage(Message message, Uri image)
|
||||||
throws ImageCopyException {
|
throws ImageCopyException {
|
||||||
|
return this.copyImageToPrivateStorage(message, image, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JingleFile copyImageToPrivateStorage(Message message, Uri image,
|
||||||
|
int sampleSize) throws ImageCopyException {
|
||||||
try {
|
try {
|
||||||
InputStream is;
|
InputStream is;
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
is = context.getContentResolver().openInputStream(image);
|
is = context.getContentResolver().openInputStream(image);
|
||||||
} else {
|
} else {
|
||||||
is = new FileInputStream(getIncomingFile());
|
is = new FileInputStream(getIncomingFile());
|
||||||
|
image = getIncomingUri();
|
||||||
}
|
}
|
||||||
JingleFile file = getJingleFile(message);
|
JingleFile file = getJingleFile(message);
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
|
@ -105,7 +117,8 @@ public class FileBackend {
|
||||||
Bitmap originalBitmap;
|
Bitmap originalBitmap;
|
||||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
int inSampleSize = (int) Math.pow(2, sampleSize);
|
int inSampleSize = (int) Math.pow(2, sampleSize);
|
||||||
Log.d("xmppService","reading bitmap with sample size "+inSampleSize);
|
Log.d("xmppService", "reading bitmap with sample size "
|
||||||
|
+ inSampleSize);
|
||||||
options.inSampleSize = inSampleSize;
|
options.inSampleSize = inSampleSize;
|
||||||
originalBitmap = BitmapFactory.decodeStream(is, null, options);
|
originalBitmap = BitmapFactory.decodeStream(is, null, options);
|
||||||
is.close();
|
is.close();
|
||||||
|
@ -116,6 +129,18 @@ public class FileBackend {
|
||||||
getIncomingFile().delete();
|
getIncomingFile().delete();
|
||||||
}
|
}
|
||||||
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
|
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
|
||||||
|
originalBitmap = null;
|
||||||
|
ExifInterface exif = new ExifInterface(image.toString());
|
||||||
|
if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
||||||
|
.equalsIgnoreCase("6")) {
|
||||||
|
scalledBitmap = rotate(scalledBitmap, 90);
|
||||||
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
||||||
|
.equalsIgnoreCase("8")) {
|
||||||
|
scalledBitmap = rotate(scalledBitmap, 270);
|
||||||
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
|
||||||
|
.equalsIgnoreCase("3")) {
|
||||||
|
scalledBitmap = rotate(scalledBitmap, 180);
|
||||||
|
}
|
||||||
OutputStream os = new FileOutputStream(file);
|
OutputStream os = new FileOutputStream(file);
|
||||||
boolean success = scalledBitmap.compress(
|
boolean success = scalledBitmap.compress(
|
||||||
Bitmap.CompressFormat.WEBP, 75, os);
|
Bitmap.CompressFormat.WEBP, 75, os);
|
||||||
|
@ -138,7 +163,7 @@ public class FileBackend {
|
||||||
R.string.error_security_exception_during_image_copy);
|
R.string.error_security_exception_during_image_copy);
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
++sampleSize;
|
++sampleSize;
|
||||||
if (sampleSize<=3) {
|
if (sampleSize <= 3) {
|
||||||
return copyImageToPrivateStorage(message, image, sampleSize);
|
return copyImageToPrivateStorage(message, image, sampleSize);
|
||||||
} else {
|
} else {
|
||||||
throw new ImageCopyException(R.string.error_out_of_memory);
|
throw new ImageCopyException(R.string.error_out_of_memory);
|
||||||
|
@ -190,6 +215,10 @@ public class FileBackend {
|
||||||
public File getIncomingFile() {
|
public File getIncomingFile() {
|
||||||
return new File(context.getFilesDir().getAbsolutePath() + "/incoming");
|
return new File(context.getFilesDir().getAbsolutePath() + "/incoming");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Uri getIncomingUri() {
|
||||||
|
return Uri.parse(context.getFilesDir().getAbsolutePath() + "/incoming");
|
||||||
|
}
|
||||||
|
|
||||||
public class ImageCopyException extends Exception {
|
public class ImageCopyException extends Exception {
|
||||||
private static final long serialVersionUID = -1010013599132881427L;
|
private static final long serialVersionUID = -1010013599132881427L;
|
||||||
|
|
Loading…
Reference in a new issue