這是仿微信的圖片選擇器。
簡介
1、來源:相機,本地圖片媒體庫中jpg和png類型的圖片
2、功能:多選、單選、拍照、預覽、裁剪、大圖、支持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
若是引用的包重復可使用類似這樣使用
dependencies {
...
compile ('com.github.arvinljw:PictureSelector:v2.0.1'){
exclude group: 'com.android.support'
}
}
3、在AndroidManifest文件中添加權限以及必須配置
權限
<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,里邊有一系列的靜態方法去啟動選擇器,只是傳遞參數不同而已。
但是到最后都調用一個方法,所以只需要告訴大家這一個方法及其參數就能夠比較好的使用了。
/**
* 去選擇圖片或視頻
*
* @param type 可選值{@link #VALUE_TYPE_PICTURE}{@link #VALUE_TYPE_VIDEO}{@link #VALUE_TYPE_PICTURE_VIDEO}
* @param singleSelection 是否單選
* @param canCrop 是否裁剪
* @param maxCount 最大數量
* @param withCamera 是否帶相機
* @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);
}
/**
* 單選圖片,不裁剪,帶相機
*/
public static void selectPicture(Activity activity, int requestCode) {
selectPicture(activity, VALUE_CAN_CROP_FALSE, VALUE_WITH_CAMERA_TRUE, requestCode);
}
/**
* 單選圖片
*
* @param canCrop 選擇是否裁剪
* @param withCamera 選擇是否帶相機
*/
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);
}
/**
* 多選圖片,帶相機
*
* @param maxCount 多選的最大數量
*/
public static void selectPictures(Activity activity, int maxCount, int requestCode) {
selectPictures(activity, maxCount, VALUE_WITH_CAMERA_TRUE, VALUE_SELECTED_PICTURES_NULL, requestCode);
}
/**
* 多選圖片
*
* @param maxCount 多選的最大數量
* @param withCamera 選擇是否帶相機
* @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);
}
也就是說這四個靜態方法是可以使用的,多選時暫時不支持裁剪,之后會想微信一樣增加編輯功能。
最后在onActivityResult中可以獲得選中的圖片,當然順序是選擇圖片時的順序。
ArrayList<String> backPics = data.getStringArrayListExtra(ConstantData.KEY_BACK_PICTURES);
這樣就獲取到選中的圖片了,不管單選多選都這樣,只是單選就只有一張。
當然6.0以上的訪問相機和本地文件的權限需要自己去實現,demo中也提供了一種方式,僅供參考。
若是有什么問題,希望不吝賜教共同進步~
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.