├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── misc.xml
├── modules.xml
└── runConfigurations.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── lqr
│ │ └── lqrnativepicselect
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── lqr
│ │ │ └── lqrnativepicselect
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── lqr
│ └── lqrnativepicselect
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── lqr
│ │ └── picselect
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── lqr
│ │ │ └── picselect
│ │ │ └── LQRPhotoSelectUtils.java
│ └── res
│ │ ├── values
│ │ └── strings.xml
│ │ └── xml
│ │ └── file_provider_paths.xml
│ └── test
│ └── java
│ └── com
│ └── lqr
│ └── picselect
│ └── ExampleUnitTest.java
├── screenhots
└── 1.gif
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
56 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 一、简述
2 |
3 | 从 Android 7.0 开始,Android SDK 中的 StrictMode 策略禁止开发人员在应用外部公开 file:// URI。具体表现为,当我们在应用中使用包含 file:// URI 的 Intent 离开自己的应用时,程序会发生故障。本人根据网上给出的适配教程,成功在图片选择及拍照方面适配上了Android 7.0,并自己封装了一个工具类,为了今后开发更加快捷,于是本库就诞生了,如果你愿意使用本库的话,只需调用本库工具类中提供的3个方法(最少),便可轻松完成拍照或图库选取图片的功能需求,且完全不需要考虑Android系统版本的问题,下面来看看如何使用本库。
4 |
5 |
6 | # 二、使用
7 |
8 | ## 1、依赖与权限
9 |
10 | ### 1)依赖
11 |
12 | compile 'com.lqr.picselect:library:1.0.1'
13 |
14 | ### 2)权限
15 |
16 |
17 |
18 |
19 |
20 | ## 2、代码调用
21 |
22 | ### 1)创建LQRPhotoSelectUtils
23 | LQRPhotoSelectUtils提供了两个构造函数,分别是:
24 |
25 | // 可以自定义是否开启图片剪切功能(无法设置剪切大小,默认800*480)
26 | public LQRPhotoSelectUtils(Activity activity, PhotoSelectListener listener, boolean shouldCrop)
27 | // 默认开启图片剪切功能,可以指定剪切大小
28 | public LQRPhotoSelectUtils(Activity activity, PhotoSelectListener listener, int aspectX, int aspectY, int outputX, int outputY)
29 |
30 | >这里有两个要说明的:
31 | >
32 | >1. 部分国产机无法使用图片剪切功能,例如华为TAG-AL00就阉割了图片剪切,但本人亲测小米3就可以进行图片剪切,这是系统优化差异造成的,不是本库的问题!!!
33 | >1. 此外,就算可以进行图片剪切,也不一定可以固定图片大小,亲测小米3与坚果pro无法固定剪切图片的大小,又因为手头上没有“谷歌亲儿子”,所以没法断定是不是本库代码问题,故在此,本人建议使用第一个构造方法。
34 |
35 | 使用第一个构造方法创建LQRPhotoSelectUtils对象示例如下:
36 |
37 | // 创建LQRPhotoSelectUtils(一个Activity对应一个LQRPhotoSelectUtils)
38 | mLqrPhotoSelectUtils = new LQRPhotoSelectUtils(this, new LQRPhotoSelectUtils.PhotoSelectListener() {
39 | @Override
40 | public void onFinish(File outputFile, Uri outputUri) {
41 | // 当拍照或从图库选取图片成功后回调
42 | mTvPath.setText(outputFile.getAbsolutePath());
43 | mTvUri.setText(outputUri.toString());
44 | Glide.with(MainActivity.this).load(outputUri).into(mIvPic);
45 | }
46 | }, true);
47 |
48 | ### 2)关联onActivityResult()
49 |
50 | @Override
51 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
52 | super.onActivityResult(requestCode, resultCode, data);
53 | // 在Activity中的onActivityResult()方法里与LQRPhotoSelectUtils关联
54 | mLqrPhotoSelectUtils.attachToActivityForResult(requestCode, resultCode, data);
55 | }
56 |
57 | ### 3)调用拍照或从图库选取图片方法
58 |
59 | // 调用相机进行拍照
60 | mLqrPhotoSelectUtils.takePhoto();
61 | // 调用图库选取图片
62 | mLqrPhotoSelectUtils.selectPhoto();
63 |
64 |
65 | ### 4)其他设置
66 |
67 | // 设置FileProvider的主机名
68 | mLqrPhotoSelectUtils.setAuthorities();
69 | // 修改图片的存储路径
70 | mLqrPhotoSelectUtils.setImgPath();
71 |
72 | >注意:
73 | >
74 | >如果你app的build.gradle中,defaultConfig{}的applicationId对应的值不是应用包名的话就需要调用setAuthorities()方法;也设置FileProvider的主机名,这是为了适配Android 7.0。取值是applicationId对应的值+".fileprovider"。若applicationId对应的值是应用包名的话则不需理会。
75 |
76 | ## 4、效果
77 |
78 | 如有问题请参考本库的Demo,相信问题并不大,下面看下Demo的运行效果:
79 |
80 | 
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.lqr.lqrnativepicselect"
8 | minSdkVersion 16
9 | targetSdkVersion 25
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(dir: 'libs', include: ['*.jar'])
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.android.support:appcompat-v7:25.3.1'
28 | compile 'com.lovedise:permissiongen:0.0.6'
29 | compile 'com.github.bumptech.glide:glide:3.7.0'
30 | compile project(':library')
31 | testCompile 'junit:junit:4.12'
32 | }
33 |
--------------------------------------------------------------------------------
/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 E:\AndroidSoft\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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/lqr/lqrnativepicselect/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.lqr.lqrnativepicselect;
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.lqr.lqrnativepicselect", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/lqr/lqrnativepicselect/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.lqr.lqrnativepicselect;
2 |
3 | import android.Manifest;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.support.annotation.NonNull;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.view.View;
10 | import android.widget.Button;
11 | import android.widget.ImageView;
12 | import android.widget.TextView;
13 | import android.widget.Toast;
14 |
15 | import com.bumptech.glide.Glide;
16 | import com.lqr.picselect.LQRPhotoSelectUtils;
17 |
18 | import java.io.File;
19 |
20 | import kr.co.namee.permissiongen.PermissionFail;
21 | import kr.co.namee.permissiongen.PermissionGen;
22 | import kr.co.namee.permissiongen.PermissionSuccess;
23 |
24 | public class MainActivity extends AppCompatActivity {
25 |
26 | private Button mBtnTakePhoto;
27 | private Button mBtnSelectPhoto;
28 | private TextView mTvPath;
29 | private TextView mTvUri;
30 | private LQRPhotoSelectUtils mLqrPhotoSelectUtils;
31 | private ImageView mIvPic;
32 |
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | setContentView(R.layout.activity_main);
37 | mBtnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
38 | mBtnSelectPhoto = (Button) findViewById(R.id.btnSelectPhoto);
39 | mTvPath = (TextView) findViewById(R.id.tvPath);
40 | mTvUri = (TextView) findViewById(R.id.tvUri);
41 | mIvPic = (ImageView) findViewById(R.id.ivPic);
42 |
43 | init();
44 | initListener();
45 | }
46 |
47 | private void init() {
48 | // 1、创建LQRPhotoSelectUtils(一个Activity对应一个LQRPhotoSelectUtils)
49 | mLqrPhotoSelectUtils = new LQRPhotoSelectUtils(this, new LQRPhotoSelectUtils.PhotoSelectListener() {
50 | @Override
51 | public void onFinish(File outputFile, Uri outputUri) {
52 | // 4、当拍照或从图库选取图片成功后回调
53 | mTvPath.setText(outputFile.getAbsolutePath());
54 | mTvUri.setText(outputUri.toString());
55 | Glide.with(MainActivity.this).load(outputUri).into(mIvPic);
56 | }
57 | }, true);
58 |
59 | // mLqrPhotoSelectUtils.setAuthorities("com.lqr.lqrnativepicselect.fileprovider");
60 | // mLqrPhotoSelectUtils.setImgPath(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + String.valueOf(System.currentTimeMillis()) + ".jpg");
61 | }
62 |
63 | private void initListener() {
64 | mBtnTakePhoto.setOnClickListener(new View.OnClickListener() {
65 | @Override
66 | public void onClick(View v) {
67 | // 3、调用拍照方法
68 | PermissionGen.with(MainActivity.this)
69 | .addRequestCode(LQRPhotoSelectUtils.REQ_TAKE_PHOTO)
70 | .permissions(Manifest.permission.READ_EXTERNAL_STORAGE,
71 | Manifest.permission.WRITE_EXTERNAL_STORAGE,
72 | Manifest.permission.CAMERA
73 | ).request();
74 | }
75 | });
76 |
77 | mBtnSelectPhoto.setOnClickListener(new View.OnClickListener() {
78 | @Override
79 | public void onClick(View v) {
80 | // 3、调用从图库选取图片方法
81 | PermissionGen.needPermission(MainActivity.this,
82 | LQRPhotoSelectUtils.REQ_SELECT_PHOTO,
83 | new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
84 | Manifest.permission.WRITE_EXTERNAL_STORAGE}
85 | );
86 | }
87 | });
88 | }
89 |
90 | @PermissionSuccess(requestCode = LQRPhotoSelectUtils.REQ_TAKE_PHOTO)
91 | private void takePhoto() {
92 | mLqrPhotoSelectUtils.takePhoto();
93 | }
94 |
95 | @PermissionSuccess(requestCode = LQRPhotoSelectUtils.REQ_SELECT_PHOTO)
96 | private void selectPhoto() {
97 | mLqrPhotoSelectUtils.selectPhoto();
98 | }
99 |
100 | @PermissionFail(requestCode = LQRPhotoSelectUtils.REQ_TAKE_PHOTO)
101 | private void showTip1() {
102 | Toast.makeText(getApplicationContext(), "不给我权限是吧,那就别玩了", Toast.LENGTH_SHORT).show();
103 | }
104 |
105 | @PermissionFail(requestCode = LQRPhotoSelectUtils.REQ_SELECT_PHOTO)
106 | private void showTip2() {
107 | Toast.makeText(getApplicationContext(), "不给我权限是吧,那就别玩了", Toast.LENGTH_SHORT).show();
108 | }
109 |
110 | @Override
111 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
112 | PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
113 | }
114 |
115 | @Override
116 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
117 | super.onActivityResult(requestCode, resultCode, data);
118 | // 2、在Activity中的onActivityResult()方法里与LQRPhotoSelectUtils关联
119 | mLqrPhotoSelectUtils.attachToActivityForResult(requestCode, resultCode, data);
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
17 |
22 |
23 |
27 |
28 |
32 |
33 |
37 |
38 |
42 |
43 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LQRNativePicSelect
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/lqr/lqrnativepicselect/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.lqr.lqrnativepicselect;
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 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.0'
9 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
10 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | jcenter()
19 | }
20 | }
21 |
22 | task clean(type: Delete) {
23 | delete rootProject.buildDir
24 | }
25 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed May 24 08:52:44 CST 2017
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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.2"
7 |
8 | defaultConfig {
9 | minSdkVersion 9
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | compile fileTree(dir: 'libs', include: ['*.jar'])
27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28 | exclude group: 'com.android.support', module: 'support-annotations'
29 | })
30 | compile 'com.android.support:appcompat-v7:25.3.1'
31 | testCompile 'junit:junit:4.12'
32 | }
33 |
34 | //项目主页
35 | def siteUrl = 'https://github.com/GitLqr/LQRNativePicSelect'
36 | //项目的git地址
37 | def gitUrl = 'https://github.com/GitLqr/LQRNativePicSelect.git'
38 | //发布到JCenter上的项目名字
39 | def libName = "LQRNativePicSelect"
40 | //发布到组织名称名字,必须填写
41 | group = "com.lqr.picselect"
42 | // 版本号,下次更新是只需要更改版本号即可
43 | version = "1.0.1"
44 | //上面配置后上传至JCenter后的编译路径是这样的: compile 'me.songning.CircleView:library:1.0.0'
45 | //生成源文件
46 | task sourcesJar(type: Jar) {
47 | from android.sourceSets.main.java.srcDirs
48 | classifier = 'sources'
49 | }
50 | //生成Javadoc文档
51 | task javadoc(type: Javadoc) {
52 | source = android.sourceSets.main.java.srcDirs
53 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
54 | }
55 | //文档打包成jar
56 | task javadocJar(type: Jar, dependsOn: javadoc) {
57 | classifier = 'javadoc'
58 | from javadoc.destinationDir
59 | }
60 | //拷贝javadoc文件
61 | task copyDoc(type: Copy) {
62 | from "${buildDir}/docs/"
63 | into "docs"
64 | }
65 | //上传到JCenter所需要的源码文件
66 | artifacts {
67 | archives javadocJar
68 | archives sourcesJar
69 | }
70 | // 配置maven库,生成POM.xml文件
71 | install {
72 | repositories.mavenInstaller {
73 | // This generates POM.xml with proper parameters
74 | pom {
75 | project {
76 | packaging 'aar'
77 | //项目描述,随意填
78 | name '适配Android7的原生图片选择工具库。'
79 | url siteUrl
80 | licenses {
81 | license {
82 | //开源协议
83 | name 'The Apache Software License, Version 2.0'
84 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
85 | }
86 | }
87 | developers {
88 | developer {
89 | //开发者的个人信息
90 | id 'lqr'
91 | name 'lqr'
92 | email 'linqiarui@gmail.com'
93 | }
94 | }
95 | scm {
96 | connection gitUrl
97 | developerConnection gitUrl
98 | url siteUrl
99 | }
100 | }
101 | }
102 | }
103 | }
104 | //上传到JCenter
105 | Properties properties = new Properties()
106 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
107 | bintray {
108 | user = properties.getProperty("bintray.user") //读取 local.properties 文件里面的 bintray.user
109 | key = properties.getProperty("bintray.apikey") //读取 local.properties 文件里面的 bintray.apikey
110 | configurations = ['archives']
111 | pkg {
112 | //这里的repo值必须要和你创建Maven仓库的时候的名字一样
113 | repo = "maven"
114 | //发布到JCenter上的项目名字
115 | name = libName
116 | //项目描述
117 | desc = '适配Android7的原生图片选择工具库。'
118 | websiteUrl = siteUrl
119 | vcsUrl = gitUrl
120 | licenses = ["Apache-2.0"]
121 | publish = true
122 | }
123 | }
124 | javadoc {
125 | options{
126 | //如果你的项目里面有中文注释的话,必须将格式设置为UTF-8,不然会出现乱码
127 | encoding "UTF-8"
128 | charSet 'UTF-8'
129 | author true
130 | version true
131 | links "http://docs.oracle.com/javase/7/docs/api"
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/library/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 E:\AndroidSoft\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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/lqr/picselect/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.lqr.picselect;
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.lqr.library.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
10 |
15 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/library/src/main/java/com/lqr/picselect/LQRPhotoSelectUtils.java:
--------------------------------------------------------------------------------
1 | package com.lqr.picselect;
2 |
3 | import android.app.Activity;
4 | import android.content.ContentValues;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.database.Cursor;
8 | import android.graphics.Bitmap;
9 | import android.net.Uri;
10 | import android.os.Build;
11 | import android.os.Environment;
12 | import android.provider.MediaStore;
13 | import android.support.v4.content.FileProvider;
14 |
15 | import java.io.File;
16 |
17 | /**
18 | * CSDN_LQR
19 | * 图片选择工具类
20 | */
21 | public class LQRPhotoSelectUtils {
22 |
23 | public static final int REQ_TAKE_PHOTO = 10001;
24 | public static final int REQ_SELECT_PHOTO = 10002;
25 | public static final int REQ_ZOOM_PHOTO = 10003;
26 |
27 | private Activity mActivity;
28 | //拍照或剪切后图片的存放位置(参考file_provider_paths.xml中的路径)
29 | private String imgPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + String.valueOf(System.currentTimeMillis()) + ".jpg";
30 | //FileProvider的主机名:一般是包名+".fileprovider",严格上是build.gradle中defaultConfig{}中applicationId对应的值+".fileprovider"
31 | private String AUTHORITIES = "packageName" + ".fileprovider";
32 | private boolean mShouldCrop = false;//是否要裁剪(默认不裁剪)
33 | private Uri mOutputUri = null;
34 | private File mInputFile;
35 | private File mOutputFile = null;
36 |
37 | //剪裁图片宽高比
38 | private int mAspectX = 1;
39 | private int mAspectY = 1;
40 | //剪裁图片大小
41 | private int mOutputX = 800;
42 | private int mOutputY = 480;
43 | PhotoSelectListener mListener;
44 |
45 | /**
46 | * 可指定是否在拍照或从图库选取照片后进行裁剪
47 | *
48 | * 默认裁剪比例1:1,宽度为800,高度为480
49 | *
50 | * @param activity 上下文
51 | * @param listener 选取图片监听
52 | * @param shouldCrop 是否裁剪
53 | */
54 | public LQRPhotoSelectUtils(Activity activity, PhotoSelectListener listener, boolean shouldCrop) {
55 | mActivity = activity;
56 | mListener = listener;
57 | mShouldCrop = shouldCrop;
58 | AUTHORITIES = activity.getPackageName() + ".fileprovider";
59 | imgPath = generateImgePath();
60 | }
61 |
62 | /**
63 | * 可以拍照或从图库选取照片后裁剪的比例及宽高
64 | *
65 | * @param activity 上下文
66 | * @param listener 选取图片监听
67 | * @param aspectX 图片裁剪时的宽度比例
68 | * @param aspectY 图片裁剪时的高度比例
69 | * @param outputX 图片裁剪后的宽度
70 | * @param outputY 图片裁剪后的高度
71 | */
72 | public LQRPhotoSelectUtils(Activity activity, PhotoSelectListener listener, int aspectX, int aspectY, int outputX, int outputY) {
73 | this(activity, listener, true);
74 | mAspectX = aspectX;
75 | mAspectY = aspectY;
76 | mOutputX = outputX;
77 | mOutputY = outputY;
78 | }
79 |
80 | /**
81 | * 设置FileProvider的主机名:一般是包名+".fileprovider",严格上是build.gradle中defaultConfig{}中applicationId对应的值+".fileprovider"
82 | *
83 | * 该工具默认是应用的包名+".fileprovider",如项目build.gradle中defaultConfig{}中applicationId不是包名,则必须调用此方法对FileProvider的主机名进行设置,否则Android7.0以上使用异常
84 | *
85 | * @param authorities FileProvider的主机名
86 | */
87 | public void setAuthorities(String authorities) {
88 | this.AUTHORITIES = authorities;
89 | }
90 |
91 | /**
92 | * 修改图片的存储路径(默认的图片存储路径是SD卡上 Android/data/应用包名/时间戳.jpg)
93 | *
94 | * @param imgPath 图片的存储路径(包括文件名和后缀)
95 | */
96 | public void setImgPath(String imgPath) {
97 | this.imgPath = imgPath;
98 | }
99 |
100 | /**
101 | * 拍照获取
102 | */
103 | public void takePhoto() {
104 | File imgFile = new File(imgPath);
105 | if (!imgFile.getParentFile().exists()) {
106 | imgFile.getParentFile().mkdirs();
107 | }
108 | Uri imgUri = null;
109 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
110 | imgUri = FileProvider.getUriForFile(mActivity, AUTHORITIES, imgFile);
111 | } else {
112 | imgUri = Uri.fromFile(imgFile);
113 | }
114 |
115 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
116 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
117 | mActivity.startActivityForResult(intent, REQ_TAKE_PHOTO);
118 | }
119 |
120 | /**
121 | * 从图库获取
122 | */
123 | public void selectPhoto() {
124 | Intent intent = new Intent(Intent.ACTION_PICK, null);
125 | intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
126 | mActivity.startActivityForResult(intent, REQ_SELECT_PHOTO);
127 | }
128 |
129 | private void zoomPhoto(File inputFile, File outputFile) {
130 | File parentFile = outputFile.getParentFile();
131 | if (!parentFile.exists()) {
132 | parentFile.mkdirs();
133 | }
134 | Intent intent = new Intent("com.android.camera.action.CROP");
135 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
136 | intent.setDataAndType(getImageContentUri(mActivity, inputFile), "image/*");
137 | } else {
138 | intent.setDataAndType(Uri.fromFile(inputFile), "image/*");
139 | }
140 | intent.putExtra("crop", "true");
141 |
142 | //设置剪裁图片宽高比
143 | intent.putExtra("mAspectX", mAspectX);
144 | intent.putExtra("mAspectY", mAspectY);
145 |
146 | //设置剪裁图片大小
147 | intent.putExtra("mOutputX", mOutputX);
148 | intent.putExtra("mOutputY", mOutputY);
149 |
150 | // 是否返回uri
151 | intent.putExtra("return-data", false);
152 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
153 | intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
154 |
155 | mActivity.startActivityForResult(intent, REQ_ZOOM_PHOTO);
156 | }
157 |
158 | public void attachToActivityForResult(int requestCode, int resultCode, Intent data) {
159 | if (resultCode == Activity.RESULT_OK) {
160 | switch (requestCode) {
161 | case LQRPhotoSelectUtils.REQ_TAKE_PHOTO://拍照
162 | mInputFile = new File(imgPath);
163 | if (mShouldCrop) {
164 | mOutputFile = new File(generateImgePath());
165 | mOutputUri = Uri.fromFile(mOutputFile);
166 | zoomPhoto(mInputFile, mOutputFile);
167 | } else {
168 | mOutputUri = Uri.fromFile(mInputFile);
169 | if (mListener != null) {
170 | mListener.onFinish(mInputFile, mOutputUri);
171 | }
172 | }
173 | break;
174 | case LQRPhotoSelectUtils.REQ_SELECT_PHOTO://图库
175 | if (data != null) {
176 | Uri sourceUri = data.getData();
177 | String[] proj = {MediaStore.Images.Media.DATA};
178 | Cursor cursor = mActivity.managedQuery(sourceUri, proj, null, null, null);
179 | int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
180 | cursor.moveToFirst();
181 | String imgPath = cursor.getString(columnIndex);
182 | mInputFile = new File(imgPath);
183 |
184 | if (mShouldCrop) {
185 | mOutputFile = new File(generateImgePath());
186 | mOutputUri = Uri.fromFile(mOutputFile);
187 | zoomPhoto(mInputFile, mOutputFile);
188 | } else {
189 | mOutputUri = Uri.fromFile(mInputFile);
190 | if (mListener != null) {
191 | mListener.onFinish(mInputFile, mOutputUri);
192 | }
193 | }
194 | }
195 | break;
196 | case LQRPhotoSelectUtils.REQ_ZOOM_PHOTO://裁剪
197 | if (data != null) {
198 | if (mOutputUri != null) {
199 | //删除拍照的临时照片
200 | File tmpFile = new File(imgPath);
201 | if (tmpFile.exists())
202 | tmpFile.delete();
203 | if (mListener != null) {
204 | mListener.onFinish(mOutputFile, mOutputUri);
205 | }
206 | }
207 | }
208 | break;
209 | }
210 | }
211 | }
212 |
213 | /**
214 | * 安卓7.0裁剪根据文件路径获取uri
215 | */
216 | private Uri getImageContentUri(Context context, File imageFile) {
217 | String filePath = imageFile.getAbsolutePath();
218 | Cursor cursor = context.getContentResolver().query(
219 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
220 | new String[]{MediaStore.Images.Media._ID},
221 | MediaStore.Images.Media.DATA + "=? ",
222 | new String[]{filePath}, null);
223 |
224 | if (cursor != null && cursor.moveToFirst()) {
225 | int id = cursor.getInt(cursor
226 | .getColumnIndex(MediaStore.MediaColumns._ID));
227 | Uri baseUri = Uri.parse("content://media/external/images/media");
228 | return Uri.withAppendedPath(baseUri, "" + id);
229 | } else {
230 | if (imageFile.exists()) {
231 | ContentValues values = new ContentValues();
232 | values.put(MediaStore.Images.Media.DATA, filePath);
233 | return context.getContentResolver().insert(
234 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
235 | } else {
236 | return null;
237 | }
238 | }
239 | }
240 |
241 | /**
242 | * 产生图片的路径,带文件夹和文件名,文件名为当前毫秒数
243 | */
244 | private String generateImgePath() {
245 | return getExternalStoragePath() + File.separator + String.valueOf(System.currentTimeMillis()) + ".jpg";
246 | // return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + String.valueOf(System.currentTimeMillis()) + ".jpg";//测试用
247 | }
248 |
249 |
250 | /**
251 | * 获取SD下的应用目录
252 | */
253 | private String getExternalStoragePath() {
254 | StringBuilder sb = new StringBuilder();
255 | sb.append(Environment.getExternalStorageDirectory().getAbsolutePath());
256 | sb.append(File.separator);
257 | String ROOT_DIR = "Android/data/" + mActivity.getPackageName();
258 | sb.append(ROOT_DIR);
259 | sb.append(File.separator);
260 | return sb.toString();
261 | }
262 |
263 | public interface PhotoSelectListener {
264 | void onFinish(File outputFile, Uri outputUri);
265 | }
266 | }
267 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | library
3 |
4 |
--------------------------------------------------------------------------------
/library/src/main/res/xml/file_provider_paths.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/library/src/test/java/com/lqr/picselect/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.lqr.picselect;
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 | }
--------------------------------------------------------------------------------
/screenhots/1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GitLqr/LQRNativePicSelect/841209053278d0dfeab0d002c908018a0620f9e3/screenhots/1.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------