├── lib
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── brunodles
│ │ ├── compressor
│ │ ├── Compressor.java
│ │ ├── SizeCompressor.java
│ │ ├── QualityCompressor.java
│ │ └── BitmapCompressor.java
│ │ └── picpicker
│ │ ├── listener
│ │ ├── CantFindCameraAppErrorListener.java
│ │ ├── ErrorCreatingTempFileForCameraListener.java
│ │ ├── NeedWritePermissionErrorListener.java
│ │ ├── PicResultListener.java
│ │ └── ActivityStarter.java
│ │ ├── Activity_ActivityStarter.java
│ │ ├── AddImageAsyncTask.java
│ │ ├── impl
│ │ └── WritePermissionAsker.java
│ │ └── PicPicker.java
├── proguard-rules.pro
└── build.gradle
├── sample
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── brunodles
│ │ │ └── picpicker
│ │ │ └── sample
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── brunodles
│ │ │ └── picpicker
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── brunodles
│ │ └── picpicker
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── LICENSE.md
├── .travis.yml
├── .gitignore
├── README.md
├── gradlew.bat
└── gradlew
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample'
2 | include 'lib'
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brunodles/PicPicker/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brunodles/PicPicker/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brunodles/PicPicker/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brunodles/PicPicker/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brunodles/PicPicker/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brunodles/PicPicker/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PicPicker Sample
3 | We need to write on disk to use camera.
4 |
5 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jan 26 14:18:16 BRT 2016
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-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/compressor/Compressor.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.compressor;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Created by bruno on 05/08/16.
7 | */
8 | public interface Compressor {
9 | Bitmap compress(Bitmap bitmap, int targetSize);
10 | }
11 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/listener/CantFindCameraAppErrorListener.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.listener;
2 |
3 | /**
4 | * This interface will be called when the lib fails to find the camera app.
5 | * Created by bruno on 29/11/15.
6 | */
7 | public interface CantFindCameraAppErrorListener {
8 | void cantFindCameraApp();
9 | }
10 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/listener/ErrorCreatingTempFileForCameraListener.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.listener;
2 |
3 | /**
4 | * This interface will be called when the lib fails to create a temp file.
5 | * Created by bruno on 29/11/15.
6 | */
7 | public interface ErrorCreatingTempFileForCameraListener {
8 | void errorCreatingTempFileForCamera();
9 | }
10 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/listener/NeedWritePermissionErrorListener.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.listener;
2 |
3 | /**
4 | * This interface will be called when the user need to be authorize the app to write a temp file.
5 | * Created by bruno on 29/11/15.
6 | */
7 | public interface NeedWritePermissionErrorListener {
8 | void needWritePermission();
9 | }
10 |
--------------------------------------------------------------------------------
/sample/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/listener/PicResultListener.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.listener;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * This interface will receive a bitmap when the lib got the response.
7 | * Created by brunodles on 10/11/14.
8 | */
9 | public interface PicResultListener {
10 | void onPictureResult(Bitmap bitmap);
11 | }
12 |
--------------------------------------------------------------------------------
/sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/sample/src/test/java/com/github/brunodles/picpicker/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/sample/src/androidTest/java/com/github/brunodles/picpicker/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/Activity_ActivityStarter.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 |
6 | import com.github.brunodles.picpicker.listener.ActivityStarter;
7 |
8 | /**
9 | * Created by bruno on 31/01/16.
10 | */
11 | class Activity_ActivityStarter implements ActivityStarter {
12 |
13 | private Activity activity;
14 |
15 | public Activity_ActivityStarter(Activity activity) {
16 | this.activity = activity;
17 | }
18 |
19 | @Override
20 | public void startActivityForResult(Intent intent, int requestCode) {
21 | activity.startActivityForResult(intent, requestCode);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/sample/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 /home/bruno/android-sdk-linux/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 |
--------------------------------------------------------------------------------
/lib/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 /home/bruno/android-sdk-linux/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 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | applicationId "com.github.brunodles.picpicker.sample"
9 | minSdkVersion 14
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
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 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.2.1'
26 | compile 'com.android.support:design:23.2.1'
27 | compile project(':lib')
28 | }
29 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/listener/ActivityStarter.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.listener;
2 |
3 | import android.content.Intent;
4 |
5 | /**
6 | * This interface is need just to make who will care about the onActivityResponse listener.
7 | * If you start an activity from a fragment, the response will be sent to OnActivityResponse on that fragment.
8 | * But if you start and activity from another Activity the response won't reach the fragment.
9 | *
10 | * You don't even need to implement it, you just need to make you actvivity or fragment implement it.
11 | * It will just work, cuz the method already exists.
12 | *
13 | * Created by brunodles on 10/11/14.
14 | */
15 | public interface ActivityStarter {
16 |
17 | void startActivityForResult(Intent intent, int requestCodeAttachImage);
18 | }
19 |
--------------------------------------------------------------------------------
/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 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) [2016] [Bruno de Lima e Silva]
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group = 'com.github.brunodles'
5 |
6 | android {
7 | compileSdkVersion 23
8 | buildToolsVersion "23.0.2"
9 |
10 | defaultConfig {
11 | minSdkVersion 14
12 | targetSdkVersion 23
13 | versionCode 1
14 | versionName "1.0"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | testCompile 'junit:junit:4.12'
27 | compile 'com.android.support:appcompat-v7:23.2.1'
28 | }
29 |
30 | task sourcesJar(type: Jar) {
31 | from android.sourceSets.main.java.srcDirs
32 | classifier = 'sources'
33 | }
34 |
35 | task javadoc(type: Javadoc) {
36 | failOnError false
37 | source = android.sourceSets.main.java.sourceFiles
38 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
39 | }
40 |
41 | task javadocJar(type: Jar, dependsOn: javadoc) {
42 | classifier = 'javadoc'
43 | from javadoc.destinationDir
44 | }
45 |
46 | artifacts {
47 | archives sourcesJar
48 | archives javadocJar
49 | }
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 |
3 | jdk:
4 | - oraclejdk8
5 |
6 | before_install:
7 | - chmod +x gradlew
8 |
9 | android:
10 | components:
11 | # Uncomment the lines below if you want to
12 | # use the latest revision of Android SDK Tools
13 | - platform-tools
14 | - tools
15 |
16 | # The BuildTools version used by your project
17 | - build-tools-23.0.2
18 |
19 | # The SDK version used to compile your project
20 | - android-23
21 |
22 | # Additional components
23 | #- extra-google-google_play_services
24 | - extra-google-m2repository
25 | - extra-android-m2repository
26 |
27 | # Specify at least one system image,
28 | # if you need to run emulator(s) during your tests
29 | #- sys-img-armeabi-v7a-android-19
30 | #- sys-img-x86-android-17
31 |
32 |
33 | #before_script:
34 | # - echo no | android create avd --force -n test -t android-17 --abi armeabi-v7a
35 | # - emulator -avd test -no-skin -no-audio -no-window &
36 | # - android-wait-for-emulator
37 | # - adb shell input keyevent 82 &
38 |
39 | # This will run all tests, including the sample and instrumentation tests, requires a connected device.
40 | #script: ./gradlew build connectedCheck
41 |
42 | # So, let's use this for awhile, jus run unit test for the lib
43 | script: ./gradlew build lib:test
44 |
45 | notifications:
46 | email: false
47 |
48 | sudo: false
49 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/compressor/SizeCompressor.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.compressor;
2 |
3 | import android.graphics.Bitmap;
4 | import android.util.Log;
5 |
6 | import static android.graphics.Bitmap.createScaledBitmap;
7 |
8 | /**
9 | * Created by bruno on 05/08/16.
10 | */
11 | public class SizeCompressor implements Compressor {
12 | private static final String TAG = "SizeCompressor";
13 |
14 | @Override
15 | public Bitmap compress(Bitmap bitmap, int targetSize) {
16 | float ratio = 1;
17 | Bitmap result = bitmap;
18 | do {
19 | try {
20 | int currentSize = result.getByteCount() / 1024;
21 | Log.d(TAG, "compress: currentSize " + currentSize);
22 | if (currentSize <= targetSize) return result;
23 |
24 | ratio = targetSize / (float) currentSize;
25 |
26 | if (ratio <= 0) ratio = 0.1F;
27 |
28 | Log.d(TAG, "compress: ratio " + ratio);
29 | int width = Math.round(ratio * bitmap.getWidth());
30 | int height = Math.round(ratio * bitmap.getHeight());
31 | result = createScaledBitmap(bitmap, width, height, false);
32 | } catch (Exception e) {
33 | Log.e(TAG, "Error resizing bitmap " + ratio, e);
34 | }
35 | } while (ratio >= 0);
36 | return result;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
20 |
21 |
25 |
26 |
33 |
34 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/compressor/QualityCompressor.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.compressor;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.util.Log;
6 |
7 | import java.io.ByteArrayOutputStream;
8 |
9 | /**
10 | * Created by bruno on 05/08/16.
11 | */
12 | public class QualityCompressor implements Compressor {
13 | private static final String TAG = "QualityCompressor";
14 | private static final Bitmap.CompressFormat DEFAULT_FILE_FORMAT = Bitmap.CompressFormat.JPEG;
15 |
16 | private ByteArrayOutputStream out = new ByteArrayOutputStream();
17 |
18 | @Override
19 | public Bitmap compress(Bitmap bitmap, int targetSize) {
20 | int quality = 100;
21 | int interactions = 0;
22 | while (quality >= 0) {
23 | try {
24 | Log.d(TAG, "compress: quality " + quality);
25 | out.reset();
26 | bitmap.compress(DEFAULT_FILE_FORMAT, quality, out);
27 | int currentSize = out.size() / 1024;
28 | Log.d(TAG, "compress: currentSize " + currentSize);
29 | if (currentSize <= targetSize)
30 | break;
31 | int nextQuality = ((targetSize * quality) / currentSize);
32 | if (nextQuality == quality)
33 | break;
34 | else
35 | quality = nextQuality;
36 | } catch (Exception e) {
37 | Log.e(TAG, "Error compressing bitmap " + quality, e);
38 | }
39 | interactions++;
40 | if (interactions > 5)
41 | break;
42 | }
43 | if (out.size() > 0)
44 | return BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
45 | return bitmap;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/AddImageAsyncTask.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.net.Uri;
7 | import android.os.AsyncTask;
8 | import android.util.Log;
9 |
10 | import com.github.brunodles.picpicker.listener.PicResultListener;
11 |
12 | import java.io.IOException;
13 | import java.io.InputStream;
14 |
15 | /**
16 | * Created by brunodles on 10/11/14.
17 | */
18 | class AddImageAsyncTask extends AsyncTask {
19 |
20 | private static final String TAG = "AddImageAsyncTask";
21 |
22 | protected final Context context;
23 | protected final Uri url;
24 | private final PicResultListener listener;
25 |
26 | public AddImageAsyncTask(Context context, Uri url, PicResultListener listener) {
27 | this.context = context;
28 | this.url = url;
29 | this.listener = listener;
30 | Log.d(TAG, "AddFileAsyncTask url " + url);
31 | }
32 |
33 | @Override
34 | protected Bitmap doInBackground(Void... params) {
35 | try {
36 | return getBitmapFromStream();
37 | } catch (Exception e) {
38 | Log.e(TAG, "doInBackground ", e);
39 | }
40 | return null;
41 | }
42 |
43 | private Bitmap getBitmapFromStream() throws IOException {
44 | InputStream stream = context.getContentResolver().openInputStream(url);
45 | Bitmap bitmap = BitmapFactory.decodeStream(stream);
46 | if (stream != null) stream.close();
47 | return bitmap;
48 | }
49 |
50 | @Override
51 | protected void onPostExecute(Bitmap bitmap) {
52 | if (listener != null) listener.onPictureResult(bitmap);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/compressor/BitmapCompressor.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.compressor;
2 |
3 | import android.graphics.Bitmap;
4 | import android.os.AsyncTask;
5 |
6 | /**
7 | * This class will help you to compress your bitmap.
8 | * When you get images from the others that image can have huge sizes, and sometimes we don't need
9 | * all that. Then you can use this class to help you with that. Just inform the wanted
10 | * image size (in kbytes) and how you want to compress it.
11 | *
12 | * Look {@link Compressor} and it's implementations {@link QualityCompressor} and {@link SizeCompressor}
13 | *
14 | * Created by bruno on 31/01/16.
15 | */
16 | public class BitmapCompressor extends AsyncTask {
17 |
18 | private static final String TAG = "BitmapCompressor";
19 |
20 | private final Compressor compressor;
21 | private final int targetSize;
22 |
23 | /**
24 | * @param compressor the script that will be used to compress the image {@link Compressor}
25 | * @param targetSizeInKbytes the wanted image size in kbytes.
26 | */
27 | public BitmapCompressor(Compressor compressor, int targetSizeInKbytes) {
28 | this.compressor = compressor;
29 | this.targetSize = targetSizeInKbytes;
30 | }
31 |
32 | /**
33 | * This use the {@link QualityCompressor} to compress the image.
34 | *
35 | * @param targetSizeInKbytes the wanted image size in kbytes.
36 | */
37 | public BitmapCompressor(int targetSizeInKbytes) {
38 | this(new QualityCompressor(), targetSizeInKbytes);
39 | }
40 |
41 | @Override
42 | protected Bitmap[] doInBackground(Bitmap... params) {
43 | Bitmap[] bitmaps = new Bitmap[params.length];
44 | for (int i = 0; i < params.length; i++) {
45 | bitmaps[i] = compressor.compress(params[i], targetSize);
46 | }
47 | return bitmaps;
48 | }
49 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/android,android,intellij
3 |
4 | ### Android ###
5 | # Built application files
6 | *.apk
7 | *.ap_
8 |
9 | # Files for the Dalvik VM
10 | *.dex
11 |
12 | # Java class files
13 | *.class
14 |
15 | # Generated files
16 | bin/
17 | gen/
18 |
19 | # Gradle files
20 | .gradle/
21 | build/
22 |
23 | # Local configuration file (sdk path, etc)
24 | local.properties
25 |
26 | # Proguard folder generated by Eclipse
27 | proguard/
28 |
29 | # Log Files
30 | *.log
31 |
32 | # Android Studio Navigation editor temp files
33 | .navigation/
34 |
35 | # Android Studio captures folder
36 | captures/
37 |
38 | ### Android Patch ###
39 | gen-external-apklibs
40 |
41 |
42 | ### Android ###
43 | # Built application files
44 | *.apk
45 | *.ap_
46 |
47 | # Files for the Dalvik VM
48 | *.dex
49 |
50 | # Java class files
51 | *.class
52 |
53 | # Generated files
54 | bin/
55 | gen/
56 |
57 | # Gradle files
58 | .gradle/
59 | build/
60 |
61 | # Local configuration file (sdk path, etc)
62 | local.properties
63 |
64 | # Proguard folder generated by Eclipse
65 | proguard/
66 |
67 | # Log Files
68 | *.log
69 |
70 | # Android Studio Navigation editor temp files
71 | .navigation/
72 |
73 | # Android Studio captures folder
74 | captures/
75 |
76 | ### Android Patch ###
77 | gen-external-apklibs
78 |
79 |
80 | ### Intellij ###
81 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
82 |
83 | *.iml
84 |
85 | ## Directory-based project format:
86 | .idea/
87 | # if you remove the above rule, at least ignore the following:
88 |
89 | # User-specific stuff:
90 | # .idea/workspace.xml
91 | # .idea/tasks.xml
92 | # .idea/dictionaries
93 | # .idea/shelf
94 |
95 | # Sensitive or high-churn files:
96 | # .idea/dataSources.ids
97 | # .idea/dataSources.xml
98 | # .idea/sqlDataSources.xml
99 | # .idea/dynamic.xml
100 | # .idea/uiDesigner.xml
101 |
102 | # Gradle:
103 | # .idea/gradle.xml
104 | # .idea/libraries
105 |
106 | # Mongo Explorer plugin:
107 | # .idea/mongoSettings.xml
108 |
109 | ## File-based project format:
110 | *.ipr
111 | *.iws
112 |
113 | ## Plugin-specific files:
114 |
115 | # IntelliJ
116 | /out/
117 |
118 | # mpeltonen/sbt-idea plugin
119 | .idea_modules/
120 |
121 | # JIRA plugin
122 | atlassian-ide-plugin.xml
123 |
124 | # Crashlytics plugin (for Android Studio and IntelliJ)
125 | com_crashlytics_export_strings.xml
126 | crashlytics.properties
127 | crashlytics-build.properties
128 | fabric.properties
129 |
130 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PicPicker
2 |
3 | [](https://jitpack.io/#brunodles/PicPicker)
4 | [](https://travis-ci.org/brunodles/PicPicker)
5 | [](https://android-arsenal.com/api?level=14)
6 |
7 | A simple library to pick pictures from the gallery and camera.
8 | By using a single object to make the requests.
9 |
10 | ## How add it
11 | Add it to your build.gradle with:
12 | ```gradle
13 | repositories {
14 | maven { url "https://jitpack.io" }
15 | }
16 | ```
17 | and:
18 |
19 | ```gradle
20 | dependencies {
21 | compile 'com.github.brunodles:PicPicker:{latest version}'
22 | }
23 | ```
24 |
25 | Ok, now you have the lib on your project, let's see how to use it.
26 |
27 | ## Seting things up
28 | First make a property on your `activity` or `fragment`, like this.
29 | ```private PicPicker picPicker;```
30 |
31 | Then on the `onCreate` method you should initialize it.
32 | ```java
33 | picPicker = new PicPicker(imageView, this)
34 | ```
35 |
36 | That `this` on the code means the `ActivityStarter` it's a class that will start the camera or
37 | gallery app intent.
38 | To make it work like that we need to add `implements ActivityStarter` on our `activity` or
39 | `fragment`. Don't even need to change anything. This is needed to let the lib know where the
40 | response will be sent.
41 |
42 | Now we need to pass the result to the lib and to do that we just need to _override_
43 | `onActivityResult` and pass it's parameters to the lib, just like that.
44 | ```java
45 | @Override
46 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
47 | picPicker.onActivityResult(requestCode, resultCode, data);
48 | super.onActivityResult(requestCode, resultCode, data);
49 | }
50 | ```
51 |
52 | You can even make a validation to check if the lib had used those parameters, it returns `true`
53 | when made something to it.
54 |
55 | ## Using it
56 | Now to use the lib is so simple, to pick a image from the gallery just call.
57 | ```java
58 | picPicker.gallery();
59 | ```
60 |
61 | If you want to grab a image from the camera call.
62 | ```java
63 | picPicker.camera();
64 | ```
65 |
66 | # Sample
67 | You can see more thing on sample, it have some explanations too.
68 | On the sample you will see how to:
69 | * work with runtime permissions.
70 | * grab the bitmap.
71 | * listen for possible errors
72 |
73 | # You can help this lib to grow
74 | If you fond something wrong or if you want some feature, just create a issue or even better create
75 | a pull request with you idea.
76 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/impl/WritePermissionAsker.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.impl;
2 |
3 | import android.Manifest;
4 | import android.app.Activity;
5 | import android.content.DialogInterface;
6 | import android.content.pm.PackageManager;
7 | import android.support.annotation.NonNull;
8 | import android.support.annotation.StringRes;
9 | import android.support.v4.app.ActivityCompat;
10 | import android.support.v7.app.AlertDialog;
11 |
12 | import com.github.brunodles.picpicker.listener.NeedWritePermissionErrorListener;
13 |
14 | /**
15 | * This class can be passed as parameter to {@link com.github.brunodles.picpicker.PicPicker} as a
16 | * Listener.
17 | * Created by bruno on 30/01/16.
18 | */
19 | public class WritePermissionAsker implements NeedWritePermissionErrorListener {
20 |
21 | private Activity activity;
22 | private int requestCode;
23 | private final int dialogMessage;
24 |
25 | public WritePermissionAsker(Activity activity, int requestCode, @StringRes int dialogMessage) {
26 | this.activity = activity;
27 | this.requestCode = requestCode;
28 | this.dialogMessage = dialogMessage;
29 | }
30 |
31 | @Override
32 | public void needWritePermission() {
33 | askPermission();
34 | }
35 |
36 | public void askPermission() {
37 | if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
38 | Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
39 | // Show an expanation to the user *asynchronously* -- don't block
40 | // this thread waiting for the user's response! After the user
41 | // sees the explanation, try again to request the permission.
42 | new AlertDialog.Builder(activity)
43 | .setMessage(dialogMessage)
44 | .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
45 | @Override
46 | public void onClick(DialogInterface dialog, int which) {
47 | ActivityCompat.requestPermissions(activity,
48 | new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
49 | requestCode);
50 | }
51 | })
52 | .show();
53 | } else {
54 | // No explanation needed, we can request the permission.
55 | ActivityCompat.requestPermissions(activity,
56 | new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
57 | requestCode);
58 | }
59 | }
60 |
61 | /**
62 | * This method should be called from the activity to enable this class to manage the request
63 | * permission result.
64 | *
65 | * @return Will return true if the permission was granted.
66 | */
67 | public boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
68 | @NonNull int[] grantResults) {
69 | return ((requestCode == this.requestCode)
70 | && (grantResults.length > 0)
71 | && (grantResults[0] == PackageManager.PERMISSION_GRANTED));
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/github/brunodles/picpicker/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker.sample;
2 |
3 | import android.content.Intent;
4 | import android.graphics.Bitmap;
5 | import android.os.Bundle;
6 | import android.support.annotation.NonNull;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.util.Log;
9 | import android.view.View;
10 | import android.widget.Button;
11 | import android.widget.ImageView;
12 | import android.widget.Toast;
13 |
14 | import com.github.brunodles.compressor.BitmapCompressor;
15 | import com.github.brunodles.picpicker.PicPicker;
16 | import com.github.brunodles.picpicker.impl.WritePermissionAsker;
17 | import com.github.brunodles.picpicker.listener.ActivityStarter;
18 | import com.github.brunodles.picpicker.listener.CantFindCameraAppErrorListener;
19 | import com.github.brunodles.picpicker.listener.ErrorCreatingTempFileForCameraListener;
20 | import com.github.brunodles.picpicker.listener.PicResultListener;
21 |
22 | public class MainActivity extends AppCompatActivity implements ActivityStarter,
23 | View.OnClickListener {
24 | private static final String TAG = "MainActivity";
25 | // This is the request code used to ask write permission
26 | private static final int RC_WRITE_EXTERNAL_STORAGE = 42;
27 |
28 | private Button galery;
29 | private Button camera;
30 | private ImageView image;
31 | private PicPicker picPicker;
32 | private WritePermissionAsker writePermissionAsker;
33 |
34 | @Override
35 | protected void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_main);
38 | galery = (Button) findViewById(R.id.galery);
39 | camera = (Button) findViewById(R.id.camera);
40 | image = (ImageView) findViewById(R.id.image);
41 |
42 | // This is the default implementation of the permission asker,
43 | // but you can write your another implementation, or ask the permission for your own.
44 | writePermissionAsker = new WritePermissionAsker(this, RC_WRITE_EXTERNAL_STORAGE,
45 | R.string.permission_message);
46 |
47 | // Prepare the picPicker
48 | picPicker = new PicPicker(this, picResultListener)
49 | // the will be called when we get a picture from the camera
50 | .setFileForCameraListener(fileForCameraListener)
51 | // this will be called when we got a error from the camera app,
52 | // sometimes it even doesn't exists.
53 | .setCameraAppErrorListener(cameraAppErrorListener)
54 | // this will be called when we need a permission, for android 6+
55 | .setPermissionErrorListener(writePermissionAsker);
56 |
57 | galery.setOnClickListener(this);
58 | camera.setOnClickListener(this);
59 | }
60 |
61 |
62 | @Override
63 | public void onClick(View v) {
64 | if (v == galery)
65 | picPicker.gallery(); // Here you call the gallery app
66 | else if (v == camera)
67 | picPicker.camera(); // Here you call the camera app
68 | }
69 |
70 | @Override
71 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
72 | @NonNull int[] grantResults) {
73 | // You need to pass the permission result to writePermissionAsker, so the lib can check
74 | // if the app have write permission. Having permission we start the camera.
75 | if (writePermissionAsker.onRequestPermissionsResult(requestCode, permissions, grantResults))
76 | picPicker.camera();
77 | }
78 |
79 | @Override
80 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
81 | // You need to pass the activity result to picPicker, so the lib can check the response from
82 | // the gallery or camera. It will return true if found the image.
83 | if (!picPicker.onActivityResult(requestCode, resultCode, data))
84 | super.onActivityResult(requestCode, resultCode, data);
85 | }
86 |
87 | // This listener will receive the bitmap from the picPicture, so you don't need to worry about from
88 | // where we got the image.
89 | private PicResultListener picResultListener = new PicResultListener() {
90 | @Override
91 | public void onPictureResult(final Bitmap bitmap) {
92 | Log.d(TAG, "onPictureResult: ");
93 | image.setImageBitmap(bitmap); // the the bitmap on a ImageView
94 | // This is the BitmapCompressor, it will compress the image to a better size.
95 | // We may not need a full sized picture on our services.
96 | // This is a AsyncTask, so we need to implement the onPostExecute
97 | // This could even be a field on the Activity, it's here just to show that it's optional.
98 | new BitmapCompressor(400) {
99 | @Override
100 | protected void onPostExecute(Bitmap[] bitmaps) {
101 | // Here we get the response from the bitmapCompressor
102 | Log.d(TAG, "bitmapCompressor.onPostExecute: ");
103 | image.setImageBitmap(bitmaps[0]); // sets the compressed bitmap on the ImageView
104 | }
105 | }.execute(bitmap); // Here we pass the bitmap
106 | }
107 | };
108 | private CantFindCameraAppErrorListener cameraAppErrorListener = new CantFindCameraAppErrorListener() {
109 | @Override
110 | public void cantFindCameraApp() {
111 | Log.e(TAG, "cantFindCameraApp: ");
112 | Toast.makeText(MainActivity.this, "Can't find the camera app", Toast.LENGTH_SHORT).show();
113 | }
114 | };
115 | private ErrorCreatingTempFileForCameraListener fileForCameraListener = new ErrorCreatingTempFileForCameraListener() {
116 | @Override
117 | public void errorCreatingTempFileForCamera() {
118 | Log.e(TAG, "errorCreatingTempFileForCamera: ");
119 | Toast.makeText(MainActivity.this, "Error starting camera", Toast.LENGTH_SHORT).show();
120 | }
121 | };
122 | }
123 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/github/brunodles/picpicker/PicPicker.java:
--------------------------------------------------------------------------------
1 | package com.github.brunodles.picpicker;
2 |
3 | import android.Manifest;
4 | import android.annotation.SuppressLint;
5 | import android.app.Activity;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.pm.PackageManager;
9 | import android.graphics.Bitmap;
10 | import android.net.Uri;
11 | import android.os.AsyncTask;
12 | import android.os.Environment;
13 | import android.provider.MediaStore;
14 | import android.support.v4.content.ContextCompat;
15 | import android.util.Log;
16 |
17 | import com.github.brunodles.picpicker.listener.ActivityStarter;
18 | import com.github.brunodles.picpicker.listener.CantFindCameraAppErrorListener;
19 | import com.github.brunodles.picpicker.listener.ErrorCreatingTempFileForCameraListener;
20 | import com.github.brunodles.picpicker.listener.NeedWritePermissionErrorListener;
21 | import com.github.brunodles.picpicker.listener.PicResultListener;
22 |
23 | import java.io.File;
24 | import java.io.IOException;
25 | import java.text.SimpleDateFormat;
26 | import java.util.Date;
27 |
28 |
29 | /**
30 | * This class will be the main interface to request a picture.
31 | * You can do this by calling {@link #gallery()} or {@link #camera()}.
32 | * This class uses a `Method chaining` pattern to set the parameters.
33 | * Created by brunodles on 10/11/14.
34 | */
35 | public final class PicPicker {
36 |
37 | private static final String TAG = "PicPicker";
38 |
39 | /**
40 | * You can change this variable to change the request code used to open gallery
41 | */
42 | public static int REQUEST_CODE_ATTACH_IMAGE = 9123;
43 | /**
44 | * You can change the variable to change the request code used to open camera
45 | */
46 | public static int REQUEST_CODE_TAKE_PICURE = 9124;
47 |
48 | private Uri fileUri;
49 | private final Context context;
50 | private final ActivityStarter activityStarter;
51 | private final PicResultListener listener;
52 |
53 | private CantFindCameraAppErrorListener cameraAppErrorListener;
54 | private NeedWritePermissionErrorListener permissionErrorListener;
55 | private ErrorCreatingTempFileForCameraListener fileForCameraListener;
56 |
57 | /**
58 | * @param context a Context
59 | * @param activityStarter an activity starter, can be an {@link Activity} or an
60 | * {@link android.support.v4.app.Fragment}
61 | * @param listener A interface able to receive results.All images captured by this class will be passed to this listener on
62 | * {@link PicResultListener#onPictureResult(Bitmap)}.
63 | */
64 | public PicPicker(Context context, ActivityStarter activityStarter, PicResultListener listener) {
65 | this.context = context;
66 | this.activityStarter = activityStarter;
67 | this.listener = listener;
68 | }
69 |
70 | /**
71 | * @param activity an activity to serve as {@link Context} and as a {@link ActivityStarter}
72 | * @param listener A interface able to receive results.All images captured by this class will be passed to this listener on
73 | * {@link PicResultListener#onPictureResult(Bitmap)}.
74 | */
75 | public PicPicker(Activity activity, PicResultListener listener) {
76 | this.context = activity;
77 | this.listener = listener;
78 | this.activityStarter = new Activity_ActivityStarter(activity);
79 | }
80 |
81 | /**
82 | * A method to set a listener for camera app error.
83 | * When the lib can't find the camera app, the error will be sent thought this interface.
84 | *
85 | * @param cameraAppErrorListener
86 | */
87 | public PicPicker setCameraAppErrorListener(CantFindCameraAppErrorListener cameraAppErrorListener) {
88 | this.cameraAppErrorListener = cameraAppErrorListener;
89 | return this;
90 | }
91 |
92 | /**
93 | * A method to set a listener for permission error.
94 | * When user is using Android Marshmallow he will need to authorize the app to use the external
95 | * storage to write a temp file.
96 | *
97 | * @param permissionErrorListener This is a interface that will be called when we need to
98 | * request camera permission.
99 | */
100 | public PicPicker setPermissionErrorListener(NeedWritePermissionErrorListener permissionErrorListener) {
101 | this.permissionErrorListener = permissionErrorListener;
102 | return this;
103 | }
104 |
105 | /**
106 | * A method to set a listener for temp file error.
107 | * To use camera, the app will need to create a temp file.
108 | * But if it fail you can know it just implementing this interface.
109 | *
110 | * @param fileForCameraListener This is a interface that will be called when an error was throw
111 | * when we try to create a temp file.
112 | */
113 | public PicPicker setFileForCameraListener(ErrorCreatingTempFileForCameraListener fileForCameraListener) {
114 | this.fileForCameraListener = fileForCameraListener;
115 | return this;
116 | }
117 |
118 | /**
119 | * This method will start the gallery.
120 | * The result will be passed to the result listener.
121 | */
122 | public void gallery() {
123 | Intent intent = new Intent();
124 | intent.setType("image/*");
125 | intent.setAction(Intent.ACTION_GET_CONTENT);
126 | intent.addCategory(Intent.CATEGORY_OPENABLE);
127 | startActivityForResult(intent, REQUEST_CODE_ATTACH_IMAGE);
128 | }
129 |
130 | private void startActivityForResult(Intent intent, int requestCodeAttachImage) {
131 | activityStarter.startActivityForResult(intent, requestCodeAttachImage);
132 | }
133 |
134 | /**
135 | * This method will start the camera.
136 | * The result will be passed to the result listener.
137 | */
138 | public void camera() {
139 | int permissionCheck = ContextCompat.checkSelfPermission(context,
140 | Manifest.permission.WRITE_EXTERNAL_STORAGE);
141 | if (permissionCheck == PackageManager.PERMISSION_GRANTED)
142 | startCameraIntent();
143 | else
144 | needWritePermission();
145 | }
146 |
147 | private void needWritePermission() {
148 | if (permissionErrorListener == null)
149 | Log.e(TAG, "Hey dev, you need to call `setPermissionErrorListener` on PicPicker, to manage permission error.");
150 | else
151 | permissionErrorListener.needWritePermission();
152 | }
153 |
154 | private void startCameraIntent() {
155 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
156 |
157 | // Ensure that there's a camera activity to handle the intent
158 | if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
159 | // Create the File where the photo should go
160 | File photoFile = null;
161 | try {
162 | photoFile = createImageFile();
163 | } catch (Exception ex) {
164 | // Error occurred while creating the File
165 | errorCreatingTempFileForCamera(ex);
166 | }
167 | // Continue only if the File was successfully created
168 | fileUri = Uri.fromFile(photoFile);
169 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
170 | startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PICURE);
171 | } else {
172 | cantFindCameraApp();
173 | }
174 | }
175 |
176 | private void errorCreatingTempFileForCamera(Exception ex) {
177 | if (fileForCameraListener == null)
178 | Log.e(TAG, "errorCreatingTempFileForCamera: ", ex);
179 | else
180 | fileForCameraListener.errorCreatingTempFileForCamera();
181 | }
182 |
183 | private void cantFindCameraApp() {
184 | if (cameraAppErrorListener == null)
185 | Log.e(TAG, "Hey dev, you need to call `setCameraAppErrorListener` on PicPicker, to manage this error. Aparently this device don't have camera, so, there's no app to open.");
186 | else
187 | cameraAppErrorListener.cantFindCameraApp();
188 | }
189 |
190 | private PackageManager getPackageManager() {
191 | return context.getPackageManager();
192 | }
193 |
194 | @SuppressLint("SimpleDateFormat")
195 | private File createImageFile() throws IOException {
196 | // Create an image file name
197 | String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
198 | String imageFileName = "JPEG_" + timeStamp + "_";
199 | File storageDir = Environment.getExternalStoragePublicDirectory(
200 | Environment.DIRECTORY_PICTURES);
201 | return File.createTempFile(
202 | imageFileName, /* prefix */
203 | ".jpg", /* suffix */
204 | storageDir /* directory */
205 | );
206 | }
207 |
208 | /**
209 | * To let the lib know when the user choose a image you will need to call this method from an
210 | * {@link Activity#onActivityResult(int, int, Intent)} or from a
211 | * {@link android.support.v4.app.Fragment#onActivityResult(int, int, Intent)}
212 | *
213 | * @return if the result is false call the default implementation. A true result means that the
214 | * lib made something.
215 | */
216 | public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
217 | if (resultCode == Activity.RESULT_OK) {
218 | Uri uri = getUri(requestCode, data);
219 | if (uri != null) {
220 | executeImageAsyncTask(uri);
221 | return true;
222 | }
223 | }
224 | return false;
225 | }
226 |
227 | private Uri getUri(int requestCode, Intent data) {
228 | if (requestCode == REQUEST_CODE_ATTACH_IMAGE) return data.getData();
229 | else if (requestCode == REQUEST_CODE_TAKE_PICURE) return fileUri;
230 | else return null;
231 | }
232 |
233 | private void executeImageAsyncTask(Uri uri) {
234 | new AddImageAsyncTask(context, uri, listener)
235 | .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
236 | }
237 | }
--------------------------------------------------------------------------------