PictureSelector

這是仿微信的圖片選擇器。

項(xiàng)目地址:PictureSelector

簡(jiǎn)介

1、來(lái)源:相機(jī),本地圖片媒體庫(kù)中jpg和png類型的圖片

2、功能:多選、單選、拍照、預(yù)覽、裁剪、大圖、支持7.0

3、TODO:

  • 增加緩存清除等工具類功能
  • 增加多選時(shí)圖片編輯功能
  • 增加視頻選擇功能

Demo下載

PicSelector下載地址

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'
}

注:該庫(kù)引用的第三方代碼

  • v7
  • recyclerview
  • annotations
  • exifinterface
  • glide

前四個(gè)都是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"/>

使用

本庫(kù)依然采用外觀模式提供了一個(gè)SelectorHelper,里邊有一系列的靜態(tài)方法去啟動(dòng)選擇器,只是傳遞參數(shù)不同而已。

但是到最后都調(diào)用一個(gè)方法,所以只需要告訴大家這一個(gè)方法及其參數(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);
}

其實(shí)方法貼出來(lái),也有注釋就不用多說(shuō)大家都明白了。這里暫時(shí)還只支持選擇圖片。

 * 拍照,是否裁剪
 */
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);
}

也就是說(shuō)這四個(gè)靜態(tài)方法是可以使用的,多選時(shí)暫時(shí)不支持裁剪,之后會(huì)想微信一樣增加編輯功能。

最后在onActivityResult中可以獲得選中的圖片,當(dāng)然順序是選擇圖片時(shí)的順序。

ArrayList<String> backPics = data.getStringArrayListExtra(ConstantData.KEY_BACK_PICTURES);

這樣就獲取到選中的圖片了,不管單選多選都這樣,只是單選就只有一張。

當(dāng)然6.0以上的訪問(wèn)相機(jī)和本地文件的權(quán)限需要自己去實(shí)現(xiàn),demo中也提供了一種方式,僅供參考。

若是有什么問(wèn)題,希望不吝賜教共同進(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.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,030評(píng)論 4 61
  • 對(duì)稱加密 對(duì)稱密鑰是雙方使用相同的密鑰 。對(duì)稱加密的要求(1)需要強(qiáng)大的加密算法。算法至少應(yīng)該滿足:即使分析人員知...
    HeartGo閱讀 712評(píng)論 0 5
  • 春天,我如溫潤(rùn)的風(fēng),悄然從你的枕邊離去, 你開(kāi)始慌張,冷靜,尋覓, 旋轉(zhuǎn)的地球儀,容不下你破碎的心, 你走遍每一處...
    恒晰閱讀 363評(píng)論 1 5
  • 最近圍繞希臘神話主題看了一些書(shū)籍,點(diǎn)滴所感: 希臘神話圍繞諸神、英雄、美女,講訴了各種傳奇與罪惡,透過(guò)荒誕的故事情...
    燈幾閱讀 215評(píng)論 0 0

友情鏈接更多精彩內(nèi)容