Album
android album
Chinese : wiki
Screenshot
multiple, radio, preview, crop, sample ui
加載失敗請(qǐng)移步至 Github 查看
圖片選擇器
選擇圖片,裁剪,拍照
簡(jiǎn)單使用
Manifests.xml 添加
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name="com.album.ui.activity.AlbumActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name="com.album.ui.activity.PreviewActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
gradle添加
// 如果項(xiàng)目中 依賴了 design 高版本包,那么可以不用單獨(dú)依賴 RecyclerView
// 因?yàn)?高版本的 design 已經(jīng)包含了 RecyclerView
compile "com.android.support:design:$designVersion"
or
compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
// 這兩個(gè)必須依賴,如果想使用 Glide 4.x ,請(qǐng)自行實(shí)現(xiàn) AlbumImageLoader
compile "com.github.yalantis:ucrop:$ucropVersion"
compile "com.github.bumptech.glide:glide:$glideVersion"
示例
Album
.getInstance()
.setAlbumModels(new ArrayList<AlbumModel>())
.setOptions(new UCrop.Options())
.setAlbumImageLoader(new SimpleAlbumImageLoader())
.setConfig(new AlbumConfig())
.setAlbumListener(new SimpleAlbumListener())
.start(this);
config
//是否隱藏相機(jī),默認(rèn)不隱藏
boolean hideCamera = false;
//是否單選 ,默認(rèn)多選
boolean isRadio = false;
//單選點(diǎn)擊是否開(kāi)啟裁剪,默認(rèn)開(kāi)啟,不開(kāi)啟直接返回當(dāng)前點(diǎn)擊的圖片路徑
boolean isCrop = true;
// 拍照之后是否直接裁剪,默認(rèn)不裁剪
boolean cameraCrop = false;
//預(yù)覽頁(yè)多選數(shù)據(jù)變動(dòng),使用者點(diǎn)擊 Toolbar 的 返回鍵 返回時(shí) 是否刷新多選數(shù)據(jù),默認(rèn)不刷新
boolean previewFinishRefresh = false;
//預(yù)覽頁(yè)多選數(shù)據(jù)變動(dòng),使用者點(diǎn)擊 Back 的 返回鍵 返回時(shí) 是否刷新多選數(shù)據(jù),默認(rèn)不刷新
boolean previewBackRefresh = false;
//權(quán)限被拒絕時(shí)是否直接 finish 掉 圖片選擇庫(kù) 的 Activity, 默認(rèn)不 finish
boolean isPermissionsDeniedFinish = false;
//是否是使用 Fresco 加載圖片,當(dāng)使用 Fresco 時(shí) 必須設(shè)置為 true,返回的 ImageView 就是 Fresco 的 SimpleDraweeView
boolean isFrescoImageLoader = false;
//多選最多可選擇幾個(gè)
int multipleMaxCount = 9;
//照相路徑
String cameraPath = null;
//裁剪路徑
String uCropPath = null;
//分頁(yè)加載時(shí)一次加載多少?gòu)垐D片
int count = 200;
// 圖片頁(yè) Activity 的 Toolbar 一些設(shè)置
int albumStatusBarColor = R.color.colorAlbumStatusBarColorDay;
int albumToolbarBackground = R.color.colorAlbumToolbarBackgroundDay;
int albumToolbarIcon = R.drawable.ic_action_back_day;
int albumToolbarIconColor = R.color.colorAlbumToolbarIconColorDay;
int albumToolbarTextColor = R.color.colorAlbumToolbarTextColorDay;
int albumToolbarText = R.string.album_name;
float albumToolbarElevation = 6f;
// 圖片頁(yè) Activity 底部的 View 一些設(shè)置
private int albumBottomViewBackground = R.color.colorAlbumBottomViewBackgroundDay;
private int albumBottomFinderTextSize = 16;
private int albumBottomFinderTextBackground = -1;
private int albumBottomFinderTextColor = R.color.colorAlbumBottomFinderTextColorDay;
private int albumBottomFinderTextCompoundDrawable = R.drawable.ic_action_album_finder_day;
private int albumBottomFinderTextDrawableColor = R.color.colorAlbumBottomFinderTextDrawableColorDay;
private int albumBottomPreViewText = R.string.album_preview;
private int albumBottomPreViewTextSize = 16;
private int albumBottomPreViewTextColor = R.color.colorAlbumBottomPreViewTextColorDay;
private int albumBottomPreviewTextBackground = -1;
private int albumBottomSelectText = R.string.album_select;
private int albumBottomSelectTextSize = 16;
private int albumBottomSelectTextColor = R.color.colorAlbumBottomSelectTextColorDay;
private int albumBottomSelectTextBackground = -1;
// 圖片頁(yè) Activity 文件夾目錄的 一些設(shè)置
int albumListPopupWidth = 600;
int albumListPopupHorizontalOffset = 0;
int albumListPopupVerticalOffset = 0;
int albumListPopupItemBackground = R.color.colorAlbumListPopupItemBackgroundDay;
int albumListPopupItemTextColor = R.color.colorAlbumListPopupItemTextColorDay;
// 圖片F(xiàn)ragment
private int spanCount = 3;
private int dividerWidth = 10;
private int albumContentViewCameraTips = R.string.album_image_camera_tv_tips;
private int albumContentViewCameraTipsSize = 18;
private int albumContentViewCameraTipsColor = R.color.colorAlbumContentViewTipsColorDay;
private int albumContentViewCameraBackgroundColor = R.color.colorAlbumContentViewBackgroundColorColorDay;
private int albumContentViewBackground = R.color.colorAlbumContentViewBackgroundDay;
private int albumContentViewCameraDrawable = R.drawable.ic_camera_alt_black_24dp;
private int albumContentViewCameraDrawableColor = R.color.colorAlbumContentViewCameraDrawableColorDay;
private int albumContentItemCheckBoxDrawable = R.drawable.selector_album_item_check;
private int albumContentEmptyDrawable = R.drawable.ic_camera_alt_black_24dp;
private int albumContentEmptyDrawableColor = R.color.colorAlbumContentEmptyDrawableColorDay;
//預(yù)覽頁(yè) 一些設(shè)置
private int albumPreviewTitle = R.string.preview_title;
private int albumPreviewBackground = R.color.colorAlbumPreviewBackgroundDay;
private int albumPreviewBottomViewBackground = R.color.colorAlbumPreviewBottomViewBackgroundDay;
private int albumPreviewBottomOkText = R.string.preview_select;
private int albumPreviewBottomOkTextColor = R.color.colorAlbumPreviewBottomViewOkColorDay;
private int albumPreviewBottomCountTextColor = R.color.colorAlbumPreviewBottomViewCountColorDay;
private int albumPreviewBottomOkTextSize = 16;
private int albumPreviewBottomCountTextSize = 16;
回調(diào)
Album 已經(jīng)放置了一個(gè) SimpleAlbumListener
public class SimpleAlbumListener implements AlbumListener {
/**
* 點(diǎn)擊圖片頁(yè)Toolbar finish
*/
@Override
public void onAlbumActivityFinish() {
}
/**
* 權(quán)限被拒絕
* type:
* <p>
* *** AlbumConstant.TYPE_ALBUM : 讀取權(quán)限被拒
* *** AlbumConstant.TYPE_CAMERA : 相機(jī)權(quán)限被拒
*/
@Override
public void onAlbumPermissionsDenied(int type) {
}
/**
* 點(diǎn)擊 預(yù)覽 或者 選擇 按鈕時(shí) Fragment 為 null, 應(yīng)該不會(huì)被觸發(fā)..
*/
@Override
public void onAlbumFragmentNull() {
}
/**
* 預(yù)覽時(shí),滑動(dòng) ViewPager 時(shí) 會(huì)檢測(cè)圖片是否還存在,不存在會(huì)觸發(fā)
* 觸發(fā)條件: Home 放在后臺(tái),在其他軟件中刪掉了圖片
*/
@Override
public void onAlbumPreviewFileNull() {
}
/**
* 圖片 文件夾 目錄 為空,就是沒(méi)有圖片..
*/
@Override
public void onAlbumFinderNull() {
}
/**
* 點(diǎn)擊預(yù)覽時(shí)沒(méi)有選擇圖片
*/
@Override
public void onAlbumBottomPreviewNull() {
}
/**
* 點(diǎn)擊選擇時(shí) 沒(méi)有選擇圖片
*/
@Override
public void onAlbumBottomSelectNull() {
}
/**
* 點(diǎn)擊 ITEM 時(shí),會(huì)判斷當(dāng)前圖片是否存在
* 觸發(fā)條件: Home 放在后臺(tái),在其他軟件中刪掉了圖片
*/
@Override
public void onAlbumFragmentFileNull() {
}
/**
* 預(yù)覽界面選擇確定,但是沒(méi)有選中的圖片
*/
@Override
public void onAlbumPreviewSelectNull() {
}
/**
* 多選點(diǎn)擊 checkbox 時(shí),會(huì)判斷當(dāng)前圖片是否存在
* 觸發(fā)條件: Home 放在后臺(tái),在其他軟件中刪掉了圖片
* <p>
* 此時(shí)可選擇是否強(qiáng)制刷新圖庫(kù)信息
*/
@Override
public void onAlbumCheckBoxFileNull() {
}
/**
* 裁剪被取消
*/
@Override
public void onAlbumFragmentCropCanceled() {
}
/**
* 拍照被取消
*/
@Override
public void onAlbumFragmentCameraCanceled() {
}
/**
* 裁剪出現(xiàn)異常
*/
@Override
public void onAlbumFragmentUCropError(@Nullable Throwable data) {
}
/**
* 返回的選擇圖片信息
* <p>
* albumModel.getPath(); 為當(dāng)前圖片地址
* <p>
* 建議 FileUtils.isFile 判斷一下圖片是否還存在
*/
@Override
public void onAlbumResources(@NonNull List<AlbumModel> list) {
}
/**
* 裁剪之后會(huì)返回的圖片F(xiàn)ile
*/
@Override
public void onAlbumUCropResources(@Nullable File scannerFile) {
}
/**
* 多選最多了
*/
@Override
public void onAlbumMaxCount() {
}
/**
* 點(diǎn)擊 back 返回到app
*/
@Override
public void onAlbumActivityBackPressed() {
}
/**
* 打開(kāi)相機(jī)失敗,有可能是當(dāng)前系統(tǒng)沒(méi)有相機(jī)
*/
@Override
public void onAlbumOpenCameraError() {
}
// 本機(jī)沒(méi)有圖片
@Override
public void onAlbumEmpty() {
}
}
自定義圖片加載
Album內(nèi)部有一個(gè)SimpleGlideAlbumImageLoader,使用的是 Glide 3.x 版本,如果想使用這個(gè),必須依賴 Glide 3.x
目前主流的圖片加載器,sample都有示例
需要注意的是如果使用
Fresco,ImageLoader的frescoView(context,type)必須返回正確的View,例如 fresco 的 預(yù)覽時(shí) 雙擊縮放功能,需要額外依賴完成, type == 1 時(shí) 是預(yù)覽頁(yè)的圖片加載
Fresco
Album
.getInstance()
.setConfig(new AlbumConfig().setFrescoImageLoader(true) // 通知 Album 圖片加載框架使用的是 Fresco
.start(this);
Glide 4.x
Picasso
這里需要說(shuō)明的是,如果直接用 file 加載圖片, Picasso 在某些手機(jī)上會(huì)出現(xiàn) ExifInterface_JNI: Raw image not detected
所以用Uri去加載圖片
universalimageloader
SimpleImageLoaderAlbumImageLoader
測(cè)試過(guò)的機(jī)型
- onePlus3T 7.1.1
- huawei ale-cl00 4.4.4
- meizu mx5 5.1
- galaxy S8+ 7.0
- hanzhong 5.1
- xiaomi note 6.0.1
- redmi note4 6.0
- oppo R7c 4.4.4
- Lenovo K30-T 4.4.4
混淆
-dontwarn com.album.**
-keep class com.album.** { *;}
-keep interface com.album.** { *;}
// ucrop
-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
感謝
關(guān)于MediaScannerConnection的內(nèi)存泄露問(wèn)題
協(xié)議
Mozilla Public License 2.0