├── .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 | 26 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 | ![](screenhots/1.gif) 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 |