Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

쨍쨍

AI 심리 분석 프로그램 도담이 본문

프로그래밍 코드/Android Studio

AI 심리 분석 프로그램 도담이

이선선 2024. 7. 22. 17:34

[ "SW 중심대학 디지털 경진대회 : SW 부문 ]

 

같은 연구실 사람들과 "SW 중심대학 디지털 경진대회 : SW 부문" 대회에 나가기 위해 AI 심리 분석 프로그램 도담이를 만들게 되었다. 먼저 예선 통과하기 위해서 주제를 정하고, 어떻게 생성AI를 사용할지, 어떤 어플리케이션을 만들지에 대한 회의를 했다.

 

팀원은 총 4명이며, 역할은 3가지(로고 디자인 및 PPT 작성 - 1명, 인공지능 - 2명, 어플리케이션 제작 - 1명)로 분류했다.

나는 그 중 어플리케이션 제작을 맡았다.

 

안드로이드 스튜디오 앱을 제작하면서 내가 가장 흥미를 느끼는 부분은 UI를 디자일 할 때이다. 내가 원하는 디자인으로 어플을 제작할 수 있고, 직관적으로 보이는 부분이기 때문에 더 흥미를 느끼는 것 같다. 그리고 새로운 앱을 만들 때 가장 먼저 하는 작업이기에 새로운 작업이라는 마음에 더 흥미를 느끼는 것일 수도 있다.

 

먼저 심리 상담 프로그램을 찾는 사람은 심리적으로 건강한 사람보다는 힘든 사람이 더 많이 사용할 것이라고 생각했기에 상담과 어울리는 따듯한 색인 노랑색을 메인 색상으로 잡고 디자인했다.

 

어플을 시작하면 도담이(어플이름) 로고가 먼저 나오고, 사용자의 나이와 성별을 입력하도록 만들었다. 그 후에는 사용할 테스트인  HTP 상담 설명이 나오도록 했고,  사용자가 직접 그린 집, 나무, 사람 그림을 첨부할 수 있도록 만들었으며, 그 이후로는 AI가 사용자가 첨부한 그림을 해석해서 사용자의 심리 상담 결과를 보여주도록 앱을 제작했다.

 

특히 사용자가 직접 그린 집, 나무, 사람 그림을 첨부하는 기능을 만드는 것을 내가 앱을 제작하면서 처음해보는 부분이였기 때문에 새로운 기능을 추가하고 배우는 것이 너무 흥미롭고 재미있었다. 새로운 기능을 배울 때 코드에 오류가 많이 생길까 걱정을 많이 했지만, 큰 오류 없이 어플에 이미지를 첨부하는 기능을 추구할 수 있어서 너무 만족스러웠고, 코드 작성에 더욱 자신감이 생겼다. 

 

처음으로 대회에 나갔는데 학교에서 진행되는 예선에서 1등을 했기에 뿌듯하고 코딩에 대한 흥미가 더 생겼다. 본선에 진출했을 때는 순위권에 들지는 못해 아쉬웠지만 더 열심히 공부해서 다음에 또 대회에 나가게 되면 더 좋은 결과를 만들어봐야겠다는 다짐을 했다.  대회기간동안 팀원들과 큰 트러블 없이 커뮤니케이션하며 같은 목표를 위해서 어플을 제작할 수 있어서 좋았다.

 

 

 

 

 

 

 

[ activity_main.xml ]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/yellow1"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".EnterActivity">
    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="350dp"
        android:layout_height="580dp"
        android:background="@drawable/ground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:hint="그림으로 알아보는 나의 심리 상태"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="#FF000000"
            android:layout_marginTop="100dp"/>

    <TextView
        android:id="@+id/name2"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:gravity="center"
        android:hint="도담이"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textColor="#FF000000" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/dodami2" />

    <Button
        android:id="@+id/enterButton"
        android:layout_width="150dp"
        android:layout_height="70dp"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:background="@drawable/button_1"
        android:layout_marginStart="100dp"
        android:textSize="20dp"
        android:text="시작하기" />

    </LinearLayout>

    <TextView
        android:id="@+id/nam"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:hint="※ 정확한 검사 및 진단은 전문가와의 상담이 필요합니다."
        android:textSize="15dp"
        android:textStyle="bold"
        android:textColor="#DEE2E6"
        android:layout_marginTop="5dp"/>
</LinearLayout>

 

 

[ EnterActivity ]

package com.example.dodami;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class EnterActivity extends AppCompatActivity {
    private Button startButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = findViewById(R.id.enterButton);

        startButton.setOnClickListener(v -> {
            Intent intent = new Intent(EnterActivity.this, SettingActivity.class);
            startActivity(intent);
        });
    }
}

 

 

 

 

