這是仿微信的圖片選擇器。
簡介
1匿情、來源:相機(jī)兰迫,本地圖片媒體庫中jpg和png類型的圖片
2、功能:多選炬称、單選汁果、拍照、預(yù)覽玲躯、裁剪据德、大圖鲸伴、支持7.0
3、TODO:
- 增加緩存清除等工具類功能
- 增加多選時圖片編輯功能
- 增加視頻選擇功能
Demo下載
use_sample.png
使用方式
引用
1晋控、在根目錄的build.gradle中加入如下配置
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
maven { url "https://dl.bintray.com/thelasterstar/maven/" }
}
}
2汞窗、在要是用的module中增加如下引用
dependencies {
...
compile 'com.github.arvinljw:PictureSelector:v2.0.1'
}
注:該庫引用的第三方代碼
- v7
- recyclerview
- annotations
- exifinterface
- glide
前四個都是com.android.support下邊的,版本是26.1.0赡译,glide使用版本4.4.0
若是引用的包重復(fù)可使用類似這樣使用
dependencies {
...
compile ('com.github.arvinljw:PictureSelector:v2.0.1'){
exclude group: 'com.android.support'
}
}
3仲吏、在AndroidManifest文件中添加權(quán)限以及必須配置
權(quán)限
<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"/>
配置
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="net.arvin.pictureselectordemo.takephoto.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/ps_file_paths"/>
</provider>
<activity
android:name="net.arvin.selector.uis.SelectorActivity"
android:screenOrientation="portrait"
android:theme="@style/TransparentTheme"/>
使用
本庫依然采用外觀模式提供了一個SelectorHelper,里邊有一系列的靜態(tài)方法去啟動選擇器蝌焚,只是傳遞參數(shù)不同而已裹唆。
但是到最后都調(diào)用一個方法,所以只需要告訴大家這一個方法及其參數(shù)就能夠比較好的使用了只洒。
/**
* 去選擇圖片或視頻
*
* @param type 可選值{@link #VALUE_TYPE_PICTURE}{@link #VALUE_TYPE_VIDEO}{@link #VALUE_TYPE_PICTURE_VIDEO}
* @param singleSelection 是否單選
* @param canCrop 是否裁剪
* @param maxCount 最大數(shù)量
* @param withCamera 是否帶相機(jī)
* @param selectedPictures 已選中圖片
* @param selectedVideos 已選中視頻
*/
private static void select(Activity activity, int type, boolean singleSelection, boolean canCrop, int maxCount, boolean withCamera,
ArrayList<String> selectedPictures, ArrayList<String> selectedVideos, int requestCode) {
Intent intent = new Intent(activity, SelectorActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(KEY_TYPE_SELECT, type);
bundle.putBoolean(KEY_SINGLE_SELECTION, singleSelection);
bundle.putBoolean(KEY_CAN_CROP, canCrop);
bundle.putInt(KEY_MAX_COUNT, maxCount);
bundle.putBoolean(KEY_WITH_CAMERA, withCamera);
if (selectedPictures != VALUE_SELECTED_PICTURES_NULL) {
bundle.putStringArrayList(KEY_SELECTED_PICTURES, selectedPictures);
}
if (selectedVideos != VALUE_SELECTED_VIDEOS_NULL) {
bundle.putStringArrayList(KEY_SELECTED_VIDEOS, selectedVideos);
}
bundle.putString(KEY_AUTHORITIES, PSUtil.getAuthorities(activity));
intent.putExtras(bundle);
activity.startActivityForResult(intent, requestCode);
}
其實方法貼出來许帐,也有注釋就不用多說大家都明白了。這里暫時還只支持選擇圖片毕谴。
* 拍照成畦,是否裁剪
*/
public static void takePhoto(Activity activity, boolean canCrop, int requestCode) {
select(activity, VALUE_TYPE_CAMERA, VALUE_SINGLE_SELECTION_TRUE, canCrop, VALUE_COUNT_SINGLE,
VALUE_WITH_CAMERA_TRUE, VALUE_SELECTED_PICTURES_NULL, VALUE_SELECTED_PICTURES_NULL, requestCode);
}
/**
* 單選圖片,不裁剪涝开,帶相機(jī)
*/
public static void selectPicture(Activity activity, int requestCode) {
selectPicture(activity, VALUE_CAN_CROP_FALSE, VALUE_WITH_CAMERA_TRUE, requestCode);
}
/**
* 單選圖片
*
* @param canCrop 選擇是否裁剪
* @param withCamera 選擇是否帶相機(jī)
*/
public static void selectPicture(Activity activity, boolean canCrop, boolean withCamera, int requestCode) {
select(activity, VALUE_TYPE_PICTURE, VALUE_SINGLE_SELECTION_TRUE, canCrop, VALUE_COUNT_SINGLE, withCamera,
VALUE_SELECTED_PICTURES_NULL, VALUE_SELECTED_VIDEOS_NULL, requestCode);
}
/**
* 多選圖片循帐,帶相機(jī)
*
* @param maxCount 多選的最大數(shù)量
*/
public static void selectPictures(Activity activity, int maxCount, int requestCode) {
selectPictures(activity, maxCount, VALUE_WITH_CAMERA_TRUE, VALUE_SELECTED_PICTURES_NULL, requestCode);
}
/**
* 多選圖片
*
* @param maxCount 多選的最大數(shù)量
* @param withCamera 選擇是否帶相機(jī)
* @param selectedPictures 已選中的圖片
*/
public static void selectPictures(Activity activity, int maxCount, boolean withCamera, ArrayList<String> selectedPictures, int requestCode) {
select(activity, VALUE_TYPE_PICTURE, VALUE_SINGLE_SELECTION_FALSE, VALUE_CAN_CROP_FALSE, maxCount, withCamera,
selectedPictures, VALUE_SELECTED_VIDEOS_NULL, requestCode);
}
也就是說這四個靜態(tài)方法是可以使用的,多選時暫時不支持裁剪舀武,之后會想微信一樣增加編輯功能拄养。
最后在onActivityResult中可以獲得選中的圖片,當(dāng)然順序是選擇圖片時的順序银舱。
ArrayList<String> backPics = data.getStringArrayListExtra(ConstantData.KEY_BACK_PICTURES);
這樣就獲取到選中的圖片了瘪匿,不管單選多選都這樣,只是單選就只有一張寻馏。
當(dāng)然6.0以上的訪問相機(jī)和本地文件的權(quán)限需要自己去實現(xiàn)棋弥,demo中也提供了一種方式,僅供參考操软。
若是有什么問題嘁锯,希望不吝賜教共同進(jìn)步~
License
Copyright 2016 arvinljw
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.