possible fix for #232

This commit is contained in:
iNPUTmice 2014-06-30 12:01:43 +02:00
parent dcb21cb9e6
commit 121fb58d5e
1 changed files with 35 additions and 6 deletions

View File

@ -11,6 +11,8 @@ import java.io.OutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.util.Log;
import android.util.LruCache;
@ -86,18 +88,28 @@ public class FileBackend {
}
}
public JingleFile copyImageToPrivateStorage(Message message, Uri image) throws ImageCopyException {
public Bitmap rotate(Bitmap bitmap, int degree) {
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);
}
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 {
private JingleFile copyImageToPrivateStorage(Message message, Uri image,
int sampleSize) throws ImageCopyException {
try {
InputStream is;
if (image != null) {
is = context.getContentResolver().openInputStream(image);
} else {
is = new FileInputStream(getIncomingFile());
image = getIncomingUri();
}
JingleFile file = getJingleFile(message);
file.getParentFile().mkdirs();
@ -105,7 +117,8 @@ public class FileBackend {
Bitmap originalBitmap;
BitmapFactory.Options options = new BitmapFactory.Options();
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;
originalBitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
@ -116,6 +129,18 @@ public class FileBackend {
getIncomingFile().delete();
}
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);
boolean success = scalledBitmap.compress(
Bitmap.CompressFormat.WEBP, 75, os);
@ -191,6 +216,10 @@ public class FileBackend {
return new File(context.getFilesDir().getAbsolutePath() + "/incoming");
}
public Uri getIncomingUri() {
return Uri.parse(context.getFilesDir().getAbsolutePath() + "/incoming");
}
public class ImageCopyException extends Exception {
private static final long serialVersionUID = -1010013599132881427L;
private int resId;