[ activity_setting ]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/yellow1"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingActivity">
    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="350dp"
        android:layout_height="600dp"
        android:background="@drawable/ground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/setting1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="사용자님의 연령과 성별을"
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/setting2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="입력해주세요."
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="25dp" />

        <LinearLayout
            android:layout_marginTop="100dp"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/white"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/setting3"
                android:layout_marginLeft="70dp"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:gravity="center"
                android:text="나이 :"
                android:textColor="#FF000000"
                android:textStyle="bold"
                android:textSize="22dp" />

            <EditText
                android:id="@+id/setting4"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#FF000000" />

    </LinearLayout>

        <RadioGroup
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/genderGroup"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:layout_marginTop="10dp">

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="여성"
                android:textSize="22dp"

                android:id="@+id/genderWoman"
                android:layout_weight="1"
                android:checked="true"/>

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="남성"
                android:textSize="22dp"
                android:id="@+id/genderMan"
                android:layout_weight="1"/>

        </RadioGroup>


        <Button
            android:id="@+id/enterButton"
            android:layout_width="140dp"
            android:layout_height="50dp"
            android:layout_marginStart="100dp"
            android:layout_marginTop="50dp"
            android:background="@drawable/button_1"
            android:gravity="center"
            android:textSize="20dp"
            android:text="ENTER" />

    </LinearLayout>

</LinearLayout>

 

 

[ SettingActivity ]

package com.example.dodami;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class SettingActivity extends AppCompatActivity {

    private EditText ageEditText;
    private RadioGroup genderGroup;
    private RadioButton selectedGenderButton;
    private Button enterButton;
    private TextView setting1, setting2, setting3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        // XML 레이아웃 요소들 초기화
        ageEditText = findViewById(R.id.setting4);
        genderGroup = findViewById(R.id.genderGroup);
        enterButton = findViewById(R.id.enterButton);
        setting1 = findViewById(R.id.setting1);
        setting2 = findViewById(R.id.setting2);
        setting3 = findViewById(R.id.setting3);

        enterButton.setOnClickListener(v -> {
            String age = ageEditText.getText().toString();
            int selectedGenderId = genderGroup.getCheckedRadioButtonId();
            selectedGenderButton = findViewById(selectedGenderId);

            if (age.isEmpty()) {
                Toast.makeText(SettingActivity.this, "나이를 입력해주세요.", Toast.LENGTH_SHORT).show();
                return;
            }

            String gender = selectedGenderButton.getText().toString();
            Toast.makeText(SettingActivity.this, "나이: " + age + " 세" + "\n성별: " + gender, Toast.LENGTH_SHORT).show();

            // ChooseActivity로 전환
            Intent intent = new Intent(SettingActivity.this, ChooseActivity.class);
            startActivity(intent);
        });
    }
}

 

 

 

 

[ activity_choose ]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/yellow1"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".EnterActivity">
    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="350dp"
        android:layout_height="600dp"
        android:background="@drawable/ground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="HTP 검사를 시작합니다."
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="28dp" />

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="HTP 검사는 검사자에게 집, 나무, 사람을"
            android:gravity="center"
            android:layout_marginTop="80dp"
            android:textColor="#FF000000"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/text3"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="차례로 그리게 한 뒤 그림 해석을 통해 검사자의"
            android:gravity="center"
            android:textColor="#FF000000"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="심리상태를 분석합니다."
            android:gravity="center"
            android:textColor="#FF000000"
            android:textSize="15dp" />

        <Button
            android:id="@+id/startButton"
            android:layout_width="150dp"
            android:layout_height="70dp"
            android:layout_marginTop="80dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="시작하기" />

    </LinearLayout>
</LinearLayout>

 

 

[ ChooseActivity ]

package com.example.dodami;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class ChooseActivity extends AppCompatActivity {

        private Button start_Button;
    private View startButton;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_choose);

            startButton = findViewById(R.id.startButton);

            startButton.setOnClickListener(v -> {
                Intent intent = new Intent(ChooseActivity.this, HouseActivity.class);
                startActivity(intent);
            });

        }
    }

 

 

 

 

[ activity_house ]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/yellow1"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingActivity">
    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="350dp"
        android:layout_height="600dp"
        android:background="@drawable/ground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="집을 그려서 이미지 파일로"
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/title2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="첨부해 주세요."
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="22dp" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="16dp"
            android:contentDescription="Selected Image" />

        <Button
            android:id="@+id/selectImageButton"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="사진 선택" />


        <Button
            android:id="@+id/enterButton"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="다음" />
    </LinearLayout>
</LinearLayout>

 

 

[ HouseActivity ]

package com.example.dodami;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class HouseActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_READ_STORAGE = 1;
    private static final int PICK_IMAGE_REQUEST = 2;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house);

        imageView = findViewById(R.id.imageView);

        Button selectImageButton = findViewById(R.id.selectImageButton);
        selectImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestStoragePermission();
            }
        });

        Button enterButton = findViewById(R.id.enterButton);
        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TreeActivity로 이동
                Intent intent = new Intent(HouseActivity.this, TreeActivity.class);
                startActivity(intent);
            }
        });
    }

    private void requestStoragePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_READ_STORAGE);
        } else {
            openGallery();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_READ_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openGallery();
            } else {
                Toast.makeText(this, "저장소 접근 권한이 필요합니다.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                imageView.setImageURI(selectedImageUri); // 선택된 이미지를 이미지뷰에 표시
            }
        }
    }
}

 

 

 

 

 

[ activity_tree ]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/yellow1"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingActivity">
    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="350dp"
        android:layout_height="600dp"
        android:background="@drawable/ground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="나무를 그려서 이미지 파일로"
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/title2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="첨부해 주세요."
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="22dp" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="16dp"
            android:contentDescription="Selected Image" />

        <Button
            android:id="@+id/selectImageButton"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="사진 선택" />


        <Button
            android:id="@+id/enterButton"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="다음" />
    </LinearLayout>
</LinearLayout>

 

 

[ TreeActivity ]

package com.example.dodami;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class TreeActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_READ_STORAGE = 1;
    private static final int PICK_IMAGE_REQUEST = 2;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree);

        imageView = findViewById(R.id.imageView);

        Button selectImageButton = findViewById(R.id.selectImageButton);
        selectImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestStoragePermission();
            }
        });

        Button enterButton = findViewById(R.id.enterButton);
        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // PeopleActivity로 이동
                Intent intent = new Intent(TreeActivity.this, PeopleActivity.class);
                startActivity(intent);
            }
        });
    }

    private void requestStoragePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_READ_STORAGE);
        } else {
            openGallery();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_READ_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openGallery();
            } else {
                Toast.makeText(this, "저장소 접근 권한이 필요합니다.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                imageView.setImageURI(selectedImageUri); // 선택된 이미지를 이미지뷰에 표시
            }
        }
    }
}

 

 

 

 

 

[ activitiy_people ]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/yellow1"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingActivity">
    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="350dp"
        android:layout_height="600dp"
        android:background="@drawable/ground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="사람을 그려서 이미지 파일로"
            android:layout_marginTop="100dp"
            android:gravity="center"
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/title2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text="첨부해 주세요."
            android:textColor="#FF000000"
            android:textStyle="bold"
            android:textSize="22dp" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="16dp"
            android:contentDescription="Selected Image" />

        <Button
            android:id="@+id/selectImageButton"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="사진 선택" />


        <Button
            android:id="@+id/enterButton"
            android:layout_width="150dp"
            android:layout_height="60dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:background="@drawable/button_1"
            android:layout_marginStart="100dp"
            android:textSize="20dp"
            android:text="다음" />
    </LinearLayout>
</LinearLayout>

 

 

[ PeopleActivity ]

package com.example.dodami;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PeopleActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_READ_STORAGE = 1;
    private static final int PICK_IMAGE_REQUEST = 2;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_people);

        imageView = findViewById(R.id.imageView);

        Button selectImageButton = findViewById(R.id.selectImageButton);
        selectImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestStoragePermission();
            }
        });

        Button enterButton = findViewById(R.id.enterButton);
        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TreeActivity로 이동
                Intent intent = new Intent(PeopleActivity.this, ResultActivity.class);
                startActivity(intent);
            }
        });
    }

    private void requestStoragePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_READ_STORAGE);
        } else {
            openGallery();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_READ_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openGallery();
            } else {
                Toast.makeText(this, "저장소 접근 권한이 필요합니다.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                imageView.setImageURI(selectedImageUri); // 선택된 이미지를 이미지뷰에 표시
            }
        }
    }
}

 

'프로그래밍 코드 > Android Studio' 카테고리의 다른 글

#5. Al Smart Home_HomeActivty  (3) 2024.12.17
#4. AI Smart Home_SocketActivity  (3) 2024.12.17
#3. AI Smart Home_MainActivity  (0) 2024.03.13
#2. AI Smart Home_RegisterActivity  (0) 2024.03.13
#1. AI Smart Home_LoginActivity  (0) 2024.03.13