Change the way to edit pictures

This commit is contained in:
stom79 2018-04-22 13:16:52 +02:00
parent 0195f69a9b
commit bbea68eb66
5 changed files with 234 additions and 156 deletions

View File

@ -20,6 +20,7 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -58,9 +59,12 @@ import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountInfoAsyncTask;
@ -446,54 +450,48 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_HEADER && resultCode == Activity.RESULT_OK) {
if (data == null) {
if (data == null || data.getData() == null) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
return;
}
ByteArrayInputStream bs = Helper.compressImage(EditProfileActivity.this, data.getData(), Helper.MediaType.MEDIA);
assert bs != null;
int n = bs.available();
byte[] bytes = new byte[n];
//noinspection ResultOfMethodCallIgnored
bs.read(bytes, 0, n);
String s;
ContentResolver cr = getContentResolver();
String mime = cr.getType(data.getData());
set_header_picture.setImageBitmap(profile_header_bmp);
try {
//noinspection ConstantConditions
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(data.getData());
BufferedInputStream bufferedInputStream;
if (inputStream != null) {
bufferedInputStream = new BufferedInputStream(inputStream);
}else {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
return;
}
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
profile_header_bmp = Bitmap.createScaledBitmap(bmp, 700, 335, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
profile_header_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
set_header_picture.setImageBitmap(profile_header_bmp);
byte[] byteArray = byteArrayOutputStream .toByteArray();
header_picture = "data:image/png;base64, " + Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (FileNotFoundException e) {
s = new String(bytes, "UTF-8");
header_picture = "data:"+mime+";base64, " + s;
} catch (UnsupportedEncodingException e) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}else if(requestCode == PICK_IMAGE_PROFILE && resultCode == Activity.RESULT_OK) {
if (data == null) {
if (data == null || data.getData() == null) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
return;
}
ByteArrayInputStream bs = Helper.compressImage(EditProfileActivity.this, data.getData(), Helper.MediaType.MEDIA);
assert bs != null;
int n = bs.available();
byte[] bytes = new byte[n];
//noinspection ResultOfMethodCallIgnored
bs.read(bytes, 0, n);
String s;
ContentResolver cr = getContentResolver();
String mime = cr.getType(data.getData());
set_profile_picture.setImageBitmap(profile_picture_bmp);
try {
@SuppressWarnings("ConstantConditions") InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(data.getData());
BufferedInputStream bufferedInputStream;
if (inputStream != null) {
bufferedInputStream = new BufferedInputStream(inputStream);
}else {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
return;
}
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
profile_picture_bmp = Bitmap.createScaledBitmap(bmp, 120, 120, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
profile_picture_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
set_profile_picture.setImageBitmap(profile_picture_bmp);
byte[] byteArray = byteArrayOutputStream .toByteArray();
profile_picture = "data:image/png;base64, " + Base64.encodeToString(byteArray, Base64.DEFAULT);
} catch (FileNotFoundException e) {
s = new String(bytes, "UTF-8");
profile_picture = "data:"+mime+";base64, " + s;
} catch (UnsupportedEncodingException e) {
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}

View File

@ -778,98 +778,7 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount
Toast.makeText(activityWeakReference.get(), R.string.toast_error, Toast.LENGTH_SHORT).show();
return null;
}
Bitmap takenImage;
try {
takenImage = MediaStore.Images.Media.getBitmap(activityWeakReference.get().getContentResolver(), uriFile);
} catch (IOException e) {
Toast.makeText(activityWeakReference.get(), R.string.toast_error, Toast.LENGTH_SHORT).show();
return null;
}
ExifInterface exif = null;
try (InputStream inputStream = this.activityWeakReference.get().getContentResolver().openInputStream(uriFile)) {
assert inputStream != null;
exif = new ExifInterface(inputStream);
} catch (IOException 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 = 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 = size;
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;
if( resize > 1 ){
ContentResolver cr = this.activityWeakReference.get().getContentResolver();
String mime = cr.getType(uriFile);
Bitmap newBitmap = Bitmap.createScaledBitmap(takenImage, (int) (takenImage.getWidth() / resize),
(int) (takenImage.getHeight() / resize), true);
Bitmap adjustedBitmap;
if( matrix != null)
adjustedBitmap = Bitmap.createBitmap(newBitmap, 0, 0, newBitmap.getWidth(), newBitmap.getHeight(), matrix, true);
else
adjustedBitmap = newBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if( mime !=null && (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 = this.activityWeakReference.get().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 {
try {
InputStream inputStream = this.activityWeakReference.get().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();
}
}
bs = Helper.compressImage(activityWeakReference.get(), uriFile, Helper.MediaType.MEDIA);
return null;
}

View File

@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.client;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
@ -156,19 +157,21 @@ public class API {
requestParams.put("note",note);
}
if( avatar != null)
Log.v(Helper.TAG,"avatar" + avatar);
try {
requestParams.put("avatar",URLEncoder.encode(avatar, "UTF-8"));
} catch (UnsupportedEncodingException e) {
requestParams.put("avatar",avatar);
}
if( privacy != null)
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
if( header != null)
try {
requestParams.put("header",URLEncoder.encode(header, "UTF-8"));
} catch (UnsupportedEncodingException e) {
requestParams.put("header",header);
}
if( privacy != null)
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
try {
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, prefKeyOauthTokenT);
} catch (HttpsConnection.HttpsConnectionException e) {

View File

@ -18,6 +18,7 @@ import android.content.SharedPreferences;
import android.os.Build;
import android.text.Html;
import android.text.SpannableString;
import android.util.Log;
import com.google.common.io.ByteStreams;
@ -40,6 +41,7 @@ import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
@ -141,7 +143,11 @@ public class HttpsConnection {
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -181,7 +187,11 @@ public class HttpsConnection {
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
response = new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
}else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -211,7 +221,11 @@ public class HttpsConnection {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
}else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -234,7 +248,11 @@ public class HttpsConnection {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
}else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -288,7 +306,11 @@ public class HttpsConnection {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -334,7 +356,11 @@ public class HttpsConnection {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -699,7 +725,11 @@ public class HttpsConnection {
if (200 != httpsURLConnection.getResponseCode()) {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -840,7 +870,11 @@ public class HttpsConnection {
if (200 != httpURLConnection.getResponseCode()) {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -932,7 +966,11 @@ public class HttpsConnection {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -980,7 +1018,11 @@ public class HttpsConnection {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -994,7 +1036,8 @@ public class HttpsConnection {
public String patch(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
@SuppressWarnings("SameParameterValue")
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
if( urlConnection.startsWith("https://")) {
URL url = new URL(urlConnection);
Map<String, Object> params = new LinkedHashMap<>();
@ -1036,19 +1079,23 @@ public class HttpsConnection {
httpsURLConnection.setDoOutput(true);
httpsURLConnection.getOutputStream().write(postDataBytes);
String response;
Log.v(Helper.TAG,"postDataBytes: " + postData.toString());
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
try {
httpsURLConnection.getInputStream().close();
}catch (Exception ignored){}
throw new HttpsConnectionException(responseCode, error);
}
getSinceMaxId();
httpsURLConnection.getInputStream().close();
return response;
}else {
URL url = new URL(urlConnection);
Map<String, Object> params = new LinkedHashMap<>();
@ -1083,19 +1130,19 @@ public class HttpsConnection {
httpURLConnection.setDoOutput(true);
httpURLConnection.getOutputStream().write(postDataBytes);
String response;
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
getSinceMaxId();
response = new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
} else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
}
getSinceMaxId();
httpURLConnection.getInputStream().close();
return response;
}
}
@ -1143,7 +1190,11 @@ public class HttpsConnection {
httpsURLConnection.getInputStream().close();
return httpsURLConnection.getResponseCode();
} else {
String error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);
@ -1188,7 +1239,11 @@ public class HttpsConnection {
httpURLConnection.getInputStream().close();
return httpURLConnection.getResponseCode();
}else {
String error = new String(ByteStreams.toByteArray(httpURLConnection.getErrorStream()));
String error = null;
if( httpsURLConnection.getErrorStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
else if( httpsURLConnection.getInputStream() != null)
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
int responseCode = httpURLConnection.getResponseCode();
httpURLConnection.getInputStream().close();
throw new HttpsConnectionException(responseCode, error);

View File

@ -23,12 +23,15 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
import android.support.media.ExifInterface;
import android.support.v4.app.FragmentActivity;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AlertDialog;
@ -66,6 +69,7 @@ import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Patterns;
import android.view.Menu;
import android.view.MenuItem;
@ -99,9 +103,11 @@ import com.google.gson.Gson;
import org.conscrypt.Conscrypt;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -132,6 +138,7 @@ import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.HashTagActivity;
import fr.gouv.etalab.mastodon.activities.LoginActivity;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.activities.MediaActivity;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.activities.WebviewActivity;
import fr.gouv.etalab.mastodon.asynctasks.RemoveAccountAsyncTask;
@ -1994,4 +2001,110 @@ public class Helper {
}
public enum MediaType{
MEDIA,
PROFILE
}
public static ByteArrayInputStream compressImage(Context context, android.net.Uri uriFile, MediaType mediaType){
Bitmap takenImage;
ByteArrayInputStream bs = null;
try {
takenImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uriFile);
} catch (IOException e) {
Toast.makeText(context, R.string.toast_error, Toast.LENGTH_SHORT).show();
return null;
}
ExifInterface exif = null;
try (InputStream inputStream = context.getContentResolver().openInputStream(uriFile)) {
assert inputStream != null;
exif = new ExifInterface(inputStream);
} catch (IOException 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_1MO);
if( mediaType == MediaType.PROFILE)
resizeSet = Helper.S_2MO;
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;
}
Log.v(Helper.TAG,"resizeby: " + resizeby);
double resize = ((double)size)/resizeby;
Log.v(Helper.TAG,"resize: " + resize);
if( resize > 1 ){
ContentResolver cr = context.getContentResolver();
String mime = cr.getType(uriFile);
Bitmap newBitmap = Bitmap.createScaledBitmap(takenImage, (int) (takenImage.getWidth() / resize),
(int) (takenImage.getHeight() / resize), true);
Bitmap adjustedBitmap;
if( matrix != null)
adjustedBitmap = Bitmap.createBitmap(newBitmap, 0, 0, newBitmap.getWidth(), newBitmap.getHeight(), matrix, true);
else
adjustedBitmap = newBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Log.v(Helper.TAG,"mime: " + mime);
if( mime !=null && (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 {
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();
}
}
return bs;
}
}