├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── nav_back.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── content_add_img_icon.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ ├── dot_blue.png
│ │ │ │ ├── dot_normal.png
│ │ │ │ └── dot_selector.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-xxhdpi
│ │ │ │ ├── content_img.png
│ │ │ │ ├── image_for_check.png
│ │ │ │ ├── image_for_nomal.png
│ │ │ │ └── image_check_number.png
│ │ │ ├── xml
│ │ │ │ └── provider_paths.xml
│ │ │ ├── drawable
│ │ │ │ ├── actionsheet_single_normal_shape.xml
│ │ │ │ ├── actionsheet_single_pressed_shape.xml
│ │ │ │ ├── shape_blue_solid.xml
│ │ │ │ ├── actionsheet_bottom_normal_shape.xml
│ │ │ │ ├── actionsheet_top_normal_shape.xml
│ │ │ │ ├── actionsheet_bottom_pressed_shape.xml
│ │ │ │ ├── actionsheet_top_pressed_shape.xml
│ │ │ │ ├── dot_selector.xml
│ │ │ │ ├── shape_pick_image_number.xml
│ │ │ │ ├── actionsheet_top_selector.xml
│ │ │ │ ├── actionsheet_bottom_selector.xml
│ │ │ │ ├── actionsheet_single_selector.xml
│ │ │ │ └── imagepicker_checkbox_selector.xml
│ │ │ ├── anim
│ │ │ │ ├── actionsheet_dialog_in.xml
│ │ │ │ ├── actionsheet_dialog_out.xml
│ │ │ │ ├── push_left_in.xml
│ │ │ │ ├── push_left_out.xml
│ │ │ │ ├── push_right_in.xml
│ │ │ │ └── push_right_out.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ ├── view_photo_edit.xml
│ │ │ │ ├── list_item_image.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── item_media_imagepicker.xml
│ │ │ │ ├── dialog_photo_bottom.xml
│ │ │ │ ├── activity_preview_photo.xml
│ │ │ │ └── activity_image_picker.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── alex
│ │ │ │ └── photopicker
│ │ │ │ ├── utils
│ │ │ │ └── ProviderUtil.java
│ │ │ │ ├── constants
│ │ │ │ └── Constant.java
│ │ │ │ ├── bens
│ │ │ │ └── MediaPhoto.java
│ │ │ │ ├── activity
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── PhotoPreviewActivity.java
│ │ │ │ └── ImagePickerActivity.java
│ │ │ │ ├── widget
│ │ │ │ ├── PhotoEditView.java
│ │ │ │ └── PhotoBottomDialog.java
│ │ │ │ └── adapter
│ │ │ │ ├── ImagePickerAdapter.java
│ │ │ │ └── PhotoEditAdapter.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── alex
│ │ │ └── photopicker
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── alex
│ │ └── photopicker
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── 504568939774866246.jpg
├── 603781355456128155.jpg
├── 645511239786389592.jpg
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/504568939774866246.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/504568939774866246.jpg
--------------------------------------------------------------------------------
/603781355456128155.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/603781355456128155.jpg
--------------------------------------------------------------------------------
/645511239786389592.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/645511239786389592.jpg
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PhotoPicker
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/nav_back.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-xhdpi/nav_back.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/dot_blue.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/drawable-xhdpi/dot_blue.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/dot_normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/drawable-xhdpi/dot_normal.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/content_img.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/drawable-xxhdpi/content_img.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/image_for_check.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/drawable-xxhdpi/image_for_check.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/image_for_nomal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/drawable-xxhdpi/image_for_nomal.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/image_check_number.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/drawable-xxhdpi/image_check_number.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/content_add_img_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuerui1993/PhotoPicker/HEAD/app/src/main/res/mipmap-xhdpi/content_add_img_icon.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 | /.idea
11 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/provider_paths.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_single_normal_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/actionsheet_dialog_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/actionsheet_dialog_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_single_pressed_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/shape_blue_solid.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_bottom_normal_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_top_normal_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 8dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_bottom_pressed_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_top_pressed_shape.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/push_left_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/push_left_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/push_right_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/push_right_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/dot_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/shape_pick_image_number.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/dot_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_top_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_bottom_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/actionsheet_single_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/imagepicker_checkbox_selector.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/utils/ProviderUtil.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.utils;
2 |
3 | import android.content.Context;
4 |
5 | /**
6 | * 用于解决provider冲突的util
7 | *
8 | * Author: nanchen
9 | * Email: liushilin520@foxmail.com
10 | * Date: 2017-03-22 18:55
11 | */
12 |
13 | public class ProviderUtil {
14 |
15 | public static String getFileProviderName(Context context){
16 | return context.getPackageName()+".fileprovider";
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/test/java/com/alex/photopicker/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/view_photo_edit.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #54B6B7
7 | #ffffff
8 | #55000000
9 | #88000000
10 | #444444
11 | #cc000000
12 | #E0E0E0
13 | #00ffffff
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/list_item_image.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/constants/Constant.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.constants;
2 |
3 | /*
4 | * @项目名: PhotoPicker
5 | * @包名: com.alex.photopicker
6 | * @文件名: Constant
7 | * @创建者: xuerui
8 | * @创建时间: 2017/4/9 0:47
9 | * @描述: TODO
10 | */
11 |
12 | public interface Constant {
13 | String PHOTO_LIST = "photo_list";
14 | String PHOTO_PREVIEW_LIST = "photo_preview_list";
15 | String PHOTO_CHECK_LIST = "photo_check_list";
16 | String PHOTO_POSITION = "photo_position";
17 | String TITLE_IS_GONE = "title_is_gone";
18 | String MAX_COUNT = "max_count";
19 | int ADD_PHOTO = 100;
20 | int PREVIEW_PHOTO = 200;
21 | }
22 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\android\Andorid-SDK\SDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/alex/photopicker/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.alex.photopicker", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/bens/MediaPhoto.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.bens;
2 |
3 | import java.io.Serializable;
4 |
5 | /*
6 | * @项目名: PhotoPicker
7 | * @包名: com.alex.photopicker.bens
8 | * @文件名: MediaPhoto
9 | * @创建者: xuerui
10 | * @创建时间: 2017/4/8 18:13
11 | * @描述: TODO
12 | */
13 | public class MediaPhoto implements Serializable{
14 | private static final String TAG = "MediaPhoto";
15 | private String url;
16 | private boolean isCheck;
17 |
18 | public MediaPhoto(String url, boolean isCheck) {
19 | this.url = url;
20 | this.isCheck = isCheck;
21 | }
22 |
23 | public void setUrl(String url) {
24 | this.url = url;
25 | }
26 |
27 | public void setCheck(boolean check) {
28 | isCheck = check;
29 | }
30 |
31 | public String getUrl() {
32 | return url;
33 | }
34 |
35 | public boolean isCheck() {
36 | return isCheck;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_media_imagepicker.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
13 |
18 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.alex.photopicker"
8 | minSdkVersion 15
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.github.bumptech.glide:glide:3.7.0'
28 | compile 'com.android.support:recyclerview-v7:24+'
29 | compile 'com.android.support:appcompat-v7:24+'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
22 |
24 |
29 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.activity;
2 | import android.content.Intent;
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.util.Log;
7 |
8 | import com.alex.photopicker.R;
9 | import com.alex.photopicker.widget.PhotoEditView;
10 |
11 |
12 | public class MainActivity extends AppCompatActivity {
13 |
14 | private static final String TAG = "MainActivity";
15 | private PhotoEditView mPhotoEditView;
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | setContentView(R.layout.activity_main);
21 | initView();
22 | }
23 |
24 | private void initView() {
25 | mPhotoEditView = (PhotoEditView) findViewById(R.id.photo_edit);
26 | mPhotoEditView.setMaxCount(9,4);
27 | }
28 |
29 | /**
30 | * 申请相机权限回调
31 | */
32 | @Override
33 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
34 | mPhotoEditView.onRequestPermissionResult(requestCode,permissions,grantResults);
35 | }
36 |
37 | @Override
38 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
39 | super.onActivityResult(requestCode, resultCode, data);
40 | Log.e(TAG, "onActivityResult: requestCode = " + requestCode + ",resultCode = " + resultCode);
41 | mPhotoEditView.onActivityResult(requestCode,resultCode,data);
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_photo_bottom.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
21 |
22 |
31 |
32 |
42 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PhotoPicker
2 | - 照片选择器,相机拍照选取图片
3 | 
4 | 
5 | 
6 |
7 | - 1.在项目build.gradle目录下添加
8 |
9 | allprojects {
10 | repositories {
11 | jcenter()
12 | maven {
13 | url 'https://jitpack.io'
14 | }
15 | }
16 | }
17 |
18 | - 2.在app的build.gradle目录下添加
19 |
20 | dependencies {
21 | compile 'com.github.xuerui1993:PhotoPicker:v1.0'
22 | }
23 |
24 | - 3.在页面布局中
25 |
26 |
30 |
31 |
32 | - 4.在activity中找到该控件并初始化
33 |
34 | mPhotoEditView = (PhotoEditView) findViewById(R.id.photo_edit);
35 | mPhotoEditView.setMaxCount(9,4); //必须写这一行,第一个参数为最多选择几张照片,第二个参数为控件有几列
36 |
37 | - 5.在activity中重写申请权限的方法和onActivityResult方法
38 |
39 | @Override
40 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[grantResults{
41 | mPhotoEditView.onRequestPermissionResult(requestCode,permissions,grantResults);
42 | }
43 |
44 | @Override
45 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
46 | super.onActivityResult(requestCode, resultCode, data);
47 | mPhotoEditView.onActivityResult(requestCode,resultCode,data);
48 | }
49 |
50 | - 6.获取照片路径
51 |
52 | List urlList = mPhotoEditView.getPhotoList();
53 |
54 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
13 |
17 |
18 |
35 |
39 |
40 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_preview_photo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
13 |
18 |
19 |
24 |
25 |
31 |
32 |
39 |
45 |
46 |
52 |
53 |
54 |
55 |
63 |
64 |
68 |
69 |
70 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_image_picker.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
19 |
25 |
26 |
27 |
34 |
35 |
45 |
46 |
47 |
52 |
53 |
54 |
61 |
62 |
66 |
67 |
68 |
78 |
90 |
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/widget/PhotoEditView.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.widget;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.pm.PackageManager;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.widget.GridLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.util.AttributeSet;
10 | import android.util.Log;
11 | import android.view.View;
12 | import android.widget.LinearLayout;
13 | import android.widget.Toast;
14 |
15 | import com.alex.photopicker.R;
16 | import com.alex.photopicker.adapter.PhotoEditAdapter;
17 | import com.alex.photopicker.constants.Constant;
18 |
19 | import java.util.List;
20 |
21 | /*
22 | * @项目名: PhotoPicker
23 | * @包名: com.alex.photopicker.widget
24 | * @文件名: PhotoEditView
25 | * @创建者: xuerui
26 | * @创建时间: 2017/7/19 2:22
27 | * @描述: TODO
28 | */
29 |
30 | public class PhotoEditView extends LinearLayout {
31 | private static final int REQUEST_CAMERA = 101;
32 | private static final int REQUEST_CODE_CAMERA = 100;
33 | private static final String TAG = "PhotoEditView";
34 | private Context mContext;
35 | private PhotoEditAdapter mPhotoEditAdapter;
36 | private RecyclerView mRecyclerView;
37 |
38 | public PhotoEditView(Context context) {
39 | this(context, null);
40 | }
41 |
42 | public PhotoEditView(Context context, @Nullable AttributeSet attrs) {
43 | super(context, attrs);
44 | mContext = context;
45 | View view = View.inflate(context, R.layout.view_photo_edit, this);
46 | initView(view);
47 | }
48 |
49 | private void initView(View view) {
50 | mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
51 |
52 | }
53 |
54 | public List getPhotoList() {
55 | return mPhotoEditAdapter.mUrlList;
56 | }
57 |
58 | public void setMaxCount(int maxCount, int cloumNumber) {
59 | mPhotoEditAdapter = new PhotoEditAdapter(mContext, maxCount);
60 | mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, cloumNumber));
61 | mRecyclerView.setHasFixedSize(true);
62 | mRecyclerView.setAdapter(mPhotoEditAdapter);
63 | }
64 |
65 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
66 | Log.e("view", "onActivityResult: requestCode = " + requestCode);
67 | if (requestCode == REQUEST_CAMERA) {
68 | Log.e("view", "onActivityResult: requestCode =101 ");
69 | mPhotoEditAdapter.setTakePhotoData();
70 | return;
71 | }
72 | switch (resultCode) {
73 | case Constant.ADD_PHOTO:
74 | mPhotoEditAdapter.setAddPhotoData(data);
75 | break;
76 | case Constant.PREVIEW_PHOTO:
77 | mPhotoEditAdapter.setPreviewPhotoData(data);
78 | break;
79 | }
80 | }
81 |
82 | public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
83 | switch (requestCode) {
84 | case REQUEST_CODE_CAMERA:
85 | Log.e(TAG, "onResult: permissions" + permissions.length + ",grant[0] = " + grantResults[0] + "[1] = " + grantResults[1] );
86 | if (permissions.length == 1) {
87 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
88 | mPhotoEditAdapter.takePhoto();
89 | } else {
90 | toast("权限被拒绝,无法使用相机拍照");
91 | }
92 | } else if (permissions.length == 2) {
93 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
94 | mPhotoEditAdapter.takePhoto();
95 | } else {
96 | toast("权限被拒绝,无法使用相机拍照");
97 | }
98 | }
99 |
100 | break;
101 | }
102 | }
103 |
104 | public void toast(String message) {
105 | Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/adapter/ImagePickerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.CheckBox;
8 | import android.widget.ImageView;
9 | import android.widget.RelativeLayout;
10 | import android.widget.Toast;
11 |
12 | import com.alex.photopicker.R;
13 | import com.alex.photopicker.bens.MediaPhoto;
14 | import com.bumptech.glide.Glide;
15 |
16 | import java.util.ArrayList;
17 | import java.util.List;
18 |
19 | /*
20 | * @项目名: ImagePickerAdapter
21 | * @包名: com.alex.photopicker
22 | * @文件名: ImagePickerAdapter
23 | * @创建者: xuerui
24 | * @创建时间: 2017/4/8 16:43
25 | * @描述: 图片选择adapter
26 | */
27 | public class ImagePickerAdapter extends RecyclerView.Adapter {
28 | private static final String TAG = "LoucaImagePickerAdapter";
29 | Context mContext;
30 | ArrayList mList;
31 | public int checkNumber = 0;
32 | public List mSelectList;
33 | int mMaxCount;
34 |
35 | public void setList(ArrayList list) {
36 | mList = list;
37 | notifyDataSetChanged();
38 | }
39 |
40 | public ImagePickerAdapter(Context context, ArrayList list,int maxCount) {
41 | mList = list;
42 | mContext = context;
43 | mSelectList = new ArrayList<>();
44 | mMaxCount = maxCount;
45 | }
46 |
47 | @Override
48 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
49 | View view = View.inflate(mContext,R.layout.item_media_imagepicker,null);
50 | ViewHolder viewHolder = new ViewHolder(view);
51 | return viewHolder;
52 | }
53 |
54 | @Override
55 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
56 | ViewHolder viewHolder = (ViewHolder) holder;
57 | viewHolder.setData(mList.get(position), position);
58 | }
59 |
60 | @Override
61 | public int getItemCount() {
62 | if (mList != null) {
63 | return mList.size();
64 | }
65 | return 0;
66 | }
67 |
68 | public interface ChangerNumberListener {
69 | void onNumberChanged();
70 | }
71 | ChangerNumberListener mListeners;
72 |
73 | public void setChangerNumberListener(ChangerNumberListener listener) {
74 | if (listener != null) {
75 | mListeners = listener;
76 | }
77 | }
78 |
79 | RecyClerItemClickListener mItemListeners;
80 | public interface RecyClerItemClickListener{
81 | void onRecyclerItemClick(ArrayList list,boolean titleIsGone,int position);
82 | }
83 |
84 | public void setOnRecyclerItemListener(RecyClerItemClickListener listener) {
85 | if (listener != null) {
86 | mItemListeners = listener;
87 | }
88 | }
89 |
90 | class ViewHolder extends RecyclerView.ViewHolder {
91 | ImageView mIvPhoto;
92 | CheckBox mCheckbox;
93 | int mPosition;
94 | RelativeLayout mRlCheck;
95 |
96 | public ViewHolder(View itemView) {
97 | super(itemView);
98 | mRlCheck = (RelativeLayout) itemView.findViewById(R.id.rl_check);
99 | mIvPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);
100 | mCheckbox = (CheckBox) itemView.findViewById(R.id.checkbox);
101 | itemView.setOnClickListener(new View.OnClickListener() {
102 | @Override
103 | public void onClick(View v) {
104 | if (mItemListeners!=null){
105 | mItemListeners.onRecyclerItemClick(mList,true,mPosition);
106 | }
107 | }
108 | });
109 | mRlCheck.setOnClickListener(new View.OnClickListener() {
110 | @Override
111 | public void onClick(View v) {
112 | boolean isCheck = mList.get(mPosition).isCheck();
113 | if (checkNumber >= mMaxCount && !isCheck) {
114 | toast("最多只能选择"+mMaxCount+"张图片");
115 | return;
116 | }
117 | mList.get(mPosition).setCheck(!isCheck);
118 | if (isCheck) {
119 | //减少了
120 | checkNumber--;
121 | mCheckbox.setChecked(false);
122 | mSelectList.remove(mPosition+"");
123 | } else {
124 | //增加了
125 | checkNumber++;
126 | mCheckbox.setChecked(true);
127 | mSelectList.add(mPosition+"");
128 | }
129 | if (mListeners != null) {
130 | mListeners.onNumberChanged();
131 | }
132 | }
133 | });
134 | }
135 |
136 | public void setData(MediaPhoto bean, int position) {
137 | mCheckbox.setButtonDrawable(R.drawable.imagepicker_checkbox_selector);
138 | mPosition = position;
139 | mCheckbox.setChecked(bean.isCheck());
140 | Glide.with(mContext).load(bean.getUrl()).placeholder(R.drawable.content_img).into(mIvPhoto);
141 | }
142 | }
143 |
144 | public void toast(String message) {
145 | Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/widget/PhotoBottomDialog.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.widget;
2 |
3 | import android.Manifest;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.pm.PackageManager;
8 | import android.support.annotation.NonNull;
9 | import android.support.annotation.StyleRes;
10 | import android.support.v4.app.ActivityCompat;
11 | import android.support.v7.app.AlertDialog;
12 | import android.util.Log;
13 | import android.view.Display;
14 | import android.view.Gravity;
15 | import android.view.View;
16 | import android.view.Window;
17 | import android.view.WindowManager;
18 | import android.widget.Button;
19 |
20 | import com.alex.photopicker.R;
21 | import com.alex.photopicker.activity.ImagePickerActivity;
22 | import com.alex.photopicker.constants.Constant;
23 |
24 | /*
25 | * @项目名: PhotoPicker
26 | * @包名: com.alex.photopicker.widget
27 | * @文件名: PhotoBottomDialog
28 | * @创建者: xuerui
29 | * @创建时间: 2017/7/19 1:09
30 | * @描述: TODO
31 | */
32 | public class PhotoBottomDialog extends AlertDialog.Builder implements View.OnClickListener {
33 | private static final int REQUEST_CODE_CAMERA = 100;
34 | private static final int REQUEST_CAMERA = 101;
35 | private static final String TAG = "PhotoBottomDialog";
36 | private int mSurplusCount;
37 | private Intent mIntent;
38 | private Activity mActivity;
39 | private Context mContext;
40 | private Button mBtnGallery;
41 | private Button mBtnTakePicture;
42 | private Button mBtnCancel;
43 | private AlertDialog mAlertDialog;
44 |
45 | public PhotoBottomDialog(@NonNull Context context, Activity activity, Intent intent, int surplusCount) {
46 | this(context, R.style.ActionSheetDialogStyle);
47 | mActivity = activity;
48 | mIntent = intent;
49 | mSurplusCount = surplusCount;
50 | }
51 |
52 | public PhotoBottomDialog(@NonNull Context context, @StyleRes int themeResId) {
53 | super(context, themeResId);
54 | mContext = context;
55 | View view = View.inflate(context, R.layout.dialog_photo_bottom, null);
56 | setView(view);
57 | mAlertDialog = create();
58 | Window window = mAlertDialog.getWindow();
59 | window.setGravity(Gravity.BOTTOM);
60 | WindowManager.LayoutParams lp = window.getAttributes();
61 | WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
62 | Display d = m.getDefaultDisplay();
63 | lp.width = (int) (d.getWidth() * 0.95);
64 | lp.y = 30;
65 | window.setAttributes(lp);
66 | initView(view);
67 | initListener();
68 | }
69 |
70 | public AlertDialog getAlertDialog() {
71 | return mAlertDialog;
72 | }
73 |
74 | private void initListener() {
75 | mBtnCancel.setOnClickListener(this);
76 | mBtnGallery.setOnClickListener(this);
77 | mBtnTakePicture.setOnClickListener(this);
78 | }
79 |
80 | private void initView(View view) {
81 | mBtnGallery = (Button) view.findViewById(R.id.btn_gallery);
82 | mBtnTakePicture = (Button) view.findViewById(R.id.btn_take_picture);
83 | mBtnCancel = (Button) view.findViewById(R.id.btn_cancel);
84 | }
85 |
86 | /**
87 | * 检查相机权限是否开启
88 | * @return true为开启, false为未开启
89 | */
90 | private boolean checkHasCameraPermission() {
91 | int result = ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA);
92 | int storage_result = ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
93 | Log.e(TAG, "checkHasCameraPermission: result = " + result +",storage_result = " +storage_result);
94 | return result == PackageManager.PERMISSION_GRANTED && storage_result == PackageManager.PERMISSION_GRANTED;
95 | }
96 |
97 | @Override
98 | public void onClick(View v) {
99 | switch (v.getId()) {
100 | case R.id.btn_cancel:
101 | mAlertDialog.dismiss();
102 | break;
103 | case R.id.btn_take_picture:
104 | mAlertDialog.dismiss();
105 | if (checkHasCameraPermission() && checkHasReadExternalStorage()) {
106 | mActivity.startActivityForResult(mIntent, REQUEST_CAMERA);
107 | } else {
108 | //申请相机权限
109 | ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_CAMERA);
110 | }
111 | break;
112 | case R.id.btn_gallery:
113 | mAlertDialog.dismiss();
114 | addPhoto();
115 | break;
116 | }
117 | }
118 |
119 | public void addPhoto() {
120 | Intent intent = new Intent(mContext, ImagePickerActivity.class);
121 | intent.putExtra(Constant.MAX_COUNT, mSurplusCount);
122 | mActivity.startActivityForResult(intent, Constant.ADD_PHOTO);
123 | }
124 |
125 | private boolean checkHasReadExternalStorage() {
126 | int result = ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
127 | return result == PackageManager.PERMISSION_GRANTED;
128 | }
129 |
130 | }
131 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/activity/PhotoPreviewActivity.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.activity;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.graphics.Color;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.support.v4.view.PagerAdapter;
9 | import android.support.v4.view.ViewPager;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.util.Log;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.ImageView;
15 | import android.widget.RelativeLayout;
16 | import android.widget.TextView;
17 | import android.widget.Toast;
18 |
19 | import com.alex.photopicker.constants.Constant;
20 | import com.alex.photopicker.R;
21 | import com.alex.photopicker.bens.MediaPhoto;
22 | import com.bumptech.glide.Glide;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | /*
28 | * @项目名: PhotoPicker
29 | * @包名: com.alex.photopicker.activity
30 | * @文件名: PhotoPreviewActivity
31 | * @创建者: dell
32 | * @创建时间: 2017/4/9 2:13
33 | * @描述: TODO
34 | */
35 | public class PhotoPreviewActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
36 | private static final String TAG = "PhotoPreviewActivity";
37 | private ArrayList mPreviewList;
38 | private RelativeLayout mRlBack;
39 | private RelativeLayout mRlTopBar;
40 | private RelativeLayout mRlCheck;
41 | private RelativeLayout mRlBottomBar;
42 | private TextView mTvComplete;
43 | private PhotoPreviewViewPagerAdpater mAdvertAdapter;
44 | private ViewPager mViewPager;
45 | private ImageView mIvCheck;
46 | private int mPosition;
47 | private int mCheckNumber = 0;
48 | private TextView mTvTitle;
49 | private int mMaxCount;
50 |
51 | @Override
52 | protected void onCreate(@Nullable Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | setContentView(R.layout.activity_preview_photo);
55 | initView();
56 | initData();
57 | initListener();
58 | }
59 |
60 | private void initView() {
61 | mRlBack = (RelativeLayout) findViewById(R.id.rl_back);
62 | mRlTopBar = (RelativeLayout) findViewById(R.id.rl_top_bar);
63 | mRlCheck = (RelativeLayout) findViewById(R.id.rl_check);
64 | mRlBottomBar = (RelativeLayout) findViewById(R.id.rl_bottom_bar);
65 | mTvComplete = (TextView) findViewById(R.id.tv_complete);
66 | mTvTitle = (TextView) findViewById(R.id.tv_title);
67 | mViewPager = (ViewPager) findViewById(R.id.top_view_pager);
68 | mIvCheck = (ImageView) findViewById(R.id.iv_check);
69 | }
70 |
71 | private void initData() {
72 | Intent intent = getIntent();
73 | if (intent != null) {
74 | mMaxCount = intent.getIntExtra(Constant.MAX_COUNT, 0);
75 | mPreviewList = (ArrayList) intent.getSerializableExtra(Constant.PHOTO_PREVIEW_LIST);
76 | mPosition = intent.getIntExtra(Constant.PHOTO_POSITION, 0);
77 | boolean titleIsGone = intent.getBooleanExtra(Constant.TITLE_IS_GONE, true);
78 | if (titleIsGone) {
79 | mTvTitle.setVisibility(View.GONE);
80 | } else {
81 | mTvTitle.setVisibility(View.VISIBLE);
82 | }
83 | if (mPreviewList == null || mPreviewList.size() == 0) return;
84 | List imageList = new ArrayList<>();
85 | for (int i = 0; i < mPreviewList.size(); i++) {
86 | ImageView imageView = new ImageView(this);
87 | imageList.add(imageView);
88 | if (mPreviewList.get(i).isCheck()) {
89 | mCheckNumber++;
90 | }
91 | }
92 | Log.e(TAG, "initData: " + mPreviewList.size());
93 | mAdvertAdapter = new PhotoPreviewViewPagerAdpater(this, mPreviewList, imageList);
94 | mViewPager.setAdapter(mAdvertAdapter);
95 | mViewPager.setCurrentItem(mPosition);
96 | }
97 | }
98 |
99 | private void initListener() {
100 | mRlBack.setOnClickListener(this);
101 | mRlCheck.setOnClickListener(this);
102 | mTvComplete.setOnClickListener(this);
103 | mViewPager.addOnPageChangeListener(this);
104 | }
105 |
106 | @Override
107 | public void onClick(View v) {
108 | switch (v.getId()) {
109 | case R.id.rl_back:
110 | finish();
111 | break;
112 | case R.id.rl_check:
113 | boolean isCheck = mPreviewList.get(mPosition).isCheck();
114 | if (isCheck) {
115 | mPreviewList.get(mPosition).setCheck(false);
116 | mIvCheck.setSelected(false);
117 | mCheckNumber--;
118 | } else {
119 | if (mCheckNumber >= mMaxCount && !isCheck) {
120 | toast("最多只能选择"+mMaxCount+"张图片");
121 | return;
122 | }
123 | mPreviewList.get(mPosition).setCheck(true);
124 | mIvCheck.setSelected(true);
125 | mCheckNumber++;
126 | }
127 | setTvCheckNumber();
128 | break;
129 | case R.id.tv_complete:
130 | ArrayList checkList = new ArrayList();
131 | for (int i = 0; i < mPreviewList.size(); i++) {
132 | if (mPreviewList.get(i).isCheck()) {
133 | checkList.add(mPreviewList.get(i));
134 | }
135 | }
136 | Intent intent = new Intent();
137 | intent.putExtra(Constant.PHOTO_CHECK_LIST, checkList);
138 | setResult(Constant.PREVIEW_PHOTO, intent);
139 | finish();
140 | break;
141 | }
142 | }
143 |
144 | private void setTvCheckNumber() {
145 | if (mCheckNumber == 0) {
146 | mTvComplete.setText("完成");
147 | } else {
148 | mTvComplete.setText("完成(" + mCheckNumber + ")");
149 | }
150 | }
151 |
152 | boolean isFirst = true;
153 |
154 | @Override
155 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
156 | if (isFirst) {
157 | mPosition = position;
158 | if (mPreviewList.get(mPosition).isCheck()) {
159 | mIvCheck.setSelected(true);
160 | }
161 | setTvCheckNumber();
162 | mTvTitle.setText(mPosition + 1 + "/" + mPreviewList.size());
163 | isFirst = false;
164 | }
165 | }
166 |
167 | @Override
168 | public void onPageSelected(int position) {
169 | Log.e(TAG, "onPageSelected: " + position);
170 | mPosition = position;
171 | Log.e(TAG, "onPageSelected: " + mPreviewList.get(position).isCheck());
172 | if (mPreviewList.get(position).isCheck()) {
173 | mIvCheck.setSelected(true);
174 | } else {
175 | mIvCheck.setSelected(false);
176 | }
177 | mTvTitle.setText(mPosition + 1 + "/" + mPreviewList.size());
178 | }
179 |
180 | @Override
181 | public void onPageScrollStateChanged(int state) {
182 |
183 | }
184 |
185 | public class PhotoPreviewViewPagerAdpater extends PagerAdapter {
186 | private static final String TAG = "PhotoPreviewViewPagerAdpater";
187 | Context mContext;
188 | List mList;
189 | List mImageList;
190 |
191 | public PhotoPreviewViewPagerAdpater(Context context, List list, List imageList) {
192 | mContext = context;
193 | mList = list;
194 | mImageList = imageList;
195 | }
196 |
197 | @Override
198 | public int getCount() {
199 | if (mList != null) {
200 | return mList.size();
201 | }
202 | return 0;
203 | }
204 |
205 | @Override
206 | public boolean isViewFromObject(View view, Object object) {
207 | return view == object;
208 | }
209 |
210 | @Override
211 | public Object instantiateItem(ViewGroup container, final int position) {
212 | ImageView imageView = mImageList.get(position);
213 | imageView.setBackgroundColor(Color.parseColor("#000000"));
214 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
215 | container.addView(imageView);
216 | String url = mList.get(position).getUrl();
217 | Glide.with(mContext).load(url).into(imageView);
218 | return imageView;
219 | }
220 |
221 | @Override
222 | public void destroyItem(ViewGroup container, int position, Object object) {
223 | container.removeView((View) object);
224 | }
225 | }
226 |
227 | public void toast(String message) {
228 | Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
229 | }
230 | }
231 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/activity/ImagePickerActivity.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.activity;
2 |
3 | import android.Manifest;
4 | import android.content.ContentResolver;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.database.Cursor;
8 | import android.os.Bundle;
9 | import android.os.Handler;
10 | import android.os.Message;
11 | import android.provider.MediaStore;
12 | import android.support.annotation.NonNull;
13 | import android.support.annotation.Nullable;
14 | import android.support.v4.app.ActivityCompat;
15 | import android.support.v7.app.AppCompatActivity;
16 | import android.support.v7.widget.GridLayoutManager;
17 | import android.support.v7.widget.RecyclerView;
18 | import android.view.View;
19 | import android.widget.RelativeLayout;
20 | import android.widget.TextView;
21 | import android.widget.Toast;
22 |
23 | import com.alex.photopicker.constants.Constant;
24 | import com.alex.photopicker.R;
25 | import com.alex.photopicker.adapter.ImagePickerAdapter;
26 | import com.alex.photopicker.bens.MediaPhoto;
27 |
28 | import java.util.ArrayList;
29 |
30 | public class ImagePickerActivity extends AppCompatActivity implements View.OnClickListener, ImagePickerAdapter.ChangerNumberListener, ImagePickerAdapter.RecyClerItemClickListener {
31 | private static final String TAG = "LoucaImagePickerActivity";
32 |
33 | private ArrayList mList;
34 |
35 | private ImagePickerAdapter mAdapter;
36 |
37 | private GridLayoutManager mGridLayoutManager;
38 | private String mTopicName;
39 | private TextView mTvComplete;
40 | private RelativeLayout mRlBack;
41 | private RecyclerView mRecyclerView;
42 | private static final int REQUEST_CODE_READ_EXTERNAL_STORAGE = 0;
43 | private Handler mHandler = new Handler() {
44 | @Override
45 | public void handleMessage(Message msg) {
46 | initRecyclerView();
47 | initListener();
48 | super.handleMessage(msg);
49 | }
50 | };
51 | private TextView mTvPreview;
52 | private TextView mTvCancel;
53 | private int mMaxCount;
54 |
55 | @Override
56 | protected void onCreate(@Nullable Bundle savedInstanceState) {
57 | super.onCreate(savedInstanceState);
58 | setContentView(R.layout.activity_image_picker);
59 | Intent intent = getIntent();
60 | if (intent!=null){
61 | mMaxCount = intent.getIntExtra(Constant.MAX_COUNT, 0);
62 | }
63 | initView();
64 | initData();
65 | }
66 |
67 | private void initView() {
68 | mTvComplete = (TextView) findViewById(R.id.tv_complete);
69 | mRlBack = (RelativeLayout) findViewById(R.id.rl_back);
70 | mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
71 | mTvPreview = (TextView) findViewById(R.id.tv_preview);
72 | mTvCancel = (TextView) findViewById(R.id.tv_cancel);
73 | }
74 |
75 | private void initListener() {
76 | mTvComplete.setOnClickListener(this);
77 | mRlBack.setOnClickListener(this);
78 | mTvCancel.setOnClickListener(this);
79 | mTvPreview.setOnClickListener(this);
80 | mAdapter.setOnRecyclerItemListener(this);
81 | }
82 |
83 | private void initData() {
84 | //检查是否有读写磁盘的权限
85 | if (checkHasReadExternalStorage()) {
86 | readSystemPhotoMedia();
87 | } else {
88 | applyPermission();//申请权限
89 | }
90 | }
91 |
92 | private void applyPermission() {
93 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_READ_EXTERNAL_STORAGE);
94 | }
95 |
96 | private boolean checkHasReadExternalStorage() {
97 | int result = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
98 | return result == PackageManager.PERMISSION_GRANTED;
99 | }
100 |
101 | @Override
102 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
103 | switch (requestCode) {
104 | case REQUEST_CODE_READ_EXTERNAL_STORAGE:
105 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
106 | readSystemPhotoMedia();
107 | } else {
108 | toast("权限被拒绝");
109 | }
110 | break;
111 | }
112 | }
113 |
114 | private void readSystemPhotoMedia() {
115 | new Thread() {
116 | @Override
117 | public void run() {
118 | mList = new ArrayList<>();
119 | ContentResolver resolver = getContentResolver();
120 | Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA}, null, null, MediaStore.Images.Media.DATE_MODIFIED);
121 | cursor.moveToPosition(-1);
122 | while (cursor.moveToNext()) {
123 | MediaPhoto photo = new MediaPhoto(cursor.getString(0), false);
124 | mList.add(photo);
125 | }
126 | Message msg = Message.obtain();
127 | mHandler.sendMessage(msg);
128 | }
129 | }.start();
130 | }
131 |
132 |
133 | private void initRecyclerView() {
134 | mRecyclerView.setHasFixedSize(true);
135 | mGridLayoutManager = new GridLayoutManager(this, 4);
136 | mRecyclerView.setLayoutManager(mGridLayoutManager);
137 | mAdapter = new ImagePickerAdapter(this, mList,mMaxCount);
138 | mRecyclerView.setAdapter(mAdapter);
139 | mRecyclerView.scrollToPosition(mList.size() - 1);
140 | mAdapter.setChangerNumberListener(this);
141 | }
142 |
143 | @Override
144 | public void onClick(View v) {
145 | switch (v.getId()) {
146 | case R.id.tv_complete:
147 | if (mAdapter.mSelectList==null||mAdapter.mSelectList.size()==0){
148 | toast("至少选择一张图片");
149 | return;
150 | }
151 | ArrayList checkList = getCheckPhotos();
152 | Intent intent = new Intent();
153 | intent.putExtra(Constant.PHOTO_LIST, checkList);
154 | setResult(Constant.ADD_PHOTO, intent);
155 | finish();
156 | overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
157 | break;
158 | case R.id.rl_back:
159 | finish();
160 | overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
161 | break;
162 | case R.id.tv_cancel:
163 | finish();
164 | overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
165 | break;
166 | case R.id.tv_preview:
167 | if (mAdapter.mSelectList==null||mAdapter.mSelectList.size()==0){
168 | toast("至少选择一张图片");
169 | return;
170 | }
171 | ArrayList checkList2 = getCheckPhotos();
172 | Intent previewIntent = new Intent(this, PhotoPreviewActivity.class);
173 | previewIntent.putExtra(Constant.PHOTO_PREVIEW_LIST, checkList2);
174 | previewIntent.putExtra(Constant.TITLE_IS_GONE,false);
175 | previewIntent.putExtra(Constant.MAX_COUNT,mMaxCount);
176 | startActivityForResult(previewIntent, Constant.PREVIEW_PHOTO);
177 | break;
178 | }
179 | }
180 |
181 | @NonNull
182 | private ArrayList getCheckPhotos() {
183 | ArrayList checkList = new ArrayList<>();
184 | for (String s : mAdapter.mSelectList) {
185 | checkList.add(mList.get(Integer.parseInt(s)));
186 | }
187 | return checkList;
188 | }
189 |
190 | @Override
191 | public void onNumberChanged() {
192 | if (mAdapter.checkNumber == 0) {
193 | mTvComplete.setText("完成");
194 | } else {
195 | mTvComplete.setText("完成("+mAdapter.checkNumber+")");
196 | }
197 | }
198 |
199 | public void toast(String message) {
200 | Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
201 | }
202 |
203 | @Override
204 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
205 | super.onActivityResult(requestCode, resultCode, data);
206 | if (resultCode == Constant.PREVIEW_PHOTO){
207 | ArrayList checkList = (ArrayList) data.getSerializableExtra(Constant.PHOTO_CHECK_LIST);
208 | Intent intent = new Intent();
209 | intent.putExtra(Constant.PHOTO_LIST, checkList);
210 | setResult(Constant.ADD_PHOTO, intent);
211 | finish();
212 | }
213 | }
214 |
215 | @Override
216 | public void onRecyclerItemClick(ArrayList list, boolean titleIsGone, int position) {
217 | Intent previewIntent = new Intent(this,PhotoPreviewActivity.class);
218 | previewIntent.putExtra(Constant.PHOTO_PREVIEW_LIST,list);
219 | previewIntent.putExtra(Constant.TITLE_IS_GONE, titleIsGone);
220 | previewIntent.putExtra(Constant.PHOTO_POSITION,position);
221 | previewIntent.putExtra(Constant.MAX_COUNT,mMaxCount);
222 | startActivityForResult(previewIntent,Constant.PREVIEW_PHOTO);
223 | }
224 | }
225 |
--------------------------------------------------------------------------------
/app/src/main/java/com/alex/photopicker/adapter/PhotoEditAdapter.java:
--------------------------------------------------------------------------------
1 | package com.alex.photopicker.adapter;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.content.pm.ResolveInfo;
8 | import android.net.Uri;
9 | import android.os.Build;
10 | import android.os.Environment;
11 | import android.os.SystemClock;
12 | import android.provider.MediaStore;
13 | import android.support.annotation.NonNull;
14 | import android.support.v4.content.FileProvider;
15 | import android.support.v7.widget.RecyclerView;
16 | import android.text.TextUtils;
17 | import android.util.Log;
18 | import android.view.View;
19 | import android.view.ViewGroup;
20 | import android.widget.ImageView;
21 |
22 | import com.alex.photopicker.R;
23 | import com.alex.photopicker.activity.PhotoPreviewActivity;
24 | import com.alex.photopicker.bens.MediaPhoto;
25 | import com.alex.photopicker.constants.Constant;
26 | import com.alex.photopicker.utils.ProviderUtil;
27 | import com.alex.photopicker.widget.PhotoBottomDialog;
28 | import com.bumptech.glide.Glide;
29 |
30 | import java.io.File;
31 | import java.util.ArrayList;
32 | import java.util.List;
33 |
34 | /*
35 | * @项目名: PhotoPicker
36 | * @包名: com.alex.photopicker.adapter
37 | * @文件名: PhotoEditAdapter
38 | * @创建者: xuerui
39 | * @创建时间: 2017/7/18 2:22
40 | * @描述: TODO
41 | */
42 |
43 | public class PhotoEditAdapter extends RecyclerView.Adapter {
44 |
45 | private static final String TAG = "PhotoEditAdapter";
46 | private static final int REQUEST_CAMERA = 101;
47 | private ArrayList mPhotoList;
48 | private Context mContext;
49 | public List mUrlList = new ArrayList<>();
50 | public File mFile;
51 | private Activity mActivity;
52 | private int mMaxImageCount;
53 | private int mSurplusCount;
54 |
55 | public void setList(List list) {
56 | mUrlList = list;
57 | notifyDataSetChanged();
58 | }
59 |
60 | public PhotoEditAdapter(Context context, int maxImgCount) {
61 | mMaxImageCount = maxImgCount;
62 | mSurplusCount = maxImgCount;
63 | mContext = context;
64 | mUrlList = new ArrayList<>();
65 | mActivity = (Activity) context;
66 | mPhotoList = new ArrayList<>();
67 | }
68 |
69 | @Override
70 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
71 | return new SelectedPicViewHolder(View.inflate(mContext, R.layout.list_item_image, null));
72 | }
73 |
74 | @Override
75 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
76 | SelectedPicViewHolder viewHolder = (SelectedPicViewHolder) holder;
77 | viewHolder.setData(position);
78 | }
79 |
80 |
81 | @Override
82 | public int getItemCount() {
83 | if (getIsAdded()) {
84 | return mUrlList.size() + 1;
85 | } else {
86 | return mUrlList.size();
87 | }
88 | }
89 |
90 | private boolean getIsAdded() {
91 | if (mUrlList.size() < mMaxImageCount) {
92 | return true;
93 | }
94 | return false;
95 | }
96 |
97 | public void takePhoto() {
98 | Intent intent = getTakePhotoIntent();
99 | mActivity.startActivityForResult(intent, REQUEST_CAMERA);
100 | }
101 |
102 | public void setTakePhotoData() {
103 | //发送一个广播,刷新相册
104 | Intent intent = new Intent();
105 | intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
106 | intent.setData(Uri.fromFile(mFile));
107 | mActivity.sendBroadcast(intent);
108 | Log.e(TAG, "setTakePhotoData: 11111 ");
109 | Uri uri;
110 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
111 | if (mFile.exists() && mFile.getAbsolutePath().length() > 0) {
112 | mUrlList.add(mFile.getAbsolutePath());
113 | setList(mUrlList);
114 | mPhotoList.add(new MediaPhoto(mFile.getAbsolutePath(), true));
115 | Log.e(TAG, "setTakePhotoData:,mFile.length() = " +mFile.getAbsolutePath().length());
116 | }
117 |
118 | } else {
119 | uri = Uri.fromFile(mFile);
120 | if (mFile.exists() && mFile.getAbsolutePath().length() > 0) {
121 | //添加拍照获取的图片
122 | mUrlList.add(mFile.getAbsolutePath());
123 | setList(mUrlList);
124 | mPhotoList.add(new MediaPhoto(mFile.getAbsolutePath(), true));
125 | } else {
126 | Log.e(TAG, "setTakePhotoData: file为空了");
127 | }
128 | }
129 | mSurplusCount = mMaxImageCount - mUrlList.size();
130 | return;
131 | }
132 |
133 | @NonNull
134 | private Intent getTakePhotoIntent() {//启动手机中的 camera app , 帮组去实现 拍照 , 那么需要发意图
135 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
136 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
137 | //写到 sd卡上 . 需要申请权限.
138 | mFile = new File(Environment.getExternalStorageDirectory(), SystemClock.elapsedRealtime() + ".jpg");
139 | Log.e(TAG, "getTakePhotoIntent: file = " + mFile);
140 | Uri uri;
141 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
142 | uri = Uri.fromFile(mFile);
143 | } else {
144 | /**
145 | * 7.0 调用系统相机拍照不再允许使用Uri方式,应该替换为FileProvider
146 | * 并且这样可以解决MIUI系统上拍照返回size为0的情况
147 | */
148 | Log.e(TAG, "provider: " + ProviderUtil.getFileProviderName(mContext) );
149 | uri = FileProvider.getUriForFile(mContext, ProviderUtil.getFileProviderName(mContext), mFile);
150 | //加入uri权限 要不三星手机不能拍照
151 | List resInfoList = mContext.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
152 | for (ResolveInfo resolveInfo : resInfoList) {
153 | String packageName = resolveInfo.activityInfo.packageName;
154 | mContext.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
155 | }
156 | }
157 | intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
158 | return intent;
159 | }
160 |
161 | public void setAddPhotoData(Intent data) {
162 | if (data != null) {
163 | //添加从相册选择的图片
164 | ArrayList list = (ArrayList) data.getSerializableExtra(Constant.PHOTO_LIST);
165 | for (int i = 0; i < list.size(); i++) {
166 | mUrlList.add(list.get(i).getUrl());
167 | mPhotoList.add(list.get(i));
168 | }
169 | setList(mUrlList);
170 | mSurplusCount = mMaxImageCount - mUrlList.size();
171 | Log.e(TAG, "onActivityResult: maxCount = " + mMaxImageCount);
172 | }
173 | }
174 |
175 | public void setPreviewPhotoData(Intent data) {
176 | if (data != null) {
177 | //修改经过预览后的图片
178 | mPhotoList = (ArrayList) data.getSerializableExtra(Constant.PHOTO_CHECK_LIST);
179 | mUrlList.clear();
180 | for (int i = 0; i < mPhotoList.size(); i++) {
181 | if (mPhotoList.get(i).isCheck()) {
182 | mUrlList.add(mPhotoList.get(i).getUrl());
183 | }
184 | }
185 | setList(mUrlList);
186 | mSurplusCount = mMaxImageCount - mUrlList.size();
187 | Log.e(TAG, "onActivityResult: maxCount = " + mMaxImageCount);
188 | }
189 | }
190 |
191 | public class SelectedPicViewHolder extends RecyclerView.ViewHolder {
192 | private int mPosition;
193 | private ImageView mIvPhoto;
194 |
195 | public SelectedPicViewHolder(View itemView) {
196 | super(itemView);
197 | mIvPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);
198 | mIvPhoto.setOnClickListener(new View.OnClickListener() {
199 | @Override
200 | public void onClick(View v) {
201 | if (getIsAdded() && mPosition == getItemCount() - 1) {
202 | //能够添加
203 | PhotoBottomDialog builder = new PhotoBottomDialog(mContext, mActivity, getTakePhotoIntent(), mSurplusCount);
204 | builder.getAlertDialog().show();
205 | } else {
206 | //预览
207 | previewPhoto(mPosition);
208 | }
209 | }
210 | });
211 | }
212 |
213 | /**
214 | * 预览图片
215 | * @param position 点击图片时的位置
216 | */
217 | public void previewPhoto(int position) {
218 | Intent intent = new Intent(mContext, PhotoPreviewActivity.class);
219 | intent.putExtra(Constant.PHOTO_PREVIEW_LIST, mPhotoList);
220 | intent.putExtra(Constant.PHOTO_POSITION, position);
221 | intent.putExtra(Constant.TITLE_IS_GONE, false);
222 | intent.putExtra(Constant.MAX_COUNT, 5);
223 | mActivity.startActivityForResult(intent, Constant.PREVIEW_PHOTO);
224 | }
225 |
226 | public void setData(int position) {
227 | mPosition = position;
228 | if (getIsAdded() && position == getItemCount() - 1) {
229 | //能添加
230 | mIvPhoto.setScaleType(ImageView.ScaleType.FIT_XY);
231 | mIvPhoto.setImageResource(R.mipmap.content_add_img_icon);
232 | } else {
233 | mIvPhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
234 | String url = mUrlList.get(position);
235 | if (!TextUtils.isEmpty(url) && url != null) {
236 | Log.e(TAG, "setData: url = " + url );
237 | Glide.with(mContext).load(url).into(mIvPhoto);
238 | }
239 | }
240 | }
241 | }
242 | }
--------------------------------------------------------------------------------