반응형

안드로이드 파일선택과 관련한 View와 Java를 빠르게 살펴보자.

Android View XML

  • 선택한 이미지를 보여주는 ImageView와 이미지 선택 클릭을 위한 Button 하나면 된다.

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout 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:layout_height="match_parent"
      tools:context=".MainActivity">
    
      <Button
          android:id="@+id/btnImageSelection"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_x="316dp"
          android:layout_y="682dp"
          android:text="이미지 선택"
          tools:layout_editor_absoluteX="8dp"
          tools:layout_editor_absoluteY="667dp" />
    
      <ImageView
          android:id="@+id/imgVwSelected"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginStart="8dp"
          android:layout_marginEnd="8dp"
          android:layout_x="0dp"
          android:layout_y="0dp"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          tools:layout_editor_absoluteY="13dp"
          tools:srcCompat="@tools:sample/backgrounds/scenic" />
    

Activity Class

  • Intent 이미지선택으로 가져온 데이터는 Bitmap 받을 수 있다.

  • 기본적으로 Intent에서 getData() 를 통해 가져올 때에는 ImageUri 를 받아오는데 이를 바로 ImageView에 세팅해주어도 된다.

  • 둘 중 하나를 필요에 따라 선택하자

    package com.derveljun.fileupload;
    
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    public class MainActivity extends AppCompatActivity {
    
        ImageView imgVwSelected;
        Button btnImageSend, btnImageSelection;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnImageSend = findViewById(R.id.btnImageSend);
    
            btnImageSelection = findViewById(R.id.btnImageSelection);
            btnImageSelection.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    // Intent를 통해 이미지를 선택
                    Intent intent = new Intent();
                    // intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent, 1);
                }
            });
    
            imgVwSelected = findViewById(R.id.imgVwSelected);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data){
            if (requestCode != 1 || resultCode != RESULT_OK) {
                return;
            }
           // 비트맵으로 받는 방법
            try {
                InputStream in = getContentResolver().openInputStream(data.getData());
                Bitmap image = BitmapFactory.decodeStream(in);
    
                imgVwSelected.setImageBitmap(image);
    
                in.close();
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }
    
            // URI로 받는 방법
            imgVwSelected.setImageURI(data.getData());
        }
    }
    

결과

반응형

WRITTEN BY
데르벨준

,