diff --git a/app/build.gradle b/app/build.gradle index 1037aee2e..d1b86dfe0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -114,7 +114,7 @@ dependencies { implementation 'com.github.adrielcafe:AndroidAudioRecorder:0.3.0' implementation 'yogesh.firzen:MukkiyaSevaigal:1.0.6' - + implementation 'id.zelory:compressor:2.1.0' implementation "ch.acra:acra-mail:$acraVersion" implementation "ch.acra:acra-limiter:$acraVersion" implementation "ch.acra:acra-notification:$acraVersion" diff --git a/app/src/main/java/app/fedilab/android/activities/EditProfileActivity.java b/app/src/main/java/app/fedilab/android/activities/EditProfileActivity.java index be55d3358..407b96fb4 100644 --- a/app/src/main/java/app/fedilab/android/activities/EditProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/EditProfileActivity.java @@ -544,7 +544,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou } catch (FileNotFoundException e) { e.printStackTrace(); } - header_picture = Helper.compressImage(EditProfileActivity.this, fileUri, Helper.MediaType.MEDIA); + header_picture = Helper.compressImage(EditProfileActivity.this, fileUri); } else if (requestCode == PICK_IMAGE_PROFILE && resultCode == RESULT_OK) { if (data == null || data.getData() == null) { Toasty.error(getApplicationContext(), getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show(); @@ -562,7 +562,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou } catch (FileNotFoundException e) { e.printStackTrace(); } - profile_picture = Helper.compressImage(EditProfileActivity.this, fileUri, Helper.MediaType.PROFILE); + profile_picture = Helper.compressImage(EditProfileActivity.this, fileUri); } } diff --git a/app/src/main/java/app/fedilab/android/activities/TootActivity.java b/app/src/main/java/app/fedilab/android/activities/TootActivity.java index 388431eec..e282267fb 100644 --- a/app/src/main/java/app/fedilab/android/activities/TootActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/TootActivity.java @@ -1490,7 +1490,7 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate, if (error) { return null; } - bs = Helper.compressImage(activityWeakReference.get(), uriFile, Helper.MediaType.MEDIA); + bs = Helper.compressImage(activityWeakReference.get(), uriFile); return null; } diff --git a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java index 931fc8176..52560bdfa 100644 --- a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java @@ -1809,32 +1809,21 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable }); //Resize - final Spinner resize_layout_spinner = rootView.findViewById(R.id.set_resize_picture); - ArrayAdapter adapterResize = ArrayAdapter.createFromResource(getContext(), - R.array.settings_resize_picture, android.R.layout.simple_spinner_item); - resize_layout_spinner.setAdapter(adapterResize); - int positionSpinnerResize = sharedpreferences.getInt(Helper.SET_PICTURE_RESIZE, Helper.S_4MO); - resize_layout_spinner.setSelection(positionSpinnerResize); - resize_layout_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + final CheckBox set_resize_picture = rootView.findViewById(R.id.set_resize_picture); + boolean compress = sharedpreferences.getBoolean(Helper.SET_PICTURE_COMPRESSED, true); + set_resize_picture.setChecked(compress); + set_resize_picture.setOnClickListener(new View.OnClickListener() { @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - if (count4 > 0) { - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putInt(Helper.SET_PICTURE_RESIZE, position); - editor.apply(); - } - count4++; - } - - @Override - public void onNothingSelected(AdapterView parent) { + public void onClick(View v) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(Helper.SET_PICTURE_COMPRESSED, set_resize_picture.isChecked()); + editor.apply(); } }); - boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true); boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true); boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true); diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 9fd486567..c6bc7128c 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -221,6 +221,7 @@ import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask; import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.Sqlite; +import id.zelory.compressor.Compressor; import info.guardianproject.netcipher.client.StrongBuilder; import info.guardianproject.netcipher.client.StrongOkHttpClientBuilder; import okhttp3.ConnectionSpec; @@ -349,7 +350,7 @@ public class Helper { public static final String SET_DISABLE_ANIMATED_EMOJI = "set_disable_animated_emoji"; public static final String SET_CAPITALIZE = "set_capitalize"; public static final String SET_WYSIWYG = "set_wysiwyg"; - public static final String SET_PICTURE_RESIZE = "set_picture_resize"; + public static final String SET_PICTURE_COMPRESSED = "set_picture_compressed"; public static final String SET_FORWARD_TAGS_IN_REPLY = "set_forward_tags_in_reply"; public static final String SET_FULL_PREVIEW = "set_full_preview"; public static final String SET_COMPACT_MODE = "set_compact_mode"; @@ -383,13 +384,6 @@ public class Helper { public static final String SET_AUTO_BACKUP_STATUSES = "set_auto_backup_statuses"; public static final String SET_AUTO_BACKUP_NOTIFICATIONS = "set_auto_backup_notifications"; - public static final int S_NO = 0; - static final int S_512KO = 1; - public static final int S_1MO = 2; - public static final int S_2MO = 3; - public static final int S_4MO = 4; - public static final int S_6MO = 5; - public static final int S_8MO = 6; public static final int ATTACHMENT_ALWAYS = 1; public static final int ATTACHMENT_WIFI = 2; @@ -3420,23 +3414,14 @@ public class Helper { } - public static ByteArrayInputStream compressImage(Context context, android.net.Uri uriFile, MediaType mediaType) { + public static ByteArrayInputStream compressImage(Context context, android.net.Uri uriFile) { ContentResolver cr = context.getContentResolver(); String mime = cr.getType(uriFile); + File file = new File(uriFile.getPath()); ByteArrayInputStream bs = null; if (mime != null && mime.toLowerCase().contains("image")) { - Bitmap takenImage; - try { - takenImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uriFile); - } catch (IOException e) { - try { - Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show(); - } catch (Exception ignored) { - } - ; - return null; - } + ExifInterface exif = null; try (InputStream inputStream = context.getContentResolver().openInputStream(uriFile)) { assert inputStream != null; @@ -3444,99 +3429,56 @@ public class Helper { } catch (Exception e) { e.printStackTrace(); } - Matrix matrix = null; - if (takenImage != null) { - int size = takenImage.getByteCount(); - if (exif != null) { - int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); - int rotationDegree = 0; - if (rotation == ExifInterface.ORIENTATION_ROTATE_90) { - rotationDegree = 90; - } else if (rotation == ExifInterface.ORIENTATION_ROTATE_180) { - rotationDegree = 180; - } else if (rotation == ExifInterface.ORIENTATION_ROTATE_270) { - rotationDegree = 270; - } - matrix = new Matrix(); - if (rotation != 0f) { - matrix.preRotate(rotationDegree); - } - } - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); - int resizeSet = sharedpreferences.getInt(Helper.SET_PICTURE_RESIZE, Helper.S_4MO); - if (mediaType == MediaType.PROFILE) - resizeSet = Helper.S_1MO; - double resizeby = size; - if (resizeSet == Helper.S_512KO) { - resizeby = 4194304; - } else if (resizeSet == Helper.S_1MO) { - resizeby = 8388608; - } else if (resizeSet == Helper.S_2MO) { - resizeby = 16777216; - } else if (resizeSet == Helper.S_4MO) { - resizeby = 33554432; - } else if (resizeSet == Helper.S_6MO) { - resizeby = 50331648; - } else if (resizeSet == Helper.S_8MO) { - resizeby = 67108864; + Matrix matrix; + if (exif != null) { + int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + int rotationDegree = 0; + if (rotation == ExifInterface.ORIENTATION_ROTATE_90) { + rotationDegree = 90; + } else if (rotation == ExifInterface.ORIENTATION_ROTATE_180) { + rotationDegree = 180; + } else if (rotation == ExifInterface.ORIENTATION_ROTATE_270) { + rotationDegree = 270; } + matrix = new Matrix(); + if (rotation != 0f) { + matrix.preRotate(rotationDegree); + } + } - double resize = ((double) size) / resizeby; - if (resize > 1) { - Bitmap newBitmap = Bitmap.createScaledBitmap(takenImage, (int) (takenImage.getWidth() / resize), - (int) (takenImage.getHeight() / resize), true); - Bitmap adjustedBitmap; - if (matrix != null) - try { - adjustedBitmap = Bitmap.createBitmap(newBitmap, 0, 0, newBitmap.getWidth(), newBitmap.getHeight(), matrix, true); - } catch (Exception e) { - adjustedBitmap = newBitmap; - } - else - adjustedBitmap = newBitmap; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - if (mime.contains("png") || mime.contains(".PNG")) - adjustedBitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); - else - adjustedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); - byte[] bitmapdata = bos.toByteArray(); - bs = new ByteArrayInputStream(bitmapdata); - } else { - try { - InputStream inputStream = context.getContentResolver().openInputStream(uriFile); - byte[] buff = new byte[8 * 1024]; - int bytesRead; - ByteArrayOutputStream bao = new ByteArrayOutputStream(); - assert inputStream != null; - while ((bytesRead = inputStream.read(buff)) != -1) { - bao.write(buff, 0, bytesRead); - } - byte[] data = bao.toByteArray(); - bs = new ByteArrayInputStream(data); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } else { + InputStream resizedIS = null; + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + boolean compressed = sharedpreferences.getBoolean(Helper.SET_PICTURE_COMPRESSED, true); + + if( compressed) { try { - InputStream inputStream = context.getContentResolver().openInputStream(uriFile); - byte[] buff = new byte[8 * 1024]; - int bytesRead; - ByteArrayOutputStream bao = new ByteArrayOutputStream(); - assert inputStream != null; - while ((bytesRead = inputStream.read(buff)) != -1) { - bao.write(buff, 0, bytesRead); - } - byte[] data = bao.toByteArray(); - bs = new ByteArrayInputStream(data); - } catch (FileNotFoundException e) { - e.printStackTrace(); + File compressedFile= new Compressor(context).compressToFile(file); + resizedIS = new FileInputStream(compressedFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } + }else{ + try { + resizedIS = context.getContentResolver().openInputStream(uriFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + try { + byte[] buff = new byte[8 * 1024]; + int bytesRead; + ByteArrayOutputStream bao = new ByteArrayOutputStream(); + assert resizedIS != null; + while ((bytesRead = resizedIS.read(buff)) != -1) { + bao.write(buff, 0, bytesRead); + } + byte[] data = bao.toByteArray(); + bs = new ByteArrayInputStream(data); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } } else { try { diff --git a/app/src/main/res/layout/fragment_settings_reveal.xml b/app/src/main/res/layout/fragment_settings_reveal.xml index 2da10700d..8d05c9774 100644 --- a/app/src/main/res/layout/fragment_settings_reveal.xml +++ b/app/src/main/res/layout/fragment_settings_reveal.xml @@ -1519,27 +1519,6 @@ android:layout_height="wrap_content"> - - - - - - - - + + + + + + + + + + + + + Notifications will be fetched every 15 minutes. Add notes Notes for the account + Allow to compress large photos into smaller sized photos with very less or negligible loss in quality of the image. \ No newline at end of file