본문 바로가기

개발/Android 오픈소스

[안드로이드] 유용한 라이브러리 - 간단한 이미지 단일/다중 선택 Y-PhotoPicker



안드로이드에서 사용자의 갤러리/카메라에서 사진을 단일,다중으로 가져올 수 있고, 전용 뷰어를 통해 Crop까지 가능하도록 추가하였습니다.

계속 해서 문제점과 기능들을 보강하여 관리할 예정입니다.




Android-Y-PhotoPicker

안드로이드용 이미지 선택기 입니다.

다중/단일 선택을 지원하며 기본으로 pager 이미지 뷰어를 지원합니다.

기존 Urcrop 기능을 제공했었으나, 0.0.2 이후부터 제거되었습니다.

API 

스크린샷

 

설정

1. app.gradle 에 y-photopicker 를 import 해줍니다.

  allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  compile 'com.github.yongbeam:Android-Y-PhotoPicker:1.0.0'


2. 프로젝트의  AndroidManifest.xml 에 아래 두개의 액티비티를 추가해줍니다. 

<activity android:name="com.yongbeam.y_photopicker.util.photopicker.PhotoPickerActivity" android:theme="@style/Theme.AppCompat.NoActionBar" /> <activity android:name="com.yongbeam.y_photopicker.util.photopicker.PhotoPagerActivity" android:theme="@style/Theme.AppCompat.NoActionBar"/>


3. y-photopicker 를 사용하기 위해선 아래 권한이 필요합니다. AndroidManifest.xml 에 아래 권한을 추가해주세요.

( y-photopicker 는 Android 6.0 이상에서 권한동의창을 띄워주고 있습니다. 별도로 처리하실 필요는 없습니다 )

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />


4. Y-PhotoPicker는 Intent 방식으로 화면을 호출하고 결과를 받습니다.

아래 옵션을 입맛에 맞게 사용하세요

setMaxSelectCount : 한번에 선택할 수 있는 최대 이미지 갯수를 지정합니다. ( 기본값 false )

setShowCamera : 카메라 버튼의 표시여부를 설정합니다( 기본값 true )

setShowGif : 이미지 선택시 gif 이미지를 포함할건지 지정합니다( gif 플레이도 이루어집니다 )

setSelectCheckBox : 사진 선택시 테두리의 색이변합니다. 이옵션을 켜면 추가로 체크박스가 보여집니다

setMaxGrideItemCount : 한줄에 몇개의 이미지가 보여질건지 설정합니다( 기본값 3 )

Intent 로 호출하고 결과를 받을때는 onActivityResult 로 받습니다 그때문에 호출시 startActivityForResult 를 사용해주세요

( 조만간 자체 콜백으로 기능이 변경될 예정입니다 )

      YPhotoPickerIntent intent = new YPhotoPickerIntent(this);
      intent.setMaxSelectCount(20);
      intent.setShowCamera(true);
      intent.setShowGif(true);
      intent.setSelectCheckBox(false);
      intent.setMaxGrideItemCount(3);
      startActivityForResult(intent, REQUEST_CODE);


5. 선택된 이미지에 대한 결과를 받이 위해서 onActivityResult 를 오버라이드 해주세요.

이미지의 경로가 Array<String> 타입으로 주도록 되어있으며, 선택한 순서가 보장됩니다.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        List<String> photos = null;
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
        }
    }


Y-PhotoPicker 내장 이미지뷰어 사용

    // start image viewr
    Intent startActivity = new Intent(this , PhotoPagerActivity.class);
    startActivity.putStringArrayListExtra("photos" , photos);
    startActivity(startActivity);

라이센스

Copyright 2016-2018 LeeYongBeom

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



github에서 보기