Album
android album
Chinese : wiki
Screenshot
multiple, radio, preview, crop, sample ui
加載失敗請移步至 Github 查看
圖片選擇器
選擇圖片爆存,裁剪华临,拍照
簡單使用
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添加
// 如果項目中 依賴了 design 高版本包菇怀,那么可以不用單獨依賴 RecyclerView
// 因為 高版本的 design 已經(jīng)包含了 RecyclerView
compile "com.android.support:design:$designVersion"
or
compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
// 這兩個必須依賴授艰,如果想使用 Glide 4.x 昧诱,請自行實現(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
//是否隱藏相機拥刻,默認不隱藏
boolean hideCamera = false;
//是否單選 抹竹,默認多選
boolean isRadio = false;
//單選點擊是否開啟裁剪线罕,默認開啟,不開啟直接返回當前點擊的圖片路徑
boolean isCrop = true;
// 拍照之后是否直接裁剪窃判,默認不裁剪
boolean cameraCrop = false;
//預覽頁多選數(shù)據(jù)變動钞楼,使用者點擊 Toolbar 的 返回鍵 返回時 是否刷新多選數(shù)據(jù),默認不刷新
boolean previewFinishRefresh = false;
//預覽頁多選數(shù)據(jù)變動袄琳,使用者點擊 Back 的 返回鍵 返回時 是否刷新多選數(shù)據(jù)询件,默認不刷新
boolean previewBackRefresh = false;
//權限被拒絕時是否直接 finish 掉 圖片選擇庫 的 Activity燃乍, 默認不 finish
boolean isPermissionsDeniedFinish = false;
//是否是使用 Fresco 加載圖片,當使用 Fresco 時 必須設置為 true,返回的 ImageView 就是 Fresco 的 SimpleDraweeView
boolean isFrescoImageLoader = false;
//多選最多可選擇幾個
int multipleMaxCount = 9;
//照相路徑
String cameraPath = null;
//裁剪路徑
String uCropPath = null;
//分頁加載時一次加載多少張圖片
int count = 200;
// 圖片頁 Activity 的 Toolbar 一些設置
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;
// 圖片頁 Activity 底部的 View 一些設置
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;
// 圖片頁 Activity 文件夾目錄的 一些設置
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;
//預覽頁 一些設置
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;
回調
Album 已經(jīng)放置了一個 SimpleAlbumListener
public class SimpleAlbumListener implements AlbumListener {
/**
* 點擊圖片頁Toolbar finish
*/
@Override
public void onAlbumActivityFinish() {
}
/**
* 權限被拒絕
* type:
* <p>
* *** AlbumConstant.TYPE_ALBUM : 讀取權限被拒
* *** AlbumConstant.TYPE_CAMERA : 相機權限被拒
*/
@Override
public void onAlbumPermissionsDenied(int type) {
}
/**
* 點擊 預覽 或者 選擇 按鈕時 Fragment 為 null, 應該不會被觸發(fā)..
*/
@Override
public void onAlbumFragmentNull() {
}
/**
* 預覽時雳殊,滑動 ViewPager 時 會檢測圖片是否還存在橘沥,不存在會觸發(fā)
* 觸發(fā)條件: Home 放在后臺,在其他軟件中刪掉了圖片
*/
@Override
public void onAlbumPreviewFileNull() {
}
/**
* 圖片 文件夾 目錄 為空夯秃,就是沒有圖片..
*/
@Override
public void onAlbumFinderNull() {
}
/**
* 點擊預覽時沒有選擇圖片
*/
@Override
public void onAlbumBottomPreviewNull() {
}
/**
* 點擊選擇時 沒有選擇圖片
*/
@Override
public void onAlbumBottomSelectNull() {
}
/**
* 點擊 ITEM 時座咆,會判斷當前圖片是否存在
* 觸發(fā)條件: Home 放在后臺,在其他軟件中刪掉了圖片
*/
@Override
public void onAlbumFragmentFileNull() {
}
/**
* 預覽界面選擇確定仓洼,但是沒有選中的圖片
*/
@Override
public void onAlbumPreviewSelectNull() {
}
/**
* 多選點擊 checkbox 時介陶,會判斷當前圖片是否存在
* 觸發(fā)條件: Home 放在后臺,在其他軟件中刪掉了圖片
* <p>
* 此時可選擇是否強制刷新圖庫信息
*/
@Override
public void onAlbumCheckBoxFileNull() {
}
/**
* 裁剪被取消
*/
@Override
public void onAlbumFragmentCropCanceled() {
}
/**
* 拍照被取消
*/
@Override
public void onAlbumFragmentCameraCanceled() {
}
/**
* 裁剪出現(xiàn)異常
*/
@Override
public void onAlbumFragmentUCropError(@Nullable Throwable data) {
}
/**
* 返回的選擇圖片信息
* <p>
* albumModel.getPath(); 為當前圖片地址
* <p>
* 建議 FileUtils.isFile 判斷一下圖片是否還存在
*/
@Override
public void onAlbumResources(@NonNull List<AlbumModel> list) {
}
/**
* 裁剪之后會返回的圖片F(xiàn)ile
*/
@Override
public void onAlbumUCropResources(@Nullable File scannerFile) {
}
/**
* 多選最多了
*/
@Override
public void onAlbumMaxCount() {
}
/**
* 點擊 back 返回到app
*/
@Override
public void onAlbumActivityBackPressed() {
}
/**
* 打開相機失敗色建,有可能是當前系統(tǒng)沒有相機
*/
@Override
public void onAlbumOpenCameraError() {
}
// 本機沒有圖片
@Override
public void onAlbumEmpty() {
}
}
自定義圖片加載
Album
內部有一個SimpleGlideAlbumImageLoader,使用的是 Glide
3.x 版本哺呜,如果想使用這個,必須依賴 Glide
3.x
目前主流的圖片加載器箕戳,sample
都有示例
需要注意的是如果使用
Fresco
某残,ImageLoader
的frescoView(context,type)
必須返回正確的View
,例如 fresco 的 預覽時 雙擊縮放功能,需要額外依賴完成, type == 1 時 是預覽頁的圖片加載
Fresco
Album
.getInstance()
.setConfig(new AlbumConfig().setFrescoImageLoader(true) // 通知 Album 圖片加載框架使用的是 Fresco
.start(this);
Glide 4.x
Picasso
這里需要說明的是陵吸,如果直接用 file
加載圖片玻墅, Picasso
在某些手機上會出現(xiàn) ExifInterface_JNI: Raw image not detected
所以用Uri
去加載圖片
universalimageloader
SimpleImageLoaderAlbumImageLoader
測試過的機型
- 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** { *; }
感謝
關于MediaScannerConnection
的內存泄露問題
協(xié)議
Mozilla Public License 2.0