Skip to content

Commit a3a8ca7

Browse files
authored
Introduce new v2.0 (#2)
* Rename .java to .kt * Migrated library to Kotlin, support api 33 * Implemented custom gallery selector * Update README.md * Fixed bugs * Cleanup * Added javaDoc to non-internal classes * Added javaDoc to non-internal classes * Introduce image picker delegate * Update gradle wrapper * Refactor rotation feature * Finalize release v2.0 * Resolve conflicts * Fix naming
1 parent d0901dd commit a3a8ca7

File tree

57 files changed

+2236
-940
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2236
-940
lines changed

README.md

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,100 @@ Android library that can be used as quick solution to ImagePicker feature implem
99
- Camera photo picker
1010
- Gallery single photo picker
1111
- Gallery multiple photo picker
12+
- Custom gallery picker, supports multiple selection (for old non-AOSP Android ROMs that does not support multiple selection intent)
1213

1314
## Implementation
1415

1516
1. In project-level gradle add new maven repository:
1617

17-
<pre>
18+
```groovy
1819
allprojects {
1920
repositories {
20-
...
2121
maven { url 'https://jitpack.io' }
2222
}
2323
}
24-
</pre>
24+
```
2525

2626
2. In app-level gradle add new implementation:
2727

28-
<pre>
28+
```groovy
2929
dependencies {
30-
implementation 'com.github.ShiftHackZ:ImagePicker:v1.0'
30+
implementation 'com.github.ShiftHackZ:ImagePicker:v2.0'
3131
}
32-
</pre>
33-
34-
3. In order to receive images, implement ImagePickerCallback in your Fragment/Activity or as object:
35-
36-
<pre>
37-
public class MainActivity extends AppCompatActivity implements ImagePickerCallback {
38-
...
39-
@Override
40-
public void onImagesSelected(List<File> files) {
41-
// Do whatever you want with list of files
42-
for (int i = 0; i < files.size(); i++) {
43-
// As example you can process each file inside for-cycle
44-
}
45-
}
46-
...
32+
```
33+
34+
3. Create file `provider_path.xml` in `res/xml` folder:
35+
36+
```xml
37+
<?xml version="1.0" encoding="utf-8"?>
38+
<paths>
39+
<external-path name="media" path="." />
40+
<external-path name="external_files" path="."/>
41+
</paths>
42+
```
43+
44+
4. In your `AndroidManifest.xml` add the file provider inside the `<application` tag:
45+
46+
```xml
47+
<provider
48+
android:name="androidx.core.content.FileProvider"
49+
android:authorities="${applicationId}.provider"
50+
android:exported="false"
51+
android:grantUriPermissions="true">
52+
<meta-data
53+
android:name="android.support.FILE_PROVIDER_PATHS"
54+
android:resource="@xml/provider_path" />
55+
</provider>
56+
```
57+
58+
5. In order to receive images, implement `ImagePickerCallback` in your Fragment/Activity or as object:
59+
60+
```kotlin
61+
class MainActivity : AppCompatActivity(), ImagePickerCallback {
62+
63+
override fun onImagePickerResult(result: PickedResult) {
64+
when (result) {
65+
PickedResult.Empty -> {
66+
// No file was selected, noting to do
67+
}
68+
is PickedResult.Error -> {
69+
val throwable = result.throwable
70+
// Some error happened, handle this throwable
71+
}
72+
is PickedResult.Multiple -> {
73+
val pickedImages = result.images
74+
val files = pickedImages.map { it.file }
75+
// Selected multiple images, do whatever you want with files
76+
}
77+
is PickedResult.Single -> {
78+
val pickedImage = result.image
79+
val file = pickedImage.file
80+
// Selected one image, do whatever you want with file
81+
}
82+
}
83+
}
4784
}
48-
</pre>
85+
```
4986

50-
4. Create an instance of ImagePicker using ImagePicker.Builder(), which require 2 mandatory params: current Activity and ImagePickerCallback:
87+
6. Create an instance of ImagePicker using ImagePicker.Builder(), which require 2 mandatory params: current Activity and ImagePickerCallback:
5188

52-
<pre>
53-
ImagePicker imagePicker = new ImagePicker.Builder(activity, callback)
54-
.useGallery(true)
55-
.useCamera(true)
56-
.useMultiSelection(true)
57-
.build();
58-
</pre>
89+
```kotlin
90+
val imagePicker = ImagePicker.Builder(this.packageName + ".provider", this)
91+
.useGallery(true) // Use gallery picker if true
92+
.useCamera(true) // Use camera picker if true
93+
.multipleSelection() // Allow multiple selection in gallery picker
94+
.minimumSelectionCount(2) // Defines min count of GallerySelector.CUSTOM multiple selection gallery picker
95+
.maximumSelectionCount(3) // Defines max count of GallerySelector.CUSTOM multiple selection gallery picker
96+
.gallerySelector(GallerySelector.CUSTOM) // Available values: GallerySelector.NATIVE, GallerySelector.CUSTOM
97+
.build()
98+
```
5999

60-
List of Builder methods:
61-
- useGallery(boolean) // Pass 'true' if you want to enable gallery picker
62-
- useMultiSelection(boolean) // Pass 'true' if you need gallery picker to support multiple photo selection
63-
- useCamera(boolean) // Pass 'true' if you want to enable camera picker
64100

65-
5. Finally, launch your ImagePicker:
101+
7. Finally, launch your ImagePicker:
66102

67-
<pre>
68-
imagePicker.start();
69-
</pre>
103+
```kotlin
104+
imagePicker.launch(context)
105+
```
70106

71107
## Credits
72108
- Developer: Dmitriy Moroz

app/build.gradle

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
plugins {
22
id 'com.android.application'
3+
id 'kotlin-android'
4+
id 'kotlin-kapt'
35
}
46

57
android {
6-
compileSdkVersion 31
7-
buildToolsVersion "30.0.3"
8+
compileSdkVersion 33
89

910
defaultConfig {
1011
applicationId "com.shz.imagepicker.imagepickerapp"
1112
minSdkVersion 16
12-
targetSdkVersion 31
13+
targetSdkVersion 33
1314
versionCode 1
1415
versionName "1.0"
1516

@@ -21,6 +22,9 @@ android {
2122
minifyEnabled false
2223
}
2324
}
25+
buildFeatures {
26+
dataBinding true
27+
}
2428
compileOptions {
2529
sourceCompatibility JavaVersion.VERSION_1_8
2630
targetCompatibility JavaVersion.VERSION_1_8
@@ -29,9 +33,9 @@ android {
2933

3034
dependencies {
3135
implementation project(':imagepicker')
32-
implementation 'com.github.bumptech.glide:glide:4.12.0'
33-
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
34-
implementation 'androidx.appcompat:appcompat:1.3.1'
35-
implementation 'com.google.android.material:material:1.4.0'
36-
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
36+
implementation 'com.github.bumptech.glide:glide:4.13.2'
37+
kapt 'com.github.bumptech.glide:compiler:4.13.2'
38+
implementation 'androidx.appcompat:appcompat:1.5.1'
39+
implementation 'com.google.android.material:material:1.6.1'
40+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
3741
}

app/src/main/AndroidManifest.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
package="com.shz.imagepicker.imagepickerapp">
44

55
<uses-permission android:name="android.permission.CAMERA" />
6+
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
7+
8+
<uses-permission
9+
android:name="android.permission.READ_EXTERNAL_STORAGE"
10+
android:maxSdkVersion="32" />
11+
<uses-permission
12+
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
13+
android:maxSdkVersion="29" />
614

715
<application
816
android:allowBackup="false"
@@ -20,6 +28,16 @@
2028
<category android:name="android.intent.category.LAUNCHER" />
2129
</intent-filter>
2230
</activity>
31+
32+
<provider
33+
android:name="androidx.core.content.FileProvider"
34+
android:authorities="${applicationId}.provider"
35+
android:exported="false"
36+
android:grantUriPermissions="true">
37+
<meta-data
38+
android:name="android.support.FILE_PROVIDER_PATHS"
39+
android:resource="@xml/provider_path" />
40+
</provider>
2341
</application>
2442

2543
</manifest>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.shz.imagepicker.imagepickerapp
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.recyclerview.widget.DiffUtil
6+
import androidx.recyclerview.widget.ListAdapter
7+
import androidx.recyclerview.widget.RecyclerView
8+
import com.bumptech.glide.Glide
9+
import com.shz.imagepicker.imagepicker.model.PickedImage
10+
import com.shz.imagepicker.imagepickerapp.databinding.ItemDemoImageBinding
11+
12+
class ImagesDemoAdapter : ListAdapter<PickedImage, ImagesDemoAdapter.ViewHolder>(diff) {
13+
14+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
15+
ItemDemoImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
16+
)
17+
18+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
19+
holder.bind(getItem(position))
20+
}
21+
22+
inner class ViewHolder(
23+
private val binding: ItemDemoImageBinding,
24+
) : RecyclerView.ViewHolder(binding.root) {
25+
26+
fun bind(item: PickedImage) {
27+
Glide.with(binding.image)
28+
.load(item.file)
29+
.centerCrop()
30+
.into(binding.image)
31+
}
32+
}
33+
34+
companion object {
35+
private val diff = object : DiffUtil.ItemCallback<PickedImage>() {
36+
override fun areItemsTheSame(
37+
oldItem: PickedImage,
38+
newItem: PickedImage
39+
): Boolean = oldItem == newItem
40+
41+
override fun areContentsTheSame(
42+
oldItem: PickedImage,
43+
newItem: PickedImage
44+
): Boolean = oldItem == newItem
45+
}
46+
}
47+
}

app/src/main/java/com/shz/imagepicker/imagepickerapp/MainActivity.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)