코드로 안드로이드에 설치된 갤러리앱에서 이미지를 선택해 가져오고싶어요.

조회수 10149회

제 앱에서 안드로이드에 설치된 갤러리앱의 사진을 선택해 열고싶은데 사진의 URI를 가지고 있을때 어떻게 할수 있을까요?

1 답변

  • 좋아요

    1

    싫어요
    채택 취소하기

    사진 한개 선택

    public class BrowsePictureActivity extends Activity {
    
        // this is the action code we use in our intent, 
        // this way we know we're looking at the response from our own action
        private static final int SELECT_PICTURE = 1;
    
        private String selectedImagePath;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            findViewById(R.id.Button01)
                    .setOnClickListener(new OnClickListener() {
    
                        public void onClick(View arg0) {
                            // 사진 선택 
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent,
                                    "Select Picture"), SELECT_PICTURE);
                        }
                    });
        }
    
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {
                    Uri selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                }
            }
        }
    
        /**
         * 사진의 URI 경로를 받는 메소드 
         */
        public String getPath(Uri uri) {
                // uri가 null일경우 null반환 
                if( uri == null ) {
                    return null;
                }
                // 미디어스토어에서 유저가 선택한 사진의 URI를 받아온다. 
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                if( cursor != null ){
                    int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    return cursor.getString(column_index);
                }
                // URI경로를 반환한다. 
                return uri.getPath();
        }
    
    }
    

    여러개 사진 선택가능하게할때

    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 이런식으로 intent에 MULTIPLE을 허용해준다.

    if (Intent.ACTION_SEND_MULTIPLE.equals(data.getAction()))
            && Intent.hasExtra(Intent.EXTRA_STREAM)) {
        // retrieve a collection of selected images
        ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        // iterate over these images
        if( list != null ) {
           for (Parcelable parcel : list) {
             Uri uri = (Uri) parcel;
             // TODO handle the images one by one here
           }
       }
    } 
    

    그리고 Result부분에 이 소스를 추가해준다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)