├── .gitignore
├── LICENSE.md
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── net
│ │ └── alhazmy13
│ │ └── mediapickerexample
│ │ ├── ImageFragment.java
│ │ ├── MainActivity.java
│ │ └── VideoFragment.java
│ └── res
│ ├── layout
│ ├── activity_main.xml
│ ├── image_layout.xml
│ └── video_layout.xml
│ ├── 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-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── comman
├── .gitignore
└── build.gradle
├── deps.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── libary
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── net
│ │ └── alhazmy13
│ │ └── mediapicker
│ │ ├── FileProcessing.java
│ │ ├── Image
│ │ ├── ImageActivity.java
│ │ ├── ImageConfig.java
│ │ ├── ImagePicker.java
│ │ ├── ImagePickerBuilderBase.java
│ │ ├── ImageProcessing.java
│ │ └── ImageTags.java
│ │ ├── Utility.java
│ │ └── Video
│ │ ├── VideoActivity.java
│ │ ├── VideoConfig.java
│ │ ├── VideoPicker.java
│ │ ├── VideoPickerBuilderBase.java
│ │ ├── VideoProcessing.java
│ │ └── VideoTags.java
│ └── res
│ ├── values-ar
│ └── string.xml
│ ├── values
│ ├── strings.xml
│ └── style.xml
│ └── xml
│ └── provider_paths.xml
├── license
├── rxjava
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── net
│ │ └── alhazmy13
│ │ └── mediapicker
│ │ └── rxjava
│ │ ├── image
│ │ ├── ImagePickerHelper.java
│ │ ├── ImagePickerObservable.java
│ │ └── ImagePickerReceiver.java
│ │ └── video
│ │ ├── VideoPickerHelper.java
│ │ ├── VideoPickerObservable.java
│ │ └── VideoPickerReceiver.java
│ └── res
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Windows thumbnail db
19 | Thumbs.db
20 |
21 | # OSX files
22 | .DS_Store
23 |
24 | # Android Studio
25 | *.iml
26 | .idea
27 | #.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
28 | .gradle
29 | build/
30 | .navigation
31 | captures/
32 | output.json #Since Android Studio 3.0
33 |
34 | #NDK
35 | obj/
36 | .externalNativeBuild
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Abdullah Alhazmy
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | # Media Picker
6 | 
7 | 
8 | 
9 | [](https://www.codacy.com/app/me_101/MediaPicker?utm_source=github.com&utm_medium=referral&utm_content=alhazmy13/MediaPicker&utm_campaign=badger)
10 |
11 |
12 | **[Please let me know if your application go to production via this link](https://docs.google.com/forms/d/e/1FAIpQLSe4Y5Fwn1mlEoD4RxjXQzTvL4mofhESuBlTkAPQhI7J_WqMDQ/viewform?c=0&w=1)**
13 | ------
14 | Media Picker is an Android Libary that lets you to select multiple images, video or voice for Android 4.1 (API 16) +.
15 | You can report any issue on issues page. **Note: If you speak Arabic, you can submit issues with Arabic language and I will check them. :)**
16 |
17 | # NOTE
18 | ----
19 | This build `2.x.x` will break backward compatibility and there are a lot of changes to improve the performance and fix a lot of Leak memory issues, So please read below document carefully.
20 | ## Installation
21 | ------
22 | **Maven**
23 |
24 | ```xml
25 |
26 | net.alhazmy13.MediaPicker
27 | libary
28 | 2.4.4
29 |
30 | ```
31 |
32 |
33 | **Gradle**
34 |
35 | ```gradle
36 | dependencies {
37 | implementation 'net.alhazmy13.MediaPicker:libary:2.4.4'
38 | }
39 | ```
40 |
41 | # Usage
42 | ------
43 | ## Images
44 | After adding the library, you need to:
45 |
46 | 1. Create an object from `ImagePicker` or `VideoPicker`
47 | 2. Override `onActivityResult` to receive the path of image or videos.
48 |
49 |
50 |
51 | ### Create an `ImagePicker`
52 | You will need to create a new instance of `ImagePicker`. Once the instance are configured, you can call `build()`.
53 |
54 | ```java
55 | new ImagePicker.Builder(MainActivity.this)
56 | .mode(ImagePicker.Mode.CAMERA_AND_GALLERY)
57 | .compressLevel(ImagePicker.ComperesLevel.MEDIUM)
58 | .directory(ImagePicker.Directory.DEFAULT)
59 | .extension(ImagePicker.Extension.PNG)
60 | .scale(600, 600)
61 | .allowMultipleImages(false)
62 | .enableDebuggingMode(true)
63 | .build();
64 | ```
65 | ### Override `onActivityResult `
66 | In order to receive the path of image, you will need to override `onActivityResult ` .
67 |
68 | ```java
69 | @Override
70 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
71 | super.onActivityResult(requestCode, resultCode, data);
72 |
73 | if (requestCode == ImagePicker.IMAGE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
74 | List mPaths = data.getStringArrayListExtra(ImagePicker.EXTRA_IMAGE_PATH);
75 | //Your Code
76 | }
77 | }
78 | ```
79 |
80 | ### Additional Options
81 | * `mode` to select the mode, you can choose one of these `CAMERA`,`GALLERY` or `CAMERA_AND_GALLERY`
82 |
83 | ```java
84 | .mode(ImagePicker.Mode.CAMERA)
85 | ```
86 |
87 | * `extension` You can change the extension of image to `PNG` or `JPG`
88 |
89 | ```java
90 | .extension(ImagePicker.Extension.PNG)
91 | ```
92 | * `compressLevel` You can change the quality of image with three different levels `HARD`,`MEDIUM`, `SOFT` or `NONE`
93 |
94 | ```java
95 | .compressLevel(ImagePicker.ComperesLevel.MEDIUM)
96 | ```
97 |
98 | * `directory` You can pass the storage path, or select `Directory.DEFAULT_DIR` to keep the default path.
99 |
100 | ```java
101 | .directory(ImagePicker.Directory.DEFAULT)
102 |
103 | //OR
104 |
105 | .directory(Environment.getExternalStorageDirectory()+"/myFolder")
106 |
107 | ```
108 |
109 | * `scale` You can scale the image to a a minimum width and height. This will only be used if compressLevel is set. To avoid OutOfMemory issues, ensure this is used.
110 |
111 | ```java
112 | .scale(500, 500)
113 | ```
114 | * `allowMultipleImages` Extra used to select and return multiple images from gallery **CANNOT select single image from gallery if this feature was enabled**
115 |
116 | ```java
117 | .allowMultipleImages(true)
118 | ```
119 |
120 | * `enableDebuggingMode` used to print Image Picker Log
121 |
122 | ```java
123 | .enableDebuggingMode(true)
124 | ```
125 |
126 | * `allowOnlineImages` an option to allow the user to select any image from online resource ex: Google Drive **(KNOWN ISSUE) if you enable this option then you cannot select multiple images**
127 |
128 | ```java
129 | .allowOnlineImages(true)
130 | ```
131 |
132 |
133 | ### Create an `VideoPicker`
134 | You will need to create a new instance of `VideoPicker`. Once the instance are configured, you can call `build()`.
135 |
136 | ```java
137 | new VideoPicker.Builder(MainActivity.this)
138 | .mode(VideoPicker.Mode.CAMERA_AND_GALLERY)
139 | .directory(VideoPicker.Directory.DEFAULT)
140 | .extension(VideoPicker.Extension.MP4)
141 | .enableDebuggingMode(true)
142 | .build();
143 | ```
144 | ### Override `onActivityResult `
145 | In order to receive the path of videos, you will need to override `onActivityResult ` .
146 |
147 | ```java
148 | @Override
149 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
150 | super.onActivityResult(requestCode, resultCode, data);
151 |
152 | if (requestCode == VideoPicker.VIDEO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
153 | List mPaths = data.getStringArrayListExtra(VideoPicker.EXTRA_VIDEO_PATH);
154 | //Your Code
155 | }
156 | }
157 | ```
158 |
159 | ### Additional Options
160 | * `mode` to select the mode, you can choose one of these `CAMERA`,`GALLERY` or `CAMERA_AND_GALLERY`
161 |
162 | ```java
163 | .mode(VideoPicker.Mode.CAMERA)
164 | ```
165 |
166 | * `extension` You can change the extension of video to `MP4`
167 |
168 | ```java
169 | .extension(VideoPicker.Extension.MP4)
170 | ```
171 |
172 | * `directory` You can pass the storage path, or select `Directory.DEFAULT_DIR` to keep the default path.
173 |
174 | ```java
175 | .directory(VideoPicker.Directory.DEFAULT)
176 |
177 | //OR
178 |
179 | .directory(Environment.getExternalStorageDirectory()+"/myFolder")
180 |
181 | ```
182 |
183 | * `enableDebuggingMode` used to print Video Picker Log
184 |
185 | ```java
186 | .enableDebuggingMode(true)
187 | ```
188 |
189 | ### RxJava 2 for MediaPicker
190 |
191 | It's an extenstion that allow you to return an observable from `ImagePickerBuilder` or `VideoPickerBuilder`, all you need is to add below dependency and then return the observable from `ImagePickerHelper` || `VideoPickerHelper` class.
192 |
193 |
194 | **Gradle**
195 |
196 | ```gradle
197 | dependencies {
198 | implementation 'io.reactivex.rxjava2:rxandroid:(Last_version)'
199 | implementation 'io.reactivex.rxjava2:rxjava:(Last_version)'
200 | implementation 'net.alhazmy13.MediaPicker:rxjava:(Last_version)'
201 | }
202 | ```
203 |
204 | ```Java
205 | new ImagePickerHelper(
206 | new ImagePicker.Builder(Context)
207 | ...)
208 | .getObservable()
209 | .subscribe(....);
210 | ```
211 |
212 | ------
213 |
214 |
215 | ## Theme the pickers
216 |
217 | You can change the strings be overwriting below resources in your project.
218 |
219 | ```xml
220 |
221 | Select From:
222 | Camera
223 | Gallery
224 | Ok
225 | Cancel
226 | Some Permission is Denied
227 | You need to grant access to
228 |
229 | ```
230 |
231 |
232 |
233 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
3 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | //apply from: '../deps.gradle'
3 |
4 | android {
5 | compileSdkVersion 28
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 28
10 | applicationId 'net.alhazmy13.mediapickerexample'
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 |
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 |
22 | lintOptions {
23 | abortOnError false
24 | }
25 | }
26 |
27 | dependencies {
28 | implementation "com.android.support:appcompat-v7:28.0.0"
29 | implementation "com.android.support:design:28.0.0"
30 | implementation "com.android.support:support-v13:28.0.0"
31 | implementation project(':libary')
32 | implementation project(':rxjava')
33 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
34 | implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
35 | }
36 |
--------------------------------------------------------------------------------
/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 /Users/Alhazmy13/Library/Android/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 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
14 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapickerexample;
2 |
3 | import android.content.Intent;
4 | import android.graphics.BitmapFactory;
5 | import android.os.Bundle;
6 | import android.support.annotation.NonNull;
7 | import android.support.v4.app.Fragment;
8 | import android.util.Log;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.ImageView;
13 | import android.widget.TextView;
14 |
15 | import net.alhazmy13.mediapicker.Image.ImagePicker;
16 |
17 | import java.util.List;
18 |
19 | import static android.app.Activity.RESULT_OK;
20 |
21 |
22 | /**
23 | * Created by alhazmy13 on 3/13/17.
24 | */
25 |
26 | public class ImageFragment extends Fragment {
27 | private ImageView imageView;
28 | private TextView path;
29 |
30 | private static final String TAG = "MainActivity";
31 | private List mPath;
32 |
33 |
34 | @Override
35 | public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
36 | Bundle savedInstanceState) {
37 | View view = inflater.inflate(R.layout.image_layout, container, false);
38 |
39 | // Find our View instances
40 | imageView = view.findViewById(R.id.iv_image);
41 | path = view.findViewById(R.id.tv_path);
42 | view.findViewById(R.id.bt_pick).setOnClickListener(new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 | pickImage();
46 | }
47 | });
48 | return view;
49 | }
50 |
51 |
52 | private void pickImage() {
53 | new ImagePicker.Builder(getActivity())
54 | .mode(ImagePicker.Mode.CAMERA_AND_GALLERY)
55 | .allowMultipleImages(true)
56 | .compressLevel(ImagePicker.ComperesLevel.MEDIUM)
57 | .directory(ImagePicker.Directory.DEFAULT)
58 | .extension(ImagePicker.Extension.PNG)
59 | .allowOnlineImages(true)
60 | .scale(600, 600)
61 | .allowMultipleImages(true)
62 | .enableDebuggingMode(true)
63 | .build();
64 | }
65 |
66 | @Override
67 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
68 | super.onActivityResult(requestCode, resultCode, data);
69 | Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
70 | if (requestCode == ImagePicker.IMAGE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
71 | mPath = data.getStringArrayListExtra(ImagePicker.EXTRA_IMAGE_PATH);
72 | Log.d(TAG, "onActivityResult: ");
73 | loadImage();
74 | }
75 | }
76 |
77 | private void loadImage() {
78 | Log.d(TAG, "loadImage: " + mPath.size());
79 | if (mPath != null && mPath.size() > 0) {
80 | path.setText(mPath.get(0));
81 | imageView.setImageBitmap(BitmapFactory.decodeFile(mPath.get(0)));
82 | }
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/net/alhazmy13/mediapickerexample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapickerexample;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.VisibleForTesting;
6 | import android.support.design.widget.TabLayout;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v4.app.FragmentManager;
9 | import android.support.v4.app.FragmentStatePagerAdapter;
10 | import android.support.v4.view.PagerAdapter;
11 | import android.support.v4.view.ViewPager;
12 | import android.support.v7.app.AppCompatActivity;
13 |
14 | public class MainActivity extends AppCompatActivity {
15 | private Fragment videoFragment = new VideoFragment();
16 | private Fragment imageFragment = new ImageFragment();
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 |
23 | ViewPager mPager = findViewById(R.id.pager);
24 | PagerAdapter mPagerAdapter = new PickerAdapter(getSupportFragmentManager());
25 | mPager.setAdapter(mPagerAdapter);
26 | TabLayout tabLayout = findViewById(R.id.tabs);
27 | tabLayout.setupWithViewPager(mPager);
28 | for (int i = 0; i < mPagerAdapter.getCount(); i++)
29 | tabLayout.getTabAt(i).setText(mPagerAdapter.getPageTitle(i));
30 | }
31 |
32 | private class PickerAdapter extends FragmentStatePagerAdapter {
33 | private static final int NUM_PAGES = 2;
34 |
35 |
36 | PickerAdapter(FragmentManager fm) {
37 | super(fm);
38 | }
39 |
40 | @Override
41 | public int getCount() {
42 | return NUM_PAGES;
43 | }
44 |
45 | @VisibleForTesting
46 | @Override
47 | public Fragment getItem(int position) {
48 | switch (position) {
49 | case 0:
50 | return videoFragment;
51 | default:
52 | return imageFragment;
53 | }
54 | }
55 |
56 | @VisibleForTesting
57 | public String getPageTitle(int position) {
58 | switch (position) {
59 | case 0:
60 | return getString(R.string.tab_title_video);
61 | default:
62 | return getString(R.string.tab_title_image);
63 | }
64 | }
65 | }
66 |
67 | @Override
68 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
69 | super.onActivityResult(requestCode, resultCode, data);
70 | //
71 | videoFragment.onActivityResult(requestCode, resultCode, data);
72 | imageFragment.onActivityResult(requestCode, resultCode, data);
73 | }
74 | }
--------------------------------------------------------------------------------
/app/src/main/java/net/alhazmy13/mediapickerexample/VideoFragment.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapickerexample;
2 |
3 | import android.content.Intent;
4 | import android.net.Uri;
5 | import android.os.Bundle;
6 | import android.support.annotation.NonNull;
7 | import android.support.v4.app.Fragment;
8 | import android.util.Log;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.MediaController;
13 | import android.widget.TextView;
14 | import android.widget.VideoView;
15 |
16 | import net.alhazmy13.mediapicker.Video.VideoPicker;
17 |
18 | import java.util.List;
19 |
20 | import static android.app.Activity.RESULT_OK;
21 |
22 | /**
23 | * Created by alhazmy13 on 3/13/17.
24 | */
25 |
26 | public class VideoFragment extends Fragment {
27 | private VideoView videoView;
28 | private TextView path;
29 |
30 | private static final String TAG = "MainActivity";
31 | private List mPath;
32 |
33 |
34 | @Override
35 | public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
36 | Bundle savedInstanceState) {
37 | View view = inflater.inflate(R.layout.video_layout, container, false);
38 | init(view);
39 |
40 | return view;
41 | }
42 |
43 | private void init(View view) {
44 | // Find our View instances
45 | videoView = view.findViewById(R.id.iv_video);
46 | MediaController mediaController = new MediaController(getActivity());
47 | mediaController.setAnchorView(videoView);
48 | videoView.setMediaController(mediaController);
49 | path = view.findViewById(R.id.tv_path);
50 | view.findViewById(R.id.bt_pick).setOnClickListener(new View.OnClickListener() {
51 | @Override
52 | public void onClick(View v) {
53 | pickVideo();
54 | }
55 | });
56 | }
57 |
58 |
59 | private void pickVideo() {
60 | new VideoPicker.Builder(getActivity())
61 | .mode(VideoPicker.Mode.CAMERA_AND_GALLERY)
62 | .directory(VideoPicker.Directory.DEFAULT)
63 | .extension(VideoPicker.Extension.MP4)
64 | .enableDebuggingMode(true)
65 | .build();
66 | }
67 |
68 | //
69 | @Override
70 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
71 | super.onActivityResult(requestCode, resultCode, data);
72 | Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
73 | if (requestCode == VideoPicker.VIDEO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
74 | mPath = data.getStringArrayListExtra(VideoPicker.EXTRA_VIDEO_PATH);
75 | loadVideo();
76 | }
77 | }
78 |
79 | private void loadVideo() {
80 | if (mPath != null && mPath.size() > 0) {
81 | path.setText(mPath.get(0));
82 | videoView.setVideoURI(Uri.parse(mPath.get(0)));
83 | videoView.start();
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
12 |
13 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/image_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
13 |
17 |
18 |
23 |
24 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/video_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
13 |
17 |
18 |
23 |
24 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alhazmy13/MediaPicker/7f8a5150943b5fc45d6ed37327cbec3d2191ef56/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alhazmy13/MediaPicker/7f8a5150943b5fc45d6ed37327cbec3d2191ef56/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alhazmy13/MediaPicker/7f8a5150943b5fc45d6ed37327cbec3d2191ef56/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alhazmy13/MediaPicker/7f8a5150943b5fc45d6ed37327cbec3d2191ef56/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alhazmy13/MediaPicker/7f8a5150943b5fc45d6ed37327cbec3d2191ef56/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MediaPicker
3 | Video
4 | Image
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | buildscript {
3 | repositories {
4 | google()
5 | jcenter()
6 | maven {
7 | url 'https://maven.google.com/'
8 | name 'Google'
9 | }
10 |
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:3.3.2'
14 | classpath 'com.novoda:bintray-release:0.8.0'
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | jcenter()
21 | maven {
22 | url 'https://maven.google.com/'
23 | name 'Google'
24 | }
25 | }
26 | }
27 |
28 | task clean(type: Delete) {
29 | delete rootProject.buildDir
30 | }
31 |
--------------------------------------------------------------------------------
/comman/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/comman/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | target
4 | dependencies {
5 | compile fileTree(dir: 'libs', include: ['*.jar'])
6 | }
--------------------------------------------------------------------------------
/deps.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 | versions = [
3 | support : '25.2.0',
4 | retrofit : '2.2.0',
5 | BUILD_TOOLS_VERSION: '25.0.2',
6 | rxJava : '2.0.1',
7 | rxAndroid : '2.0.1',
8 | MIN_SDK_VERSION : 14,
9 | TARGET_SDK_VERSION : 25,
10 | LIBRARY_VERSION : '2.3.2'
11 | ]
12 | supportDeps = [
13 | appcompatV7: "com.android.support:appcompat-v7:$versions.support",
14 | design : "com.android.support:design:$versions.support",
15 | support : "com.android.support:support-v13:$versions.support"
16 | ]
17 | rxJava = [
18 | rxJava : "io.reactivex.rxjava2:rxjava:$versions.rxJava",
19 | rxAndroid: "io.reactivex.rxjava2:rxandroid:$versions.rxAndroid"
20 | ]
21 |
22 |
23 | supportLibs = supportDeps.values()
24 | rxJavaLibs = rxJava.values()
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Sat Sep 10 13:03:48 EAT 2016
16 | org.gradle.jvmargs=-Xmx1536m
17 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alhazmy13/MediaPicker/7f8a5150943b5fc45d6ed37327cbec3d2191ef56/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Apr 17 04:20:44 AST 2018
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-4.10.1-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 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/libary/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
3 |
4 |
--------------------------------------------------------------------------------
/libary/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'
3 |
4 | //./gradlew bintrayUpload
5 | publish {
6 | userOrg = 'alhazmy13'
7 | groupId = 'net.alhazmy13.MediaPicker'
8 | artifactId = 'libary'
9 | uploadName = 'MediaPicker'
10 | publishVersion = '2.4.4'
11 | desc = 'MediaPicker'
12 | dryRun = false
13 | website = 'https://github.com/alhzmy13/MediaPicker'
14 | }
15 |
16 | android {
17 | compileSdkVersion 28
18 |
19 | defaultConfig {
20 | minSdkVersion 14
21 | targetSdkVersion 28
22 | versionCode 1
23 | versionName "1.0"
24 | }
25 |
26 | lintOptions {
27 | abortOnError false
28 | }
29 |
30 | buildTypes {
31 | release {
32 | minifyEnabled false
33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34 | }
35 | }
36 | }
37 |
38 |
39 | dependencies {
40 | implementation 'com.android.support:appcompat-v7:27.1.0'
41 | }
42 |
--------------------------------------------------------------------------------
/libary/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 /Users/Alhazmy13/Library/Android/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 |
--------------------------------------------------------------------------------
/libary/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
12 |
15 |
16 |
20 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/FileProcessing.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.ContentUris;
6 | import android.content.Context;
7 | import android.database.Cursor;
8 | import android.net.Uri;
9 | import android.os.Build;
10 | import android.os.Environment;
11 | import android.provider.DocumentsContract;
12 | import android.provider.MediaStore;
13 | import android.text.TextUtils;
14 |
15 | import java.io.File;
16 | import java.io.FileInputStream;
17 | import java.io.FileNotFoundException;
18 | import java.io.FileOutputStream;
19 | import java.io.IOException;
20 | import java.io.InputStream;
21 | import java.io.OutputStream;
22 | import java.util.UUID;
23 |
24 | /**
25 | * Created by Alhazmy13 on 8/15/16.
26 | * MediaPicker
27 | */
28 | public class FileProcessing {
29 |
30 |
31 | /**
32 | * Get a file path from a Uri. This will get the the path for Storage Access
33 | * Framework Documents, as well as the _data field for the MediaStore and
34 | * other file-based ContentProviders.
35 | *
36 | * @param context The context.
37 | * @param uri The Uri to query.
38 | * @author paulburke
39 | */
40 | @TargetApi(Build.VERSION_CODES.KITKAT)
41 | public static String getPath(final Context context, final Uri uri) {
42 |
43 | final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
44 |
45 | // DocumentProvider
46 | if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
47 | String filePath = "";
48 |
49 | // ExternalStorageProvider
50 | if (isExternalStorageDocument(uri)) {
51 | final String docId = DocumentsContract.getDocumentId(uri);
52 | final String[] split = docId.split(":");
53 | final String type = split[0];
54 |
55 | if ("primary".equalsIgnoreCase(type)) {
56 | return Environment.getExternalStorageDirectory() + "/" + split[1];
57 | }
58 | else {
59 | if (Build.VERSION.SDK_INT > 20) {
60 | //getExternalMediaDirs() added in API 21
61 | File external[] = context.getExternalMediaDirs();
62 | if (external.length > 1) {
63 | filePath = external[1].getAbsolutePath();
64 | filePath = filePath.substring(0, filePath.indexOf("Android")) + split[1];
65 | }
66 | }else{
67 | filePath = "/storage/" + type + "/" + split[1];
68 | }
69 | return filePath;
70 | }
71 | }
72 | // DownloadsProvider
73 | else if (isDownloadsDocument(uri)) {
74 |
75 | final String id = DocumentsContract.getDocumentId(uri);
76 | if (!TextUtils.isEmpty(id)) {
77 | if (id.startsWith("raw:")) {
78 | return id.replaceFirst("raw:", "");
79 | }
80 | }
81 | final Uri contentUri = ContentUris.withAppendedId(
82 | Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
83 |
84 | return getDataColumn(context, contentUri, null, null);
85 | }
86 | // MediaProvider
87 | else if (isMediaDocument(uri)) {
88 | final String docId = DocumentsContract.getDocumentId(uri);
89 | final String[] split = docId.split(":");
90 | final String type = split[0];
91 |
92 | Uri contentUri = null;
93 | if ("image".equals(type)) {
94 | contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
95 | } else if ("video".equals(type)) {
96 | contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
97 | } else if ("audio".equals(type)) {
98 | contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
99 | }
100 |
101 | final String selection = "_id=?";
102 | final String[] selectionArgs = new String[]{
103 | split[1]
104 | };
105 |
106 | return getDataColumn(context, contentUri, selection, selectionArgs);
107 | }
108 | }
109 | // MediaStore (and general)
110 | else if ("content".equalsIgnoreCase(uri.getScheme())) {
111 | return getDataColumn(context, uri, null, null);
112 | }
113 | // File
114 | else if ("file".equalsIgnoreCase(uri.getScheme())) {
115 | return uri.getPath();
116 | }
117 |
118 | return "";
119 | }
120 |
121 | /**
122 | * Get the value of the data column for this Uri. This is useful for
123 | * MediaStore Uris, and other file-based ContentProviders.
124 | *
125 | * @param context The context.
126 | * @param uri The Uri to query.
127 | * @param selection (Optional) Filter used in the query.
128 | * @param selectionArgs (Optional) Selection arguments used in the query.
129 | * @return The value of the _data column, which is typically a file path.
130 | */
131 | private static String getDataColumn(Context context, Uri uri, String selection,
132 | String[] selectionArgs) {
133 |
134 | Cursor cursor = null;
135 | final String column = "_data";
136 | final String[] projection = {
137 | column
138 | };
139 |
140 | try {
141 | cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
142 | null);
143 | if (cursor != null && cursor.moveToFirst()) {
144 | final int column_index = cursor.getColumnIndexOrThrow(column);
145 | String data = cursor.getString(column_index);
146 | if(data != null){
147 | return data;
148 | }
149 | return downloadAndSaveImage(uri, context);
150 | }
151 | } finally {
152 | if (cursor != null)
153 | cursor.close();
154 | }
155 | return null;
156 | }
157 |
158 | private static String downloadAndSaveImage(Uri uri, Context context){
159 | InputStream inputStream = null;
160 | String filePath = null;
161 |
162 | if (uri.getAuthority() != null) {
163 | try {
164 | inputStream = context.getContentResolver().openInputStream(uri); // context needed
165 | File photoFile = createTemporalFileFrom(inputStream, context);
166 |
167 | filePath = photoFile.getPath();
168 |
169 | } catch (FileNotFoundException e) {
170 | // log
171 | } catch (IOException e) {
172 | // log
173 | }finally {
174 | try {
175 | inputStream.close();
176 | } catch (IOException e) {
177 | e.printStackTrace();
178 | }
179 | }
180 | }
181 |
182 | return filePath;
183 | }
184 |
185 | private static File createTemporalFileFrom(InputStream inputStream, Context context) throws IOException {
186 | File targetFile = null;
187 |
188 | if (inputStream != null) {
189 | int read;
190 | byte[] buffer = new byte[8 * 1024];
191 |
192 | targetFile = createTemporalFile(context);
193 | OutputStream outputStream = new FileOutputStream(targetFile);
194 |
195 | while ((read = inputStream.read(buffer)) != -1) {
196 | outputStream.write(buffer, 0, read);
197 | }
198 | outputStream.flush();
199 |
200 | try {
201 | outputStream.close();
202 | } catch (IOException e) {
203 | e.printStackTrace();
204 | }
205 | }
206 |
207 | return targetFile;
208 | }
209 |
210 |
211 | private static File createTemporalFile(Context context) {
212 | return new File(context.getExternalCacheDir(), String.format("%s.jpg", UUID.randomUUID())); // context needed
213 | }
214 |
215 | /**
216 | * @param uri The Uri to check.
217 | * @return Whether the Uri authority is ExternalStorageProvider.
218 | */
219 | private static boolean isExternalStorageDocument(Uri uri) {
220 | return "com.android.externalstorage.documents".equals(uri.getAuthority());
221 | }
222 |
223 | /**
224 | * @param uri The Uri to check.
225 | * @return Whether the Uri authority is DownloadsProvider.
226 | */
227 | private static boolean isDownloadsDocument(Uri uri) {
228 | return "com.android.providers.downloads.documents".equals(uri.getAuthority());
229 | }
230 |
231 | /**
232 | * @param uri The Uri to check.
233 | * @return Whether the Uri authority is MediaProvider.
234 | */
235 | private static boolean isMediaDocument(Uri uri) {
236 | return "com.android.providers.media.documents".equals(uri.getAuthority());
237 | }
238 |
239 | public static String getVideoPath(final Uri uri, final Activity activity) {
240 | String[] projection = {MediaStore.Images.Media.DATA};
241 | Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
242 | if (cursor != null) {
243 | int column_index = cursor
244 | .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
245 | cursor.moveToFirst();
246 | return cursor.getString(column_index);
247 | } else
248 | return null;
249 | }
250 |
251 | public static void copyDirectory(File sourceLocation , File targetLocation) {
252 | try {
253 | if (sourceLocation.isDirectory()) {
254 | if (!targetLocation.exists()) {
255 | targetLocation.mkdir();
256 | }
257 |
258 | String[] children = sourceLocation.list();
259 | for (String aChildren : children) {
260 | copyDirectory(new File(sourceLocation, aChildren),
261 | new File(targetLocation, aChildren));
262 | }
263 | } else {
264 |
265 | InputStream in = new FileInputStream(sourceLocation);
266 | OutputStream out = new FileOutputStream(targetLocation);
267 |
268 | // Copy the bits from instream to outstream
269 | byte[] buf = new byte[1024];
270 | int len;
271 | while ((len = in.read(buf)) > 0) {
272 | out.write(buf, 0, len);
273 | }
274 | in.close();
275 | out.close();
276 | }
277 | }catch (Exception ignored){}
278 | }
279 | }
280 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageActivity.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Image;
2 |
3 | import android.Manifest;
4 | import android.annotation.TargetApi;
5 | import android.app.AlertDialog;
6 | import android.content.ClipData;
7 | import android.content.Context;
8 | import android.content.DialogInterface;
9 | import android.content.Intent;
10 | import android.content.pm.PackageManager;
11 | import android.net.Uri;
12 | import android.os.AsyncTask;
13 | import android.os.Build;
14 | import android.os.Bundle;
15 | import android.provider.MediaStore;
16 | import android.support.annotation.NonNull;
17 | import android.support.v4.app.ActivityCompat;
18 | import android.support.v4.content.FileProvider;
19 | import android.support.v7.app.AppCompatActivity;
20 | import android.util.Log;
21 | import android.widget.Toast;
22 |
23 | import net.alhazmy13.mediapicker.FileProcessing;
24 | import net.alhazmy13.mediapicker.R;
25 | import net.alhazmy13.mediapicker.Utility;
26 |
27 | import java.io.BufferedInputStream;
28 | import java.io.BufferedOutputStream;
29 | import java.io.File;
30 | import java.io.FileNotFoundException;
31 | import java.io.FileOutputStream;
32 | import java.io.IOException;
33 | import java.io.InputStream;
34 | import java.io.Serializable;
35 | import java.lang.ref.WeakReference;
36 | import java.util.ArrayList;
37 | import java.util.HashMap;
38 | import java.util.List;
39 | import java.util.Map;
40 |
41 | import static android.content.DialogInterface.BUTTON_POSITIVE;
42 |
43 | /**
44 | * Created by Alhazmy13 on 10/26/15.
45 | * MediaPicker
46 | */
47 | public class ImageActivity extends AppCompatActivity {
48 |
49 | private File destination;
50 | private Uri mImageUri;
51 | private ImageConfig mImgConfig;
52 | private List listOfImgs;
53 | private AlertDialog alertDialog;
54 |
55 | public static Intent getCallingIntent(Context activity, ImageConfig imageConfig) {
56 | Intent intent = new Intent(activity, ImageActivity.class);
57 | intent.putExtra(ImageTags.Tags.IMG_CONFIG, imageConfig);
58 | return intent;
59 | }
60 |
61 | @Override
62 | protected void onCreate(Bundle savedInstanceState) {
63 | super.onCreate(savedInstanceState);
64 | Intent intent = getIntent();
65 | if (intent != null) {
66 | mImgConfig = (ImageConfig) intent.getSerializableExtra(ImageTags.Tags.IMG_CONFIG);
67 | }
68 |
69 | if (savedInstanceState == null) {
70 | pickImageWrapper();
71 | listOfImgs = new ArrayList<>();
72 | }
73 | if (mImgConfig.debug)
74 | Log.d(ImageTags.Tags.TAG, mImgConfig.toString());
75 | }
76 |
77 | @Override
78 | protected void onStop() {
79 | if (alertDialog != null)
80 | alertDialog.dismiss();
81 | super.onStop();
82 | }
83 |
84 | private void pickImage() {
85 | Utility.createFolder(mImgConfig.directory);
86 | destination = new File(mImgConfig.directory, Utility.getRandomString() + mImgConfig.extension.getValue());
87 | switch (mImgConfig.mode) {
88 | case CAMERA:
89 | startActivityFromCamera();
90 | break;
91 | case GALLERY:
92 | if (mImgConfig.allowMultiple && mImgConfig.allowOnlineImages)
93 | startActivityFromGalleryMultiImg();
94 | else
95 | startActivityFromGallery();
96 | break;
97 | case CAMERA_AND_GALLERY:
98 | showFromCameraOrGalleryAlert();
99 | break;
100 | default:
101 | break;
102 | }
103 | }
104 |
105 | private void showFromCameraOrGalleryAlert() {
106 | alertDialog = new AlertDialog.Builder(this)
107 | .setTitle(getString(R.string.media_picker_select_from))
108 | .setPositiveButton(getString(R.string.media_picker_camera), new DialogInterface.OnClickListener() {
109 | @Override
110 | public void onClick(DialogInterface dialogInterface, int i) {
111 | if (mImgConfig.debug)
112 | Log.d(ImageTags.Tags.TAG, "Alert Dialog - Start From Camera");
113 | startActivityFromCamera();
114 | alertDialog.dismiss();
115 | }
116 | })
117 | .setNegativeButton(getString(R.string.media_picker_gallery), new DialogInterface.OnClickListener() {
118 | @Override
119 | public void onClick(DialogInterface dialogInterface, int i) {
120 | if (mImgConfig.debug)
121 | Log.d(ImageTags.Tags.TAG, "Alert Dialog - Start From Gallery");
122 | if (mImgConfig.allowMultiple)
123 | startActivityFromGalleryMultiImg();
124 | else
125 | startActivityFromGallery();
126 | alertDialog.dismiss();
127 | }
128 | })
129 | .setOnCancelListener(new DialogInterface.OnCancelListener() {
130 | @Override
131 | public void onCancel(DialogInterface dialogInterface) {
132 | if (mImgConfig.debug)
133 | Log.d(ImageTags.Tags.TAG, "Alert Dialog - Canceled");
134 | alertDialog.dismiss();
135 | finish();
136 | }
137 | })
138 | .create();
139 | if (alertDialog != null)
140 | alertDialog.show();
141 |
142 | }
143 |
144 | @Override
145 | public void onBackPressed() {
146 | setResult(RESULT_CANCELED);
147 | super.onBackPressed();
148 | }
149 |
150 | private void startActivityFromGallery() {
151 | mImgConfig.isImgFromCamera = false;
152 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
153 | photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, !mImgConfig.allowOnlineImages);
154 | photoPickerIntent.setType("image/*");
155 | startActivityForResult(photoPickerIntent, ImageTags.IntentCode.REQUEST_CODE_SELECT_PHOTO);
156 | if (mImgConfig.debug)
157 | Log.d(ImageTags.Tags.TAG, "Gallery Start with Single Image mode");
158 | }
159 |
160 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
161 | private void startActivityFromGalleryMultiImg() {
162 | mImgConfig.isImgFromCamera = false;
163 | Intent photoPickerIntent = new Intent();
164 | photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, !mImgConfig.allowOnlineImages);
165 | photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
166 | photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
167 | photoPickerIntent.setType("image/*");
168 | startActivityForResult(Intent.createChooser(photoPickerIntent, "Select Picture"), ImageTags.IntentCode.REQUEST_CODE_SELECT_MULTI_PHOTO);
169 | if (mImgConfig.debug)
170 | Log.d(ImageTags.Tags.TAG, "Gallery Start with Multiple Images mode");
171 | }
172 |
173 | private void startActivityFromCamera() {
174 | mImgConfig.isImgFromCamera = true;
175 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
176 | mImageUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", destination);
177 | intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
178 | startActivityForResult(Intent.createChooser(intent, "Select Picture"), ImageTags.IntentCode.CAMERA_REQUEST);
179 | if (mImgConfig.debug)
180 | Log.d(ImageTags.Tags.TAG, "Camera Start");
181 | }
182 |
183 | @Override
184 | protected void onSaveInstanceState(Bundle outState) {
185 | super.onSaveInstanceState(outState);
186 | if (mImageUri != null) {
187 | outState.putString(ImageTags.Tags.CAMERA_IMAGE_URI, mImageUri.toString());
188 | outState.putSerializable(ImageTags.Tags.IMG_CONFIG, mImgConfig);
189 | }
190 | outState.putBoolean(ImageTags.Tags.IS_ALERT_SHOWING, (alertDialog == null ? false : alertDialog.isShowing()));
191 | }
192 |
193 | @Override
194 | protected void onRestoreInstanceState(Bundle savedInstanceState) {
195 | super.onRestoreInstanceState(savedInstanceState);
196 | if (savedInstanceState.containsKey(ImageTags.Tags.CAMERA_IMAGE_URI)) {
197 | mImageUri = Uri.parse(savedInstanceState.getString(ImageTags.Tags.CAMERA_IMAGE_URI));
198 | destination = new File(mImageUri.getPath());
199 | mImgConfig = (ImageConfig) savedInstanceState.getSerializable(ImageTags.Tags.IMG_CONFIG);
200 | }
201 | if (savedInstanceState.getBoolean(ImageTags.Tags.IS_ALERT_SHOWING, false)) {
202 | if (alertDialog == null)
203 | pickImage();
204 | else
205 | alertDialog.show();
206 | }
207 | }
208 |
209 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
210 | @Override
211 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
212 | if (mImgConfig.debug)
213 | Log.d(ImageTags.Tags.TAG, "onActivityResult() called with: " + "requestCode = [" + requestCode + "]," +
214 | " resultCode = [" + resultCode + "], data = [" + data + "]");
215 | if (resultCode == RESULT_OK) {
216 | switch (requestCode) {
217 | case ImageTags.IntentCode.CAMERA_REQUEST:
218 | new CompressImageTask(destination.getAbsolutePath(), mImgConfig
219 | , ImageActivity.this).execute();
220 |
221 | break;
222 | case ImageTags.IntentCode.REQUEST_CODE_SELECT_PHOTO:
223 | processOneImage(data);
224 | break;
225 | case ImageTags.IntentCode.REQUEST_CODE_SELECT_MULTI_PHOTO:
226 | processMutliPhoto(data);
227 | break;
228 | default:
229 | break;
230 | }
231 | } else {
232 | Intent intent = new Intent();
233 | intent.setAction("net.alhazmy13.mediapicker.rxjava.image.service");
234 | intent.putExtra(ImageTags.Tags.PICK_ERROR, "user did not select any image");
235 | sendBroadcast(intent);
236 | finish();
237 | }
238 | }
239 |
240 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
241 | private void processMutliPhoto(Intent data) {
242 | //Check if the intent contain only one image
243 | if (data.getClipData() == null) {
244 | processOneImage(data);
245 | } else {
246 | //intent has multi images
247 | listOfImgs = ImageProcessing.processMultiImage(this, data);
248 | if (listOfImgs != null && listOfImgs.size() > 0) {
249 | new CompressImageTask(listOfImgs, mImgConfig, ImageActivity.this).execute();
250 | } else {
251 | //For 'Select pic from Google Photos - app Crash' fix
252 | String check = data.getClipData().toString();
253 | if (check != null && check.contains("com.google.android.apps.photos")) {
254 | ClipData clipdata = data.getClipData();
255 | for (int i = 0; i < clipdata.getItemCount(); i++) {
256 | Uri selectedImage = clipdata.getItemAt(i).getUri();
257 | String selectedImagePath = FileProcessing.getPath(ImageActivity.this, selectedImage);
258 | listOfImgs.add(selectedImagePath);
259 | }
260 | new CompressImageTask(listOfImgs, mImgConfig, ImageActivity.this).execute();
261 | }
262 | }
263 | }
264 | }
265 |
266 | public void processOneImage(Intent data) {
267 | try {
268 | Uri selectedImage = data.getData();
269 | if (selectedImage != null) {
270 | String rawPath = selectedImage.toString();
271 | //For 'Select pic from Google Drive - app Crash' fix
272 | if (rawPath.contains("com.google.android.apps.docs.storage")) {
273 | String fileTempPath = getCacheDir().getPath();
274 | new ImageActivity.SaveImageFromGoogleDriveTask(fileTempPath, mImgConfig, selectedImage, ImageActivity.this).execute();
275 | } else {
276 | String selectedImagePath = FileProcessing.getPath(this, selectedImage);
277 | new ImageActivity.CompressImageTask(selectedImagePath,
278 | mImgConfig, ImageActivity.this).execute();
279 | }
280 | }
281 | } catch (Exception ex) {
282 | ex.printStackTrace();
283 | }
284 |
285 | }
286 |
287 | private void finishActivity(List path) {
288 | Intent resultIntent = new Intent();
289 | resultIntent.putExtra(ImagePicker.EXTRA_IMAGE_PATH, (Serializable) path);
290 | setResult(RESULT_OK, resultIntent);
291 | finish();
292 | }
293 |
294 | private void pickImageWrapper() {
295 | if (Build.VERSION.SDK_INT >= 23) {
296 | List permissionsNeeded = new ArrayList<>();
297 |
298 | final List permissionsList = new ArrayList<>();
299 | if ((mImgConfig.mode == ImagePicker.Mode.CAMERA || mImgConfig.mode == ImagePicker.Mode.CAMERA_AND_GALLERY) && !addPermission(permissionsList, Manifest.permission.CAMERA))
300 | permissionsNeeded.add(getString(R.string.media_picker_camera));
301 | if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
302 | permissionsNeeded.add(getString(R.string.media_picker_read_Write_external_storage));
303 |
304 | if (permissionsList.size() > 0) {
305 | if (permissionsNeeded.size() > 0) {
306 | // Need Rationale
307 | StringBuilder message = new StringBuilder(getString(R.string.media_picker_you_need_to_grant_access_to) + " " + permissionsNeeded.get(0));
308 | for (int i = 1; i < permissionsNeeded.size(); i++)
309 | message.append(", ").append(permissionsNeeded.get(i));
310 | showMessageOKCancel(message.toString(),
311 | new DialogInterface.OnClickListener() {
312 | @Override
313 | public void onClick(DialogInterface dialog, int which) {
314 | switch (which) {
315 | case BUTTON_POSITIVE:
316 | ActivityCompat.requestPermissions(ImageActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
317 | ImageTags.IntentCode.REQUEST_CODE_ASK_PERMISSIONS);
318 | break;
319 |
320 | default:
321 | onBackPressed();
322 | break;
323 |
324 | }
325 |
326 |
327 | }
328 | });
329 | return;
330 | }
331 | ActivityCompat.requestPermissions(ImageActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
332 | ImageTags.IntentCode.REQUEST_CODE_ASK_PERMISSIONS);
333 | return;
334 | }
335 |
336 | pickImage();
337 | } else {
338 | pickImage();
339 | }
340 | }
341 |
342 | private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
343 | new AlertDialog.Builder(this)
344 | .setMessage(message)
345 | .setPositiveButton(getString(R.string.media_picker_ok), okListener)
346 | .setNegativeButton(getString(R.string.media_picker_cancel), okListener)
347 | .create()
348 | .show();
349 | }
350 |
351 | private boolean addPermission(List permissionsList, String permission) {
352 | if (ActivityCompat.checkSelfPermission(ImageActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
353 | permissionsList.add(permission);
354 | // Check for Rationale Option
355 | return ActivityCompat.shouldShowRequestPermissionRationale(ImageActivity.this, permission);
356 | }
357 | return true;
358 | }
359 |
360 | @Override
361 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
362 | switch (requestCode) {
363 | case ImageTags.IntentCode.REQUEST_CODE_ASK_PERMISSIONS:
364 | Map perms = new HashMap<>();
365 | // Initial
366 | perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
367 | perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
368 | // Fill with results
369 | for (int i = 0; i < permissions.length; i++)
370 | perms.put(permissions[i], grantResults[i]);
371 | // Check for ACCESS_FINE_LOCATION
372 | if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
373 | && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
374 | // All Permissions Granted
375 | pickImage();
376 | } else {
377 | // Permission Denied
378 | Toast.makeText(ImageActivity.this, getString(R.string.media_picker_some_permission_is_denied), Toast.LENGTH_SHORT)
379 | .show();
380 | onBackPressed();
381 | }
382 |
383 | break;
384 | default:
385 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
386 | }
387 | }
388 |
389 | private static class CompressImageTask extends AsyncTask {
390 |
391 | private final ImageConfig mImgConfig;
392 | private final List listOfImgs;
393 | private List destinationPaths;
394 | private WeakReference mContext;
395 |
396 |
397 | CompressImageTask(List listOfImgs, ImageConfig imageConfig, ImageActivity context) {
398 | this.listOfImgs = listOfImgs;
399 | this.mContext = new WeakReference<>(context);
400 | this.mImgConfig = imageConfig;
401 | this.destinationPaths = new ArrayList<>();
402 | }
403 |
404 | CompressImageTask(String absolutePath, ImageConfig imageConfig, ImageActivity context) {
405 | List list = new ArrayList<>();
406 | list.add(absolutePath);
407 | this.listOfImgs = list;
408 | this.mContext = new WeakReference<>(context);
409 | this.destinationPaths = new ArrayList<>();
410 | this.mImgConfig = imageConfig;
411 | }
412 |
413 |
414 | @Override
415 | protected Void doInBackground(Void... params) {
416 | for (String mPath : listOfImgs) {
417 | File file = new File(mPath);
418 | File destinationFile;
419 | if (mImgConfig.isImgFromCamera) {
420 | destinationFile = file;
421 | } else {
422 | destinationFile = new File(mImgConfig.directory, Utility.getRandomString() + mImgConfig.extension.getValue());
423 | }
424 | destinationPaths.add(destinationFile.getAbsolutePath());
425 | try {
426 | Utility.compressAndRotateIfNeeded(file, destinationFile, mImgConfig.compressLevel.getValue(), mImgConfig.reqWidth, mImgConfig.reqHeight);
427 | } catch (IOException e) {
428 | e.printStackTrace();
429 | }
430 |
431 | }
432 |
433 | return null;
434 | }
435 |
436 | @Override
437 | protected void onPostExecute(Void aVoid) {
438 | super.onPostExecute(aVoid);
439 |
440 | ImageActivity context = mContext.get();
441 | if (context != null) {
442 | context.finishActivity(destinationPaths);
443 | Intent intent = new Intent();
444 | intent.setAction("net.alhazmy13.mediapicker.rxjava.image.service");
445 | intent.putExtra(ImageTags.Tags.IMAGE_PATH, (Serializable) destinationPaths);
446 | context.sendBroadcast(intent);
447 | }
448 | }
449 | }
450 |
451 |
452 | private static class SaveImageFromGoogleDriveTask extends AsyncTask {
453 |
454 | private final ImageConfig mImgConfig;
455 | private final List listOfImgs;
456 | private List destinationPaths;
457 | private List destinationUris;
458 | private WeakReference mContext;
459 |
460 |
461 | SaveImageFromGoogleDriveTask(String absolutePath, ImageConfig imageConfig, Uri uri, ImageActivity context) {
462 | List list = new ArrayList<>();
463 | list.add(absolutePath);
464 | this.listOfImgs = list;
465 |
466 | List uris = new ArrayList<>();
467 | uris.add(uri);
468 | destinationUris = uris;
469 |
470 | this.mContext = new WeakReference<>(context);
471 | this.destinationPaths = new ArrayList<>();
472 | this.mImgConfig = imageConfig;
473 | }
474 |
475 |
476 | @Override
477 | protected Void doInBackground(Void... params) {
478 |
479 | for (int i = 0; i < listOfImgs.size(); i++) {
480 | String path = listOfImgs.get(i);
481 | Uri uriPath = destinationUris.get(i);
482 | try {
483 | String fileName = "drive_img_" + System.currentTimeMillis() + ".jpg";
484 | String fullImagePath = path + "/" + fileName;
485 | boolean isFileSaved = saveFile(uriPath, fullImagePath);
486 | if (isFileSaved) {
487 | destinationPaths.add(fullImagePath);
488 | }
489 | } catch (Exception e) {
490 | e.printStackTrace();
491 | }
492 | }
493 | return null;
494 | }
495 |
496 | @Override
497 | protected void onPostExecute(Void aVoid) {
498 | super.onPostExecute(aVoid);
499 | new CompressImageTask(destinationPaths, mImgConfig, mContext.get()).execute();
500 | }
501 |
502 | boolean filenotfoundexecption;
503 |
504 | //For Google Drive
505 | boolean saveFile(Uri sourceuri, String destination) throws IOException {
506 | filenotfoundexecption = false;
507 | int originalsize;
508 | InputStream input = null;
509 | try {
510 | input = mContext.get().getContentResolver().openInputStream(sourceuri);
511 | } catch (FileNotFoundException e) {
512 | e.printStackTrace();
513 | filenotfoundexecption = true;
514 | }
515 |
516 | try {
517 | originalsize = input.available();
518 | BufferedInputStream bis;
519 | BufferedOutputStream bos;
520 | try {
521 | bis = new BufferedInputStream(input);
522 | bos = new BufferedOutputStream(new FileOutputStream(destination, false));
523 | byte[] buf = new byte[originalsize];
524 | bis.read(buf);
525 | do {
526 | bos.write(buf);
527 | } while (bis.read(buf) != -1);
528 | } catch (IOException e) {
529 | filenotfoundexecption = true;
530 | return false;
531 | }
532 | } catch (NullPointerException e) {
533 | filenotfoundexecption = true;
534 | }
535 | return true;
536 | }
537 |
538 | }
539 | }
540 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageConfig.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Image;
2 |
3 | import android.os.Environment;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * Created by Alhazmy13 on 8/16/16.
9 | * MediaPicker
10 | */
11 | class ImageConfig implements Serializable {
12 |
13 | protected ImagePicker.Extension extension;
14 | protected ImagePicker.ComperesLevel compressLevel;
15 | protected ImagePicker.Mode mode;
16 | protected String directory;
17 | protected int reqHeight;
18 | protected int reqWidth;
19 | protected boolean allowMultiple;
20 | protected boolean isImgFromCamera;
21 | protected boolean allowOnlineImages;
22 | protected boolean debug;
23 |
24 | ImageConfig() {
25 | this.extension = ImagePicker.Extension.PNG;
26 | this.compressLevel = ImagePicker.ComperesLevel.NONE;
27 | this.mode = ImagePicker.Mode.CAMERA;
28 | this.directory = Environment.getExternalStorageDirectory() + ImageTags.Tags.IMAGE_PICKER_DIR;
29 | this.reqHeight = 0;
30 | this.reqWidth = 0;
31 | this.allowMultiple = false;
32 | this.allowOnlineImages = false;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "ImageConfig{" +
38 | "extension=" + extension +
39 | ", compressLevel=" + compressLevel +
40 | ", mode=" + mode +
41 | ", directory='" + directory + '\'' +
42 | ", reqHeight=" + reqHeight +
43 | ", reqWidth=" + reqWidth +
44 | ", allowMultiple=" + allowMultiple +
45 | ", isImgFromCamera=" + isImgFromCamera +
46 | ", allowOnlineImages="+ allowOnlineImages +
47 | ", debug=" + debug +
48 | '}';
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImagePicker.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Image;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.Intent;
6 | import android.os.Build;
7 | import android.os.Environment;
8 |
9 | import java.lang.ref.WeakReference;
10 |
11 | /**
12 | * Created by Alhazmy13 on 10/26/15.
13 | * MediaPicker
14 | */
15 | public class ImagePicker {
16 |
17 | /**
18 | * The constant IMAGE_PICKER_REQUEST_CODE.
19 | */
20 | public static final int IMAGE_PICKER_REQUEST_CODE = 42141;
21 | /**
22 | * The constant EXTRA_IMAGE_PATH.
23 | */
24 | public static final String EXTRA_IMAGE_PATH = "EXTRA_IMAGE_PATH";
25 |
26 | /**
27 | * Instantiates a new Image picker.
28 | *
29 | * @param builder the builder
30 | */
31 | ImagePicker(Builder builder) {
32 |
33 | // Required
34 | WeakReference context = builder.context;
35 |
36 | // Optional
37 | ImageConfig imageConfig = builder.imageConfig;
38 | Intent callingIntent = ImageActivity.getCallingIntent(context.get(), imageConfig);
39 |
40 | context.get().startActivityForResult(callingIntent, IMAGE_PICKER_REQUEST_CODE);
41 | }
42 |
43 |
44 | /**
45 | * The type Builder.
46 | */
47 | public static class Builder implements ImagePickerBuilderBase {
48 |
49 | // Required params
50 | private final WeakReference context;
51 |
52 | private ImageConfig imageConfig;
53 |
54 | /**
55 | * Instantiates a new Builder.
56 | *
57 | * @param context the context
58 | */
59 | public Builder(Activity context) {
60 | this.context = new WeakReference<>(context);
61 | this.imageConfig = new ImageConfig();
62 | }
63 |
64 | @Override
65 | public ImagePicker.Builder compressLevel(ComperesLevel compressLevel) {
66 | this.imageConfig.compressLevel = compressLevel;
67 | return this;
68 | }
69 |
70 | @Override
71 | public ImagePicker.Builder mode(Mode mode) {
72 | this.imageConfig.mode = mode;
73 | return this;
74 | }
75 |
76 | @Override
77 | public ImagePicker.Builder directory(String directory) {
78 | this.imageConfig.directory = directory;
79 | return this;
80 | }
81 |
82 | @Override
83 | public ImagePicker.Builder directory(Directory directory) {
84 | switch (directory) {
85 | case DEFAULT:
86 | this.imageConfig.directory = Environment.getExternalStorageDirectory() + ImageTags.Tags.IMAGE_PICKER_DIR;
87 | break;
88 | default:
89 | break;
90 | }
91 | return this;
92 | }
93 |
94 | @Override
95 | public ImagePicker.Builder extension(Extension extension) {
96 | this.imageConfig.extension = extension;
97 | return this;
98 | }
99 |
100 | @Override
101 | public ImagePicker.Builder scale(int minWidth, int minHeight) {
102 | this.imageConfig.reqHeight = minHeight;
103 | this.imageConfig.reqWidth = minWidth;
104 | return this;
105 | }
106 |
107 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
108 | @Override
109 | public ImagePicker.Builder allowMultipleImages(boolean allowMultiple) {
110 | this.imageConfig.allowMultiple = allowMultiple;
111 | return this;
112 | }
113 |
114 | @Override
115 | public ImagePicker.Builder enableDebuggingMode(boolean debug) {
116 | this.imageConfig.debug = debug;
117 | return this;
118 | }
119 |
120 | @Override
121 | public Builder allowOnlineImages(boolean allowOnlineImages) {
122 | this.imageConfig.allowOnlineImages = allowOnlineImages;
123 | return this;
124 | }
125 |
126 |
127 | @Override
128 | public ImagePicker build() {
129 | return new ImagePicker(this);
130 | }
131 |
132 |
133 | /**
134 | * Gets context.
135 | *
136 | * @return the context
137 | */
138 | public Activity getContext() {
139 | return context.get();
140 | }
141 |
142 | }
143 |
144 |
145 | /**
146 | * The enum Extension.
147 | */
148 | public enum Extension {
149 | /**
150 | * Png extension.
151 | */
152 | PNG(".png"), /**
153 | * Jpg extension.
154 | */
155 | JPG(".jpg");
156 | private final String value;
157 |
158 | Extension(String value) {
159 | this.value = value;
160 | }
161 |
162 | /**
163 | * Gets value.
164 | *
165 | * @return the value
166 | */
167 | public String getValue() {
168 | return value;
169 | }
170 | }
171 |
172 | /**
173 | * The enum Comperes level.
174 | */
175 | public enum ComperesLevel {
176 | /**
177 | * Hard comperes level.
178 | */
179 | HARD(20), /**
180 | * Medium comperes level.
181 | */
182 | MEDIUM(50), /**
183 | * Soft comperes level.
184 | */
185 | SOFT(80), /**
186 | * None comperes level.
187 | */
188 | NONE(100);
189 | private final int value;
190 |
191 | ComperesLevel(int value) {
192 | this.value = value;
193 | }
194 |
195 | /**
196 | * Gets value.
197 | *
198 | * @return the value
199 | */
200 | public int getValue() {
201 | return value;
202 | }
203 |
204 | }
205 |
206 | /**
207 | * The enum Mode.
208 | */
209 | public enum Mode {
210 | /**
211 | * Camera mode.
212 | */
213 | CAMERA(0), /**
214 | * Gallery mode.
215 | */
216 | GALLERY(1), /**
217 | * Camera and gallery mode.
218 | */
219 | CAMERA_AND_GALLERY(2);
220 | private final int value;
221 |
222 | Mode(int value) {
223 | this.value = value;
224 | }
225 |
226 | /**
227 | * Gets value.
228 | *
229 | * @return the value
230 | */
231 | public int getValue() {
232 | return value;
233 | }
234 | }
235 |
236 | /**
237 | * The enum Directory.
238 | */
239 | public enum Directory {
240 | /**
241 | * Default directory.
242 | */
243 | DEFAULT(0);
244 | private final int value;
245 |
246 | Directory(int value) {
247 | this.value = value;
248 | }
249 |
250 | /**
251 | * Gets value.
252 | *
253 | * @return the value
254 | */
255 | public int getValue() {
256 | return value;
257 | }
258 | }
259 | }
260 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImagePickerBuilderBase.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Image;
2 |
3 | /**
4 | * Created by Alhazmy13 on 8/7/16.
5 | * MediaPicker
6 | */
7 | public interface ImagePickerBuilderBase {
8 | /**
9 | * Compress level image picker . builder.
10 | *
11 | * @param compressLevel the compress level
12 | * @return the image picker . builder
13 | */
14 | ImagePicker.Builder compressLevel(ImagePicker.ComperesLevel compressLevel);
15 |
16 | /**
17 | * Mode image picker . builder.
18 | *
19 | * @param mode the mode
20 | * @return the image picker . builder
21 | */
22 | ImagePicker.Builder mode(ImagePicker.Mode mode);
23 |
24 | /**
25 | * Directory image picker . builder.
26 | *
27 | * @param directory the directory
28 | * @return the image picker . builder
29 | */
30 | ImagePicker.Builder directory(String directory);
31 |
32 | /**
33 | * Directory image picker . builder.
34 | *
35 | * @param directory the directory
36 | * @return the image picker . builder
37 | */
38 | ImagePicker.Builder directory(ImagePicker.Directory directory);
39 |
40 | /**
41 | * Extension image picker . builder.
42 | *
43 | * @param extension the extension
44 | * @return the image picker . builder
45 | */
46 | ImagePicker.Builder extension(ImagePicker.Extension extension);
47 |
48 | /**
49 | * Scale image picker . builder.
50 | *
51 | * @param minWidth the min width
52 | * @param minHeight the min height
53 | * @return the image picker . builder
54 | */
55 | ImagePicker.Builder scale(int minWidth, int minHeight);
56 |
57 | /**
58 | * Allow multiple images
59 | *
60 | * @param allowMultiple the allow multiple
61 | * @return the image picker . builder
62 | */
63 | ImagePicker.Builder allowMultipleImages(boolean allowMultiple);
64 |
65 | /**
66 | * Enable debugging mode image picker . builder.
67 | *
68 | * @param debug the debug
69 | * @return the image picker . builder
70 | */
71 | ImagePicker.Builder enableDebuggingMode(boolean debug);
72 |
73 | /**
74 | * Allow online images (ex: Google Drive, Google Photo ...)
75 | *
76 | * @param allowOnlineImages the allow online images
77 | * @return the image picker . builder
78 | */
79 | ImagePicker.Builder allowOnlineImages(boolean allowOnlineImages);
80 |
81 | /**
82 | * Build image picker.
83 | *
84 | * @return the image picker
85 | */
86 | ImagePicker build();
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageProcessing.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Image;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.ClipData;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.net.Uri;
8 | import android.os.Build;
9 |
10 | import net.alhazmy13.mediapicker.FileProcessing;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | /**
16 | * Created by Alhazmy13 on 8/15/16.
17 | * MediaPicker
18 | */
19 | class ImageProcessing {
20 |
21 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
22 | public static List processMultiImage(Context context, Intent data) {
23 | List listOfImgs = new ArrayList<>();
24 | if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) && (null == data.getData()))
25 | {
26 | ClipData clipdata = data.getClipData();
27 | for (int i = 0; i< (clipdata != null ? clipdata.getItemCount() : 0); i++)
28 | {
29 | Uri selectedImage = clipdata.getItemAt(i).getUri();
30 | String selectedImagePath = FileProcessing.getPath(context, selectedImage);
31 | listOfImgs.add(selectedImagePath);
32 | }
33 | }
34 | return listOfImgs;
35 | }
36 |
37 | }
38 |
39 |
40 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageTags.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Image;
2 |
3 | public class ImageTags {
4 | public static final class Tags {
5 | public static final String TAG = "ImagePicker";
6 | public static final String CAMERA_IMAGE_URI = "cameraImageUri";
7 | public static final String IMAGE_PATH = "IMAGE_PATH";
8 | public static final String IMAGE_PICKER_DIR = "/mediapicker/images/";
9 | public static final String IMG_CONFIG = "IMG_CONFIG";
10 | public static final String PICK_ERROR = "PICK_ERROR";
11 | public static final String IS_ALERT_SHOWING = "IS_ALERT_SHOWING";
12 | }
13 |
14 | public static final class Action {
15 | public static final String SERVICE_ACTION = "net.alhazmy13.mediapicker.rxjava.image.service";
16 | }
17 |
18 | public final class IntentCode {
19 | public static final int REQUEST_CODE_SELECT_MULTI_PHOTO = 5341;
20 | public static final int CAMERA_REQUEST = 1888;
21 | public static final int REQUEST_CODE_ASK_PERMISSIONS = 123;
22 | public static final int REQUEST_CODE_SELECT_PHOTO = 43;
23 |
24 |
25 | }
26 |
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Utility.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker;
2 |
3 | import android.content.Context;
4 | import android.database.Cursor;
5 | import android.graphics.Bitmap;
6 | import android.graphics.BitmapFactory;
7 | import android.graphics.Matrix;
8 | import android.media.ExifInterface;
9 | import android.net.Uri;
10 | import android.provider.MediaStore;
11 | import android.support.annotation.WorkerThread;
12 | import android.util.Log;
13 |
14 | import java.io.BufferedOutputStream;
15 | import java.io.File;
16 | import java.io.FileOutputStream;
17 | import java.io.IOException;
18 | import java.io.OutputStream;
19 | import java.util.UUID;
20 |
21 | /**
22 | * Created by Alhazmy13 on 11/10/15.
23 | */
24 | public class Utility {
25 | private static final String TAG = "Utility";
26 |
27 | public static String compressImage(String path) throws IOException {
28 | File file = new File(path);
29 | Bitmap bitmap = BitmapFactory.decodeFile(path);
30 | OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
31 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
32 | os.close();
33 | return path;
34 | }
35 |
36 | public static String getRandomString() {
37 | return UUID.randomUUID().toString();
38 | }
39 |
40 | public static void createFolder(String path) {
41 | try {
42 | File dir = new File(path.substring(0, path.lastIndexOf("/")));
43 | Log.d(TAG, "createFolder: " + dir.exists());
44 | if (!dir.exists()) {
45 | dir.mkdirs();
46 | }
47 | } catch (Exception ex) {
48 | Log.w(TAG, "creating file error: ", ex);
49 | }
50 |
51 | }
52 |
53 | @WorkerThread
54 | public static void compressAndRotateIfNeeded(File sourceFile, File destinationFile, int value, int reqWidth, int reqHeight) throws IOException {
55 |
56 | String path = sourceFile.getAbsolutePath();
57 |
58 | BitmapFactory.Options bounds = new BitmapFactory.Options();
59 | Bitmap bm;
60 | if (reqHeight != 0 && reqWidth != 0) {
61 | bounds.inJustDecodeBounds = true;
62 | bm = BitmapFactory.decodeFile(path, bounds);
63 | bounds.inSampleSize = calculateInSampleSize(bounds, reqWidth, reqHeight);
64 | bounds.inJustDecodeBounds = false;
65 | }
66 | bm = BitmapFactory.decodeFile(path, bounds);
67 |
68 | if (bm == null) {
69 | Log.d("compress", "bitmap is null");
70 | return;
71 | }
72 |
73 | int rotationAngle = getCameraPhotoOrientation(sourceFile);
74 |
75 | Matrix matrix = new Matrix();
76 | matrix.postRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
77 | Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight,
78 | matrix, true);
79 |
80 | FileOutputStream fos = new FileOutputStream(destinationFile.getAbsoluteFile());
81 | rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, value, fos);
82 | fos.flush();
83 | fos.close();
84 | }
85 |
86 | private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
87 | // Raw height and width of image
88 | final int height = options.outHeight;
89 | final int width = options.outWidth;
90 | int inSampleSize = 1;
91 |
92 | if (height > reqHeight || width > reqWidth) {
93 |
94 | final int halfHeight = height / 2;
95 | final int halfWidth = width / 2;
96 |
97 | // Calculate the largest inSampleSize value that is a power of 2 and keeps both
98 | // height and width larger than the requested height and width.
99 | while ((halfHeight / inSampleSize) >= reqHeight
100 | && (halfWidth / inSampleSize) >= reqWidth) {
101 | inSampleSize *= 2;
102 | }
103 | }
104 |
105 | return inSampleSize;
106 | }
107 |
108 | @WorkerThread
109 | private static int getCameraPhotoOrientation(File file) throws IOException {
110 | ExifInterface exif = new ExifInterface(
111 | file.getAbsolutePath());
112 | int orientation = exif.getAttributeInt(
113 | ExifInterface.TAG_ORIENTATION,
114 | ExifInterface.ORIENTATION_NORMAL);
115 |
116 | int rotate = 0;
117 |
118 | switch (orientation) {
119 | case ExifInterface.ORIENTATION_ROTATE_270:
120 | rotate = 270;
121 | break;
122 | case ExifInterface.ORIENTATION_ROTATE_180:
123 | rotate = 180;
124 | break;
125 | case ExifInterface.ORIENTATION_ROTATE_90:
126 | rotate = 90;
127 | break;
128 | default:
129 | break;
130 | }
131 |
132 | return rotate;
133 | }
134 |
135 | }
136 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoActivity.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Video;
2 |
3 | import android.Manifest;
4 | import android.annotation.TargetApi;
5 | import android.app.AlertDialog;
6 | import android.content.Context;
7 | import android.content.DialogInterface;
8 | import android.content.Intent;
9 | import android.content.pm.PackageManager;
10 | import android.net.Uri;
11 | import android.os.AsyncTask;
12 | import android.os.Build;
13 | import android.os.Bundle;
14 | import android.provider.MediaStore;
15 | import android.support.annotation.NonNull;
16 | import android.support.annotation.RequiresApi;
17 | import android.support.v4.app.ActivityCompat;
18 | import android.support.v4.content.FileProvider;
19 | import android.support.v7.app.AppCompatActivity;
20 | import android.util.Log;
21 | import android.widget.Toast;
22 |
23 | import net.alhazmy13.mediapicker.FileProcessing;
24 | import net.alhazmy13.mediapicker.R;
25 | import net.alhazmy13.mediapicker.Utility;
26 |
27 | import java.io.File;
28 | import java.io.Serializable;
29 | import java.lang.ref.WeakReference;
30 | import java.util.ArrayList;
31 | import java.util.HashMap;
32 | import java.util.List;
33 | import java.util.Map;
34 |
35 | /**
36 | * Created by Alhazmy13 on 10/26/15.
37 | * MediaPicker
38 | */
39 | public class VideoActivity extends AppCompatActivity {
40 |
41 | private File destination;
42 | private Uri mVideoUri;
43 | private VideoConfig mVideoConfig;
44 | private List mListOfVideos;
45 | private AlertDialog alertDialog;
46 |
47 | public static Intent getCallingIntent(Context activity, VideoConfig videoConfig) {
48 | Intent intent = new Intent(activity, VideoActivity.class);
49 | intent.putExtra(VideoTags.Tags.IMG_CONFIG, videoConfig);
50 | return intent;
51 | }
52 |
53 | @Override
54 | protected void onCreate(Bundle savedInstanceState) {
55 | super.onCreate(savedInstanceState);
56 | Intent intent = getIntent();
57 | if (intent != null) {
58 | mVideoConfig = (VideoConfig) intent.getSerializableExtra(VideoTags.Tags.IMG_CONFIG);
59 | }
60 |
61 | if (savedInstanceState == null) {
62 | pickVideoWrapper();
63 | mListOfVideos = new ArrayList<>();
64 | }
65 | if (mVideoConfig.debug)
66 | Log.d(VideoTags.Tags.TAG, mVideoConfig.toString());
67 | }
68 |
69 | @Override
70 | protected void onPause() {
71 | if (alertDialog != null)
72 | alertDialog.dismiss();
73 | super.onPause();
74 | }
75 |
76 | private void pickVideo() {
77 | Utility.createFolder(mVideoConfig.directory);
78 | destination = new File(mVideoConfig.directory, Utility.getRandomString() + mVideoConfig.extension.getValue());
79 | switch (mVideoConfig.mode) {
80 | case CAMERA:
81 | startActivityFromCamera();
82 | break;
83 | case GALLERY:
84 | if (mVideoConfig.allowMultiple)
85 | startActivityFromGalleryMultiImg();
86 | else
87 | startActivityFromGallery();
88 | break;
89 | case CAMERA_AND_GALLERY:
90 | showFromCameraOrGalleryAlert();
91 | break;
92 | default:
93 | break;
94 | }
95 | }
96 |
97 | private void showFromCameraOrGalleryAlert() {
98 | alertDialog = new AlertDialog.Builder(this)
99 | .setTitle(getString(R.string.media_picker_select_from))
100 | .setPositiveButton(getString(R.string.media_picker_camera), new DialogInterface.OnClickListener() {
101 | @Override
102 | public void onClick(DialogInterface dialogInterface, int i) {
103 | if (mVideoConfig.debug)
104 | Log.d(VideoTags.Tags.TAG, "Alert Dialog - Start From Camera");
105 | startActivityFromCamera();
106 | alertDialog.dismiss();
107 | }
108 | })
109 | .setNegativeButton(getString(R.string.media_picker_gallery), new DialogInterface.OnClickListener() {
110 | @Override
111 | public void onClick(DialogInterface dialogInterface, int i) {
112 | if (mVideoConfig.debug)
113 | Log.d(VideoTags.Tags.TAG, "Alert Dialog - Start From Gallery");
114 | if (mVideoConfig.allowMultiple)
115 | startActivityFromGalleryMultiImg();
116 | else
117 | startActivityFromGallery();
118 | alertDialog.dismiss();
119 | }
120 | })
121 | .setOnCancelListener(new DialogInterface.OnCancelListener() {
122 | @Override
123 | public void onCancel(DialogInterface dialogInterface) {
124 | if (mVideoConfig.debug)
125 | Log.d(VideoTags.Tags.TAG, "Alert Dialog - Canceled");
126 | alertDialog.dismiss();
127 | finish();
128 | }
129 | }).create();
130 | alertDialog.show();
131 |
132 | }
133 |
134 | private void startActivityFromGallery() {
135 | mVideoConfig.isImgFromCamera = false;
136 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
137 | photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
138 | photoPickerIntent.setType("video/*");
139 | startActivityForResult(photoPickerIntent, VideoTags.IntentCode.REQUEST_CODE_SELECT_PHOTO);
140 | if (mVideoConfig.debug)
141 | Log.d(VideoTags.Tags.TAG, "Gallery Start with Single video mode");
142 | }
143 |
144 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
145 | private void startActivityFromGalleryMultiImg() {
146 | mVideoConfig.isImgFromCamera = false;
147 | Intent photoPickerIntent = new Intent();
148 | photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
149 | photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
150 | photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
151 | photoPickerIntent.setType("video/*");
152 | startActivityForResult(Intent.createChooser(photoPickerIntent, "Select Picture"), VideoTags.IntentCode.REQUEST_CODE_SELECT_MULTI_PHOTO);
153 | if (mVideoConfig.debug)
154 | Log.d(VideoTags.Tags.TAG, "Gallery Start with Multiple videos mode");
155 | }
156 |
157 | private void startActivityFromCamera() {
158 | mVideoConfig.isImgFromCamera = true;
159 | Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
160 | mVideoUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", destination);
161 | intent.putExtra(MediaStore.EXTRA_OUTPUT, mVideoUri);
162 | startActivityForResult(Intent.createChooser(intent, "Select Video"), VideoTags.IntentCode.CAMERA_REQUEST);
163 | if (mVideoConfig.debug)
164 | Log.d(VideoTags.Tags.TAG, "Camera Start");
165 | }
166 |
167 | @Override
168 | protected void onSaveInstanceState(Bundle outState) {
169 | super.onSaveInstanceState(outState);
170 | if (mVideoUri != null) {
171 | outState.putString(VideoTags.Tags.CAMERA_IMAGE_URI, mVideoUri.toString());
172 | outState.putSerializable(VideoTags.Tags.IMG_CONFIG, mVideoConfig);
173 | }
174 | }
175 |
176 | @Override
177 | protected void onRestoreInstanceState(Bundle savedInstanceState) {
178 | super.onRestoreInstanceState(savedInstanceState);
179 | if (savedInstanceState.containsKey(VideoTags.Tags.CAMERA_IMAGE_URI)) {
180 | mVideoUri = Uri.parse(savedInstanceState.getString(VideoTags.Tags.CAMERA_IMAGE_URI));
181 | destination = new File(mVideoUri.getPath());
182 | mVideoConfig = (VideoConfig) savedInstanceState.getSerializable(VideoTags.Tags.IMG_CONFIG);
183 | }
184 | }
185 |
186 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
187 | @Override
188 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
189 | if (mVideoConfig.debug)
190 | Log.d(VideoTags.Tags.TAG, "onActivityResult() called with: " + "requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
191 | if (resultCode == RESULT_OK) {
192 | switch (requestCode) {
193 | case VideoTags.IntentCode.CAMERA_REQUEST:
194 | new VideoActivity.CompresVideoTask(destination.getAbsolutePath(), mVideoConfig
195 | , VideoActivity.this).execute();
196 | break;
197 | case VideoTags.IntentCode.REQUEST_CODE_SELECT_PHOTO:
198 | processOneVideo(data);
199 | break;
200 | case VideoTags.IntentCode.REQUEST_CODE_SELECT_MULTI_PHOTO:
201 | //Check if the intent contain only one image
202 | if (data.getClipData() == null) {
203 | processOneVideo(data);
204 | } else {
205 | //intent has multi images
206 | mListOfVideos = VideoProcessing.processMultiVideos(this, data);
207 | new VideoActivity.CompresVideoTask(mListOfVideos,
208 | mVideoConfig, VideoActivity.this).execute();
209 | }
210 | break;
211 | default:
212 | break;
213 | }
214 | } else {
215 | Intent intent = new Intent();
216 | intent.setAction(VideoTags.Action.SERVICE_ACTION);
217 | intent.putExtra(VideoTags.Tags.PICK_ERROR, "user did not select any videos");
218 | sendBroadcast(intent);
219 | finish();
220 | }
221 | }
222 |
223 | private void processOneVideo(Intent data) {
224 | try {
225 | Uri selectedVideo = data.getData();
226 | String path = FileProcessing.getVideoPath(selectedVideo, VideoActivity.this);
227 | new VideoActivity.CompresVideoTask(path,
228 | mVideoConfig, VideoActivity.this).execute();
229 |
230 | } catch (Exception ex) {
231 | Intent intent = new Intent();
232 | intent.setAction(VideoTags.Action.SERVICE_ACTION);
233 | intent.putExtra(VideoTags.Tags.PICK_ERROR, "Issue with video path: " + ex.getMessage());
234 | sendBroadcast(intent);
235 | setResult(RESULT_CANCELED, intent);
236 | finish();
237 | }
238 |
239 | }
240 |
241 | private void finishActivity(List path) {
242 | Intent intent = new Intent();
243 | intent.setAction(VideoTags.Action.SERVICE_ACTION);
244 | intent.putExtra(VideoTags.Tags.VIDEO_PATH, (Serializable) path);
245 | sendBroadcast(intent);
246 |
247 | Intent resultIntent = new Intent();
248 | resultIntent.putExtra(VideoPicker.EXTRA_VIDEO_PATH, (Serializable) path);
249 | setResult(RESULT_OK, resultIntent);
250 | finish();
251 | }
252 |
253 | private void pickVideoWrapper() {
254 | if (Build.VERSION.SDK_INT >= 23) {
255 | List permissionsNeeded = new ArrayList<>();
256 |
257 | final List permissionsList = new ArrayList<>();
258 | if ((mVideoConfig.mode == VideoPicker.Mode.CAMERA || mVideoConfig.mode == VideoPicker.Mode.CAMERA_AND_GALLERY) && !addPermission(permissionsList, Manifest.permission.CAMERA))
259 | permissionsNeeded.add(getString(R.string.media_picker_camera));
260 | if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
261 | permissionsNeeded.add(getString(R.string.media_picker_read_Write_external_storage));
262 |
263 | if (permissionsList.size() > 0) {
264 | if (permissionsNeeded.size() > 0) {
265 | // Need Rationale
266 | StringBuilder message = new StringBuilder(getString(R.string.media_picker_you_need_to_grant_access_to) + permissionsNeeded.get(0));
267 | for (int i = 1; i < permissionsNeeded.size(); i++)
268 | message.append(", ").append(permissionsNeeded.get(i));
269 | showMessageOKCancel(message.toString(),
270 | new DialogInterface.OnClickListener() {
271 | @Override
272 | public void onClick(DialogInterface dialog, int which) {
273 | ActivityCompat.requestPermissions(VideoActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
274 | VideoTags.IntentCode.REQUEST_CODE_ASK_PERMISSIONS);
275 | }
276 | });
277 | return;
278 | }
279 | ActivityCompat.requestPermissions(VideoActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
280 | VideoTags.IntentCode.REQUEST_CODE_ASK_PERMISSIONS);
281 | return;
282 | }
283 |
284 | pickVideo();
285 | } else {
286 | pickVideo();
287 | }
288 | }
289 |
290 | private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
291 | new AlertDialog.Builder(VideoActivity.this)
292 | .setMessage(message)
293 | .setPositiveButton(getString(R.string.media_picker_ok), okListener)
294 | .setNegativeButton(getString(R.string.media_picker_cancel), null)
295 | .create()
296 | .show();
297 | }
298 |
299 | private boolean addPermission(List permissionsList, String permission) {
300 | if (ActivityCompat.checkSelfPermission(VideoActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
301 | permissionsList.add(permission);
302 | // Check for Rationale Option
303 | return ActivityCompat.shouldShowRequestPermissionRationale(VideoActivity.this, permission);
304 | }
305 | return true;
306 | }
307 |
308 | @Override
309 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
310 | switch (requestCode) {
311 | case VideoTags.IntentCode.REQUEST_CODE_ASK_PERMISSIONS:
312 | Map perms = new HashMap();
313 | // Initial
314 | perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
315 | perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
316 | // Fill with results
317 | for (int i = 0; i < permissions.length; i++)
318 | perms.put(permissions[i], grantResults[i]);
319 | // Check for ACCESS_FINE_LOCATION
320 | if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
321 | && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
322 | // All Permissions Granted
323 | pickVideo();
324 | } else {
325 | // Permission Denied
326 | Toast.makeText(VideoActivity.this, getString(R.string.media_picker_some_permission_is_denied), Toast.LENGTH_SHORT)
327 | .show();
328 | onBackPressed();
329 | }
330 |
331 | break;
332 | default:
333 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
334 | }
335 | }
336 |
337 | @Override
338 | public void onBackPressed() {
339 | setResult(RESULT_CANCELED);
340 | super.onBackPressed();
341 | }
342 |
343 | private static class CompresVideoTask extends AsyncTask {
344 |
345 | private final VideoConfig mVideoConfig;
346 | private final List listOfImgs;
347 | private List destinationPaths;
348 | private WeakReference mContext;
349 |
350 |
351 | CompresVideoTask(List listOfImgs, VideoConfig videoConfig, VideoActivity context) {
352 | this.listOfImgs = listOfImgs;
353 | this.mContext = new WeakReference<>(context);
354 | this.mVideoConfig = videoConfig;
355 | this.destinationPaths = new ArrayList<>();
356 | }
357 |
358 | CompresVideoTask(String absolutePath, VideoConfig videoConfig, VideoActivity context) {
359 | List list = new ArrayList<>();
360 | list.add(absolutePath);
361 | this.listOfImgs = list;
362 | this.mContext = new WeakReference<>(context);
363 | this.destinationPaths = new ArrayList<>();
364 | this.mVideoConfig = videoConfig;
365 | }
366 |
367 |
368 | @Override
369 | protected Void doInBackground(Void... params) {
370 | for (String mPath : listOfImgs) {
371 | File file = new File(mPath);
372 | File destinationFile;
373 | if (mVideoConfig.isImgFromCamera) {
374 | destinationFile = file;
375 | } else {
376 | destinationFile = new File(mVideoConfig.directory, Utility.getRandomString() + mVideoConfig.extension.getValue());
377 | FileProcessing.copyDirectory(file, destinationFile);
378 | }
379 | destinationPaths.add(destinationFile.getAbsolutePath());
380 |
381 | }
382 |
383 | return null;
384 | }
385 |
386 | @Override
387 | protected void onPostExecute(Void aVoid) {
388 | super.onPostExecute(aVoid);
389 |
390 | VideoActivity context = mContext.get();
391 | if (context != null) {
392 | context.finishActivity(destinationPaths);
393 |
394 | }
395 | }
396 | }
397 |
398 |
399 | }
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoConfig.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Video;
2 |
3 | import android.os.Environment;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * Created by Alhazmy13 on 8/16/16.
9 | * MediaPicker
10 | */
11 | class VideoConfig implements Serializable {
12 |
13 | protected VideoPicker.Extension extension;
14 | protected VideoPicker.Mode mode;
15 | protected String directory;
16 | protected boolean allowMultiple;
17 | protected boolean isImgFromCamera;
18 | protected boolean debug;
19 |
20 | VideoConfig() {
21 | this.extension = VideoPicker.Extension.MP4;
22 | this.mode = VideoPicker.Mode.CAMERA;
23 | this.directory = Environment.getExternalStorageDirectory() + VideoTags.Tags.IMAGE_PICKER_DIR;
24 | this.allowMultiple = false;
25 | }
26 |
27 | @Override
28 | public String toString() {
29 | return "ImageConfig{" +
30 | "extension=" + extension +
31 | ", mode=" + mode +
32 | ", directory='" + directory + '\'' +
33 | ", allowMultiple=" + allowMultiple +
34 | ", isImgFromCamera=" + isImgFromCamera +
35 | ", debug=" + debug +
36 | '}';
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoPicker.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Video;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Environment;
6 |
7 | import java.lang.ref.WeakReference;
8 |
9 |
10 | /**
11 | * Created by Alhazmy13 on 10/26/15.
12 | */
13 | public class VideoPicker {
14 |
15 |
16 | public static final int VIDEO_PICKER_REQUEST_CODE = 53213;
17 | public static final String EXTRA_VIDEO_PATH = "EXTRA_VIDEO_PATH";
18 |
19 | VideoPicker(VideoPicker.Builder builder) {
20 |
21 | // Required
22 | WeakReference context = builder.context;
23 |
24 | // Optional
25 | VideoConfig imageConfig = builder.imageConfig;
26 | Intent callingIntent = VideoActivity.getCallingIntent(context.get(), imageConfig);
27 |
28 | context.get().startActivityForResult(callingIntent, VIDEO_PICKER_REQUEST_CODE);
29 | }
30 |
31 |
32 | public static class Builder implements VideoPickerBuilderBase {
33 |
34 | // Required params
35 | private final WeakReference context;
36 |
37 | private VideoConfig imageConfig;
38 |
39 | public Builder(Activity context) {
40 | this.context = new WeakReference<>(context);
41 | this.imageConfig = new VideoConfig();
42 | }
43 |
44 |
45 | @Override
46 | public VideoPicker.Builder mode(VideoPicker.Mode mode) {
47 | this.imageConfig.mode = mode;
48 | return this;
49 | }
50 |
51 | @Override
52 | public VideoPicker.Builder directory(String directory) {
53 | this.imageConfig.directory = directory;
54 | return this;
55 | }
56 |
57 | @Override
58 | public VideoPicker.Builder directory(VideoPicker.Directory directory) {
59 | switch (directory) {
60 | case DEFAULT:
61 | this.imageConfig.directory = Environment.getExternalStorageDirectory() + VideoTags.Tags.IMAGE_PICKER_DIR;
62 | break;
63 | default:
64 | break;
65 | }
66 | return this;
67 | }
68 |
69 | @Override
70 | public VideoPicker.Builder extension(VideoPicker.Extension extension) {
71 | this.imageConfig.extension = extension;
72 | return this;
73 | }
74 |
75 | @Override
76 | public VideoPicker.Builder enableDebuggingMode(boolean debug) {
77 | this.imageConfig.debug = debug;
78 | return this;
79 | }
80 |
81 |
82 | @Override
83 | public VideoPicker build() {
84 | return new VideoPicker(this);
85 | }
86 |
87 |
88 | public Activity getContext() {
89 | return context.get();
90 | }
91 |
92 | }
93 |
94 |
95 | public enum Extension {
96 | MP4(".mp4");
97 | private final String value;
98 |
99 | Extension(String value) {
100 | this.value = value;
101 | }
102 |
103 | public String getValue() {
104 | return value;
105 | }
106 | }
107 |
108 |
109 | public enum Mode {
110 | CAMERA(0), GALLERY(1), CAMERA_AND_GALLERY(2);
111 | private final int value;
112 |
113 | Mode(int value) {
114 | this.value = value;
115 | }
116 |
117 | public int getValue() {
118 | return value;
119 | }
120 | }
121 |
122 | public enum Directory {
123 | DEFAULT(0);
124 | private final int value;
125 |
126 | Directory(int value) {
127 | this.value = value;
128 | }
129 |
130 | public int getValue() {
131 | return value;
132 | }
133 | }
134 |
135 | }
136 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoPickerBuilderBase.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Video;
2 |
3 | /**
4 | * Created by Alhazmy13 on 8/7/16.
5 | * MediaPicker
6 | */
7 | public interface VideoPickerBuilderBase {
8 |
9 | VideoPicker.Builder mode(VideoPicker.Mode mode);
10 |
11 | VideoPicker.Builder directory(String directory);
12 |
13 | VideoPicker.Builder directory(VideoPicker.Directory directory);
14 |
15 | VideoPicker.Builder extension(VideoPicker.Extension extension);
16 |
17 | VideoPicker.Builder enableDebuggingMode(boolean debug);
18 |
19 | VideoPicker build();
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoProcessing.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Video;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.ClipData;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.net.Uri;
8 | import android.os.Build;
9 |
10 | import net.alhazmy13.mediapicker.FileProcessing;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | /**
16 | * Created by Alhazmy13 on 8/15/16.
17 | * MediaPicker
18 | */
19 | class VideoProcessing {
20 |
21 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
22 | static List processMultiVideos(Context context, Intent data) {
23 | List listOfImgs = new ArrayList<>();
24 | if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) && (null == data.getData())) {
25 | ClipData clipdata = data.getClipData();
26 | for (int i = 0; i < clipdata.getItemCount(); i++) {
27 | Uri selectedImage = clipdata.getItemAt(i).getUri();
28 | String selectedImagePath = FileProcessing.getPath(context, selectedImage);
29 | listOfImgs.add(selectedImagePath);
30 | }
31 | }
32 | return listOfImgs;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoTags.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.Video;
2 |
3 | /**
4 | * Created by Alhazmy13 on 2/8/16.
5 | * MediaPicker
6 | */
7 | public class VideoTags {
8 | public static final class Tags {
9 | public static final String TAG = "VideoPicker";
10 | public static final String CAMERA_IMAGE_URI = "cameraVideoUri";
11 | public static final String VIDEO_PATH = "VIDEO_PATH";
12 | public static final String IMAGE_PICKER_DIR = "/mediapicker/videos/";
13 | public static final String IMG_CONFIG = "IMG_CONFIG";
14 | public static final String PICK_ERROR = "PICK_ERROR";
15 | }
16 |
17 | public static final class Action {
18 | public static final String SERVICE_ACTION = "net.alhazmy13.mediapicker.rxjava.video.service";
19 | }
20 |
21 | public final class IntentCode {
22 | public static final int REQUEST_CODE_SELECT_MULTI_PHOTO = 5341;
23 | public static final int CAMERA_REQUEST = 1888;
24 | public static final int REQUEST_CODE_ASK_PERMISSIONS = 123;
25 | public static final int REQUEST_CODE_SELECT_PHOTO = 43;
26 |
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/libary/src/main/res/values-ar/string.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | حدد مصدر الصورة
4 | الكاميرا
5 | مستعرض الصور
6 | موافق
7 | إلغاء
8 | بعض الصلاحيات لم يوافق عليها!
9 | يجب الموافقة على الصلاحيات
10 |
--------------------------------------------------------------------------------
/libary/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Select From:
3 | Camera
4 | Gallery
5 | Ok
6 | Cancel
7 | Some Permission is Denied
8 | You need to grant access to
9 |
10 |
11 |
--------------------------------------------------------------------------------
/libary/src/main/res/values/style.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/libary/src/main/res/xml/provider_paths.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
--------------------------------------------------------------------------------
/license:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/rxjava/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/rxjava/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | //apply plugin: 'com.novoda.bintray-release'
3 |
4 | //./gradlew bintrayUpload
5 |
6 | //publish {
7 | // userOrg = 'alhazmy13'
8 | // groupId = 'net.alhazmy13.MediaPicker'
9 | // artifactId = 'rxjava'
10 | // uploadName = 'MediaPickerRx'
11 | // publishVersion = '2.4.4'
12 | // desc = 'MediaPickerRx'
13 | // dryRun = false
14 | // website = 'https://github.com/alhzmy13/MediaPicker'
15 | //}
16 |
17 | android {
18 | compileSdkVersion 28
19 |
20 | defaultConfig {
21 | minSdkVersion 14
22 | targetSdkVersion 28
23 | versionCode 1
24 | versionName "1.0"
25 | }
26 |
27 | buildTypes {
28 | release {
29 | minifyEnabled false
30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
31 | }
32 | }
33 | }
34 |
35 | dependencies {
36 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
37 | implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
38 | implementation project(':libary')
39 | }
40 |
--------------------------------------------------------------------------------
/rxjava/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 /Users/Alhazmy13/Library/Android/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 |
--------------------------------------------------------------------------------
/rxjava/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerHelper.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.rxjava.image;
2 |
3 | import net.alhazmy13.mediapicker.Image.ImagePicker;
4 |
5 | import java.util.List;
6 |
7 | import io.reactivex.Observable;
8 |
9 |
10 | /**
11 | * Created by Alhazmy13 on 8/7/16.
12 | * MediaPicker
13 | */
14 | public class ImagePickerHelper {
15 | private ImagePicker.Builder mBuilder;
16 |
17 | public ImagePickerHelper(ImagePicker.Builder builder) {
18 | this.mBuilder = builder;
19 | }
20 |
21 | public Observable> getObservable() {
22 | return Observable.create(new ImagePickerObservable(mBuilder));
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerObservable.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.rxjava.image;
2 |
3 |
4 | import android.content.Context;
5 | import android.content.IntentFilter;
6 |
7 | import net.alhazmy13.mediapicker.Image.ImagePicker;
8 | import net.alhazmy13.mediapicker.Image.ImageTags;
9 |
10 | import java.lang.ref.WeakReference;
11 | import java.util.List;
12 |
13 | import io.reactivex.ObservableEmitter;
14 | import io.reactivex.ObservableOnSubscribe;
15 | import io.reactivex.disposables.Disposable;
16 | import io.reactivex.disposables.Disposables;
17 | import io.reactivex.functions.Action;
18 |
19 | /**
20 | * Created by Alhazmy13 on 8/7/16.
21 | * MediaPicker
22 | */
23 | public class ImagePickerObservable implements ObservableOnSubscribe>, Disposable {
24 |
25 | private static final String TAG = "ImagePickerObservable";
26 | private ImagePicker.Builder imagePicker;
27 | private ImagePickerReceiver broadcastReceiver;
28 | private final WeakReference contextWeakReference;
29 |
30 | public ImagePickerObservable(ImagePicker.Builder imagePicker) {
31 | this.imagePicker = imagePicker;
32 | this.contextWeakReference = new WeakReference(imagePicker.getContext());
33 |
34 | }
35 |
36 |
37 | @Override
38 | public void subscribe(ObservableEmitter> emitter) {
39 | emitter.setDisposable(Disposables.fromAction(new Action() {
40 | @Override
41 | public void run() {
42 | dispose();
43 | }
44 | }));
45 | broadcastReceiver = new ImagePickerReceiver(emitter);
46 | imagePicker.build();
47 | contextWeakReference.get().registerReceiver(broadcastReceiver, new IntentFilter(ImageTags.Action.SERVICE_ACTION));
48 | }
49 |
50 | @Override
51 | public void dispose() {
52 | if (contextWeakReference != null && contextWeakReference.get() != null && broadcastReceiver != null) {
53 | contextWeakReference.get().unregisterReceiver(broadcastReceiver);
54 |
55 | }
56 | broadcastReceiver = null;
57 | }
58 |
59 | @Override
60 | public boolean isDisposed() {
61 | return broadcastReceiver == null;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerReceiver.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.rxjava.image;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.util.Log;
7 |
8 | import net.alhazmy13.mediapicker.Image.ImageTags;
9 |
10 | import java.util.List;
11 |
12 | import io.reactivex.Emitter;
13 |
14 |
15 | /**
16 | * Created by Alhazmy13 on 8/7/16.
17 | * MediaPicker
18 | */
19 | public class ImagePickerReceiver extends BroadcastReceiver {
20 |
21 | private static final String TAG = "VideoPickerReceiver";
22 | private Emitter super List> emitter;
23 |
24 | public ImagePickerReceiver(Emitter super List> observer) {
25 | this.emitter = observer;
26 | }
27 |
28 | public void onReceive(Context context, Intent intent) {
29 | Log.d(TAG, "Received message " + intent);
30 | List imagePath = intent.getStringArrayListExtra(ImageTags.Tags.IMAGE_PATH);
31 | if (imagePath != null && imagePath.size() > 0) {
32 | emitter.onNext(imagePath);
33 | emitter.onComplete();
34 | } else {
35 | emitter.onError(new Throwable(intent.getStringExtra(ImageTags.Tags.PICK_ERROR)));
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerHelper.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.rxjava.video;
2 |
3 | import net.alhazmy13.mediapicker.Video.VideoPicker;
4 |
5 | import java.util.List;
6 |
7 | import io.reactivex.Observable;
8 |
9 |
10 | /**
11 | * Created by Alhazmy13 on 8/7/16.
12 | * MediaPicker
13 | */
14 | public class VideoPickerHelper {
15 | private VideoPicker.Builder mBuilder;
16 |
17 | public VideoPickerHelper(VideoPicker.Builder builder) {
18 | this.mBuilder = builder;
19 | }
20 |
21 | public Observable> getObservable() {
22 | return Observable.create(new VideoPickerObservable(mBuilder));
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerObservable.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.rxjava.video;
2 |
3 |
4 | import android.content.Context;
5 | import android.content.IntentFilter;
6 |
7 | import net.alhazmy13.mediapicker.Video.VideoPicker;
8 | import net.alhazmy13.mediapicker.Video.VideoTags;
9 |
10 | import java.lang.ref.WeakReference;
11 | import java.util.List;
12 |
13 | import io.reactivex.ObservableEmitter;
14 | import io.reactivex.ObservableOnSubscribe;
15 | import io.reactivex.disposables.Disposable;
16 |
17 | /**
18 | * Created by Alhazmy13 on 8/7/16.
19 | * MediaPicker
20 | */
21 | public class VideoPickerObservable implements ObservableOnSubscribe>, Disposable {
22 |
23 | private static final String TAG = "VideoPickerObservable";
24 | private VideoPicker.Builder mVideoPicker;
25 | private VideoPickerReceiver broadcastReceiver;
26 | private final WeakReference contextWeakReference;
27 |
28 | public VideoPickerObservable(VideoPicker.Builder videoPicker) {
29 | this.mVideoPicker = videoPicker;
30 | this.contextWeakReference = new WeakReference(mVideoPicker.getContext());
31 |
32 |
33 | }
34 |
35 |
36 | @Override
37 | public void subscribe(ObservableEmitter> emitter) {
38 | broadcastReceiver = new VideoPickerReceiver(emitter);
39 | mVideoPicker.build();
40 | contextWeakReference.get().registerReceiver(broadcastReceiver, new IntentFilter(VideoTags.Action.SERVICE_ACTION));
41 |
42 | }
43 |
44 | @Override
45 | public void dispose() {
46 | if (contextWeakReference != null && contextWeakReference.get() != null && broadcastReceiver != null) {
47 | contextWeakReference.get().unregisterReceiver(broadcastReceiver);
48 |
49 | }
50 | broadcastReceiver = null;
51 | }
52 |
53 | @Override
54 | public boolean isDisposed() {
55 | return broadcastReceiver == null;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerReceiver.java:
--------------------------------------------------------------------------------
1 | package net.alhazmy13.mediapicker.rxjava.video;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.util.Log;
7 |
8 | import net.alhazmy13.mediapicker.Video.VideoTags;
9 |
10 | import java.util.List;
11 |
12 | import io.reactivex.ObservableEmitter;
13 |
14 | /**
15 | * Created by Alhazmy13 on 8/7/16.
16 | * MediaPicker
17 | */
18 | public class VideoPickerReceiver extends BroadcastReceiver {
19 |
20 | private static final String TAG = "VideoPickerReceiver";
21 | private ObservableEmitter> observer;
22 |
23 | public VideoPickerReceiver(ObservableEmitter> observer) {
24 | this.observer = observer;
25 | }
26 |
27 | public void onReceive(Context context, Intent intent) {
28 | Log.d(TAG, "Received message " + intent);
29 | List imagePath = intent.getStringArrayListExtra(VideoTags.Tags.VIDEO_PATH);
30 | if (imagePath != null && imagePath.size() > 0)
31 | observer.onNext(imagePath);
32 | else
33 | observer.onError(new Throwable(intent.getStringExtra(VideoTags.Tags.PICK_ERROR)));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/rxjava/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Adapter-rxjava
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':libary', ':rxjava'
2 |
--------------------------------------------------------------------------------