diff --git a/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java b/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java index 0ef3a73..f6f3965 100644 --- a/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java +++ b/app/src/main/java/com/example/cosmosclient/views/SignupActivity.java @@ -41,6 +41,7 @@ private Button MakeAccountButton; private ImageView iconChoice; private String iconImageBase64; + private int quality = 70; @Override protected void onCreate(Bundle savedInstanceState) { @@ -185,27 +186,21 @@ //画像をBase64に変換 ByteArrayOutputStream baos = new ByteArrayOutputStream(); Bitmap afterResizeBitmap = Bitmap.createScaledBitmap(image,50,50,true); - afterResizeBitmap.compress(Bitmap.CompressFormat.JPEG,70, baos); + afterResizeBitmap.compress(Bitmap.CompressFormat.JPEG,quality, baos); byte[] b = baos.toByteArray(); String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT); - if(imageEncoded.length() <= 2000){ - iconChoice.setImageBitmap(image); - iconImageBase64 = imageEncoded; - }else{ - //byte→stringで2000文字以上なら、圧縮率を上げて再変換 - afterResizeBitmap.compress(Bitmap.CompressFormat.JPEG,60, baos); + while (imageEncoded.length() >2000){ + //文字数の2000文字以下になるまで圧縮率を上げて再度変換 + baos = new ByteArrayOutputStream(); + quality -=5; + afterResizeBitmap.compress(Bitmap.CompressFormat.JPEG,quality, baos); b = baos.toByteArray(); imageEncoded = Base64.encodeToString(b, Base64.DEFAULT); - if(imageEncoded.length() <=2000){ - iconChoice.setImageBitmap(image); - iconImageBase64 = imageEncoded; - }else{ - Toast.makeText(SignupActivity.this, - "この画像は使用できません。\n別の画像を選択してください。",Toast.LENGTH_LONG).show(); - } } + iconChoice.setImageBitmap(image); + iconImageBase64 = imageEncoded; }catch(IOException e){ e.printStackTrace(); }