diff --git a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java index a52e03b..458568c 100644 --- a/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java +++ b/app/src/main/java/com/example/nemophila/MyPageEditorActivity.java @@ -10,8 +10,11 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.view.Gravity; import android.view.View; import android.widget.Button; @@ -24,14 +27,24 @@ import com.example.nemophila.entities.Account; import com.example.nemophila.viewmodels.AccountViewModel; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Base64; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class MyPageEditorActivity extends AppCompatActivity { + //フィールド private String icon; private Nemophila nemophila; private String uid; private String token; + private ImageButton iconButton; + private ExecutorService executor; + private Handler handler; + private String iconUrlString; //マイページ編集画面の作成 private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), new ActivityResultCallback() { @@ -49,26 +62,32 @@ super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_page_editor); - // フィールド + // 初期化 AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class); nemophila = (Nemophila) getApplication(); uid = nemophila.getUid(); token = nemophila.getToken(); + 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"; + + // アカウントのアイコンを表示 + setIcon(iconButton); //MyPageに戻るボタン ImageButton backButton = findViewById(R.id.backMyPage); backButton.setOnClickListener(v -> finish()); - ImageButton changeIconButton = findViewById(R.id.changeIconButton); - changeIconButton.setOnClickListener(new View.OnClickListener(){ + iconButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { launcher.launch(new String[] {"image/*"}); } }); //変更確定ボタンを押した場合の動き - Button changeNameButton = findViewById(R.id.changeConfirm); - changeNameButton.setOnClickListener(new View.OnClickListener() { + Button changeDecisionButton = findViewById(R.id.changeConfirm); + changeDecisionButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //名前とパスワードを取り込む EditText editName = findViewById(R.id.editName); @@ -178,4 +197,24 @@ } }); } + + // アイコンに画像を設定 + private void setIcon(ImageView iv) { + executor.execute(() -> { + try{ + URL url = new URL(iconUrlString); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.connect(); + + InputStream is = con.getInputStream(); + + Bitmap bitmap = BitmapFactory.decodeStream(is); + handler.post(() -> iv.setImageBitmap(bitmap)); + is.close(); + con.disconnect(); + }catch (Exception e){ + e.printStackTrace(); + } + }); + } }