fix media resizing (#686)

* fix media resizing

* move exception catching out of method
This commit is contained in:
Konrad Pozniak 2018-06-18 13:26:31 +02:00 committed by GitHub
parent 63f9d99390
commit a42ce9b793
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View file

@ -85,7 +85,6 @@ import com.keylesspalace.tusky.adapter.EmojiAdapter;
import com.keylesspalace.tusky.adapter.MentionAutoCompleteAdapter;
import com.keylesspalace.tusky.adapter.OnEmojiSelectedListener;
import com.keylesspalace.tusky.db.AccountEntity;
import com.keylesspalace.tusky.db.AccountManager;
import com.keylesspalace.tusky.db.AppDatabase;
import com.keylesspalace.tusky.db.InstanceEntity;
import com.keylesspalace.tusky.di.Injectable;
@ -151,6 +150,7 @@ public final class ComposeActivity
private static final String TAG = "ComposeActivity"; // logging tag
static final int STATUS_CHARACTER_LIMIT = 500;
private static final int STATUS_MEDIA_SIZE_LIMIT = 8388608; // 8MiB
private static final int STATUS_MEDIA_PIXEL_SIZE_LIMIT = 16777216; // 4096^2 Pixels
private static final int MEDIA_PICK_RESULT = 1;
private static final int MEDIA_TAKE_PHOTO_RESULT = 2;
private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
@ -1063,10 +1063,16 @@ public final class ComposeActivity
if (item.readyStage != QueuedMedia.ReadyStage.UPLOADED) {
waitForMediaLatch.countUp();
if (mediaSize > STATUS_MEDIA_SIZE_LIMIT && type == QueuedMedia.Type.IMAGE) {
downsizeMedia(item);
} else {
uploadMedia(item);
try {
if (type == QueuedMedia.Type.IMAGE &&
(mediaSize > STATUS_MEDIA_SIZE_LIMIT || MediaUtils.getImageSquarePixels(getContentResolver(), item.uri) > STATUS_MEDIA_PIXEL_SIZE_LIMIT)) {
downsizeMedia(item);
} else {
uploadMedia(item);
}
} catch (FileNotFoundException e) {
onUploadFailure(item, false);
}
}
}

View file

@ -172,7 +172,6 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> {
* test, and keep trying at smaller sizes. The initial estimate should be good for
* many cases, so it should only iterate once, but the loop is used to be absolutely
* sure it gets downsized to below the limit. */
int iterations = 0;
int scaledImageSize = 1024;
do {
stream.reset();
@ -208,12 +207,11 @@ public class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> {
} else {
format = Bitmap.CompressFormat.PNG;
}
reorientedBitmap.compress(format, 75, stream);
reorientedBitmap.compress(format, 85, stream);
reorientedBitmap.recycle();
scaledImageSize /= 2;
iterations++;
} while (stream.size() > sizeLimit);
Assert.expect(iterations < 3);
resultList.add(stream.toByteArray());
if (isCancelled()) {
return false;

View file

@ -126,4 +126,17 @@ public class MediaUtils {
source.recycle();
return bitmap;
}
public static long getImageSquarePixels(ContentResolver contentResolver, Uri uri) throws FileNotFoundException {
InputStream input;
input = contentResolver.openInputStream(uri);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, options);
IOUtils.closeQuietly(input);
return (long) options.outWidth * options.outHeight;
}
}