Fixes issue #265 - Resize pictures before upload

This commit is contained in:
stom79 2018-01-24 11:15:45 +01:00
parent 66e19d9ebd
commit 3184f375db
7 changed files with 129 additions and 19 deletions

View File

@ -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),

View File

@ -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)

View File

@ -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<CharSequence> 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<CharSequence> 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;
}

View File

@ -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;

View File

@ -142,6 +142,25 @@
android:text="@string/show_boost_count"
android:layout_height="wrap_content" />
<!-- Resize pictures -->
<LinearLayout
android:layout_marginTop="10dp"
android:id="@+id/resize_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_resize_picture"/>
<Spinner
android:id="@+id/set_resize_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- TABS Layout -->
<LinearLayout
android:id="@+id/translation_layout_container"

View File

@ -141,6 +141,25 @@
android:text="@string/show_boost_count"
android:layout_height="wrap_content" />
<!-- Resize pictures -->
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_resize_picture"/>
<Spinner
android:id="@+id/set_resize_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- Translation engine -->
<LinearLayout
android:layout_marginTop="10dp"

View File

@ -339,6 +339,13 @@
<item>No</item>
</string-array>
<string-array name="settings_resize_picture">
<item>No</item>
<item>512 Ko</item>
<item>1 Mo</item>
<item>2 Mo</item>
</string-array>
<string name="set_led_colour">Set LED colour:</string>
<string-array name="led_colours">
@ -366,6 +373,8 @@
<string name="action_search">Search</string>
<string name="set_capitalize">First letter in capital for replies</string>
<string name="set_resize_picture">Resize pictures</string>
<!-- Quick settings for notifications -->
<string name="settings_popup_title">Push notifications</string>
<string name="settings_popup_message">