From 3184f375db45d55d15ad45d33eef780076c88618 Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 24 Jan 2018 11:15:45 +0100 Subject: [PATCH] Fixes issue #265 - Resize pictures before upload --- .../mastodon/activities/TootActivity.java | 65 +++++++++++++++---- .../mastodon/client/Entities/Status.java | 4 +- .../mastodon/fragments/SettingsFragment.java | 27 +++++++- .../gouv/etalab/mastodon/helper/Helper.java | 5 ++ .../res/layout-sw600dp/fragment_settings.xml | 19 ++++++ app/src/main/res/layout/fragment_settings.xml | 19 ++++++ app/src/main/res/values/strings.xml | 9 +++ 7 files changed, 129 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index 7e431eec6..1cd5dea76 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -89,8 +89,10 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.lang.ref.WeakReference; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -672,11 +674,22 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount } picture_scrollview.setVisibility(View.VISIBLE); try { + File photoFiletmp = createImageFile(false); InputStream inputStream = getContentResolver().openInputStream(fileUri); - toot_picture_container.setVisibility(View.VISIBLE); - picture_scrollview.setVisibility(View.VISIBLE); - toot_picture.setEnabled(false); - new HttpsConnection(TootActivity.this).upload(inputStream, TootActivity.this); + OutputStream output = new FileOutputStream(photoFile); + try { + byte[] buffer = new byte[4 * 1024]; // or other buffer size + int read; + + assert inputStream != null; + while ((read = inputStream.read(buffer)) != -1) { + output.write(buffer, 0, read); + } + output.flush(); + } finally { + output.close(); + } + new asyncPicture(TootActivity.this, photoFiletmp).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); count++; } catch (Exception e) { Toast.makeText(getApplicationContext(), R.string.toot_select_image_error, Toast.LENGTH_LONG).show(); @@ -699,7 +712,7 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go try { - photoFile = createImageFile(); + photoFile = createImageFile(true); } catch (IOException ignored) {Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();} // Continue only if the File was successfully created if (photoFile != null) { @@ -713,11 +726,11 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount } - private File createImageFile() throws IOException { + private File createImageFile(boolean external) throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; - File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); + File storageDir = external?getExternalFilesDir(Environment.DIRECTORY_PICTURES):getCacheDir(); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ @@ -741,9 +754,26 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount try { //noinspection ConstantConditions InputStream inputStream = getContentResolver().openInputStream(data.getData()); - toot_picture_container.setVisibility(View.VISIBLE); - toot_picture.setEnabled(false); - new HttpsConnection(TootActivity.this).upload(inputStream, TootActivity.this); + File photoFiletmp; + try { + photoFiletmp = createImageFile(false); + OutputStream output = new FileOutputStream(photoFiletmp); + try { + byte[] buffer = new byte[4 * 1024]; // or other buffer size + int read; + + assert inputStream != null; + while ((read = inputStream.read(buffer)) != -1) { + output.write(buffer, 0, read); + } + output.flush(); + new asyncPicture(TootActivity.this, photoFiletmp).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } finally { + output.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } } catch (FileNotFoundException e) { Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show(); toot_picture.setEnabled(true); @@ -756,9 +786,7 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount toot_content.setSelection(toot_content.getText().length()); } }else if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) { - new asyncPicture(TootActivity.this, photoFile).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } } @@ -778,8 +806,17 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount Bitmap takenImage = BitmapFactory.decodeFile(String.valueOf(this.fileWeakReference.get())); int size = takenImage.getByteCount(); - //Resize image to 2 meg - double resize = ((double)size)/((double)16777216); + SharedPreferences sharedpreferences = this.activityWeakReference.get().getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + int resizeSet = sharedpreferences.getInt(Helper.SET_PICTURE_RESIZE, Helper.S_1MO); + double resizeby = 1; + if( resizeSet == Helper.S_512KO){ + resizeby = 4194304; + }else if(resizeSet == Helper.S_1MO){ + resizeby = 8388608; + }else if(resizeSet == Helper.S_2MO){ + resizeby = 16777216; + } + double resize = ((double)size)/resizeby; Bitmap newBitmap; if( resize > 1 ){ newBitmap = Bitmap.createScaledBitmap(takenImage, (int)(takenImage.getWidth()/resize), diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java index 7e5178e6a..00502e71c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java @@ -444,7 +444,7 @@ public class Status implements Parcelable{ public void makeClickable(Context context){ - if( ((Activity)context).isFinishing() ) + if( ((Activity)context).isFinishing() || status == null) return; SpannableString spannableStringContent, spannableStringCW; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) @@ -466,7 +466,7 @@ public class Status implements Parcelable{ public void makeClickableTranslation(Context context){ - if( ((Activity)context).isFinishing() ) + if( ((Activity)context).isFinishing() || status == null) return; SpannableString spannableStringTranslated; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java index 11681642c..b00c49658 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java @@ -70,7 +70,7 @@ public class SettingsFragment extends Fragment { private Context context; private static final int ACTIVITY_CHOOSE_FILE = 411; private TextView set_folder; - int count2 = 0; + int count1, count2 = 0; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -520,6 +520,7 @@ public class SettingsFragment extends Fragment { } }); + //Translators final Spinner translation_layout_spinner = rootView.findViewById(R.id.translation_layout_spinner); ArrayAdapter adapterTrans = ArrayAdapter.createFromResource(getContext(), R.array.settings_translation, android.R.layout.simple_spinner_item); @@ -569,8 +570,28 @@ public class SettingsFragment extends Fragment { } }); - - + //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_1MO); + resize_layout_spinner.setSelection(positionSpinnerResize); + resize_layout_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + if( count1 > 0){ + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putInt(Helper.SET_PICTURE_RESIZE, position); + editor.apply(); + }else { + count1++; + } + } + @Override + public void onNothingSelected(AdapterView parent) { + } + }); return rootView; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index f81d48ae3..2a392bc88 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -224,6 +224,11 @@ public class Helper { public static final String SET_LIVE_NOTIFICATIONS = "set_live_notifications"; public static final String SET_DISABLE_GIF = "set_disable_gif"; public static final String SET_CAPITALIZE = "set_capitalize"; + public static final String SET_PICTURE_RESIZE = "set_picture_resize"; + public static final int S_NONE = 0; + public static final int S_512KO = 1; + public static final int S_1MO = 2; + public static final int S_2MO = 3; public static final int ATTACHMENT_ALWAYS = 1; public static final int ATTACHMENT_WIFI = 2; public static final int ATTACHMENT_ASK = 3; diff --git a/app/src/main/res/layout-sw600dp/fragment_settings.xml b/app/src/main/res/layout-sw600dp/fragment_settings.xml index 75b4e7868..daaa1a261 100644 --- a/app/src/main/res/layout-sw600dp/fragment_settings.xml +++ b/app/src/main/res/layout-sw600dp/fragment_settings.xml @@ -142,6 +142,25 @@ android:text="@string/show_boost_count" android:layout_height="wrap_content" /> + + + + + + + + + + + + + No + + No + 512 Ko + 1 Mo + 2 Mo + + Set LED colour: @@ -366,6 +373,8 @@ Search First letter in capital for replies + Resize pictures + Push notifications