diff --git a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java index 458568c..cf6713f 100644 --- a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java +++ b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java @@ -27,6 +27,9 @@ import com.example.nemophila.entities.Account; import com.example.nemophila.viewmodels.AccountViewModel; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileNotFoundException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; @@ -45,15 +48,25 @@ private ExecutorService executor; private Handler handler; private String iconUrlString; - + private boolean iconFlag; //マイページ編集画面の作成 private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), new ActivityResultCallback() { @Override public void onActivityResult(Uri uri) { if(uri == null)return; - icon = new String(Base64.getEncoder().encode(uri.toString().getBytes())); ImageButton changeIconButton = findViewById(R.id.changeIconButton); changeIconButton.setImageURI(uri); + try { + InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(uri); + Bitmap bitmap = BitmapFactory.decodeStream(new BufferedInputStream(inputStream)); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); + byte[] byteArray = stream.toByteArray(); + icon = Base64.getEncoder().encodeToString(byteArray); + iconFlag = true; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } } }); @@ -70,7 +83,8 @@ iconButton = findViewById(R.id.changeIconButton); executor = Executors.newSingleThreadExecutor(); handler = new Handler(); - iconUrlString = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/test01.jpg"; + iconUrlString = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/icon"+uid+".jpg"; + iconFlag = false; // アカウントのアイコンを表示 setIcon(iconButton); @@ -84,7 +98,16 @@ launcher.launch(new String[] {"image/*"}); } }); - + Button changeDefaultButton = findViewById(R.id.changeDefaultButton); + changeDefaultButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + icon = null; + iconUrlString = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/test01.jpg"; + setIcon(iconButton); + iconFlag = true; + } + }); //変更確定ボタンを押した場合の動き Button changeDecisionButton = findViewById(R.id.changeConfirm); changeDecisionButton.setOnClickListener(new View.OnClickListener() { @@ -107,8 +130,8 @@ accountViewModel.changePw(uid, oldPw, newPw, token); } // アイコンが入力されているとき通信を行う - if(icon != null){ - accountViewModel.changeIcon(uid, icon, token); + if(iconFlag){ + accountViewModel.changeIcon(uid, icon, token); } } @@ -214,6 +237,8 @@ con.disconnect(); }catch (Exception e){ e.printStackTrace(); + iconUrlString = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/test01.jpg"; + setIcon(iv); } }); }