Skip to content

Commit

Permalink
Introduce new v2.0 (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ShiftHackZ authored Dec 13, 2022
1 parent d0901dd commit a3a8ca7
Show file tree
Hide file tree
Showing 57 changed files with 2,236 additions and 940 deletions.
110 changes: 73 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,100 @@ Android library that can be used as quick solution to ImagePicker feature implem
- Camera photo picker
- Gallery single photo picker
- Gallery multiple photo picker
- Custom gallery picker, supports multiple selection (for old non-AOSP Android ROMs that does not support multiple selection intent)

## Implementation

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

<pre>
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
</pre>
```

2. In app-level gradle add new implementation:

<pre>
```groovy
dependencies {
implementation 'com.github.ShiftHackZ:ImagePicker:v1.0'
implementation 'com.github.ShiftHackZ:ImagePicker:v2.0'
}
</pre>

3. In order to receive images, implement ImagePickerCallback in your Fragment/Activity or as object:

<pre>
public class MainActivity extends AppCompatActivity implements ImagePickerCallback {
...
@Override
public void onImagesSelected(List<File> files) {
// Do whatever you want with list of files
for (int i = 0; i < files.size(); i++) {
// As example you can process each file inside for-cycle
}
}
...
```

3. Create file `provider_path.xml` in `res/xml` folder:

```xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="media" path="." />
<external-path name="external_files" path="."/>
</paths>
```

4. In your `AndroidManifest.xml` add the file provider inside the `<application` tag:

```xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path" />
</provider>
```

5. In order to receive images, implement `ImagePickerCallback` in your Fragment/Activity or as object:

```kotlin
class MainActivity : AppCompatActivity(), ImagePickerCallback {

override fun onImagePickerResult(result: PickedResult) {
when (result) {
PickedResult.Empty -> {
// No file was selected, noting to do
}
is PickedResult.Error -> {
val throwable = result.throwable
// Some error happened, handle this throwable
}
is PickedResult.Multiple -> {
val pickedImages = result.images
val files = pickedImages.map { it.file }
// Selected multiple images, do whatever you want with files
}
is PickedResult.Single -> {
val pickedImage = result.image
val file = pickedImage.file
// Selected one image, do whatever you want with file
}
}
}
}
</pre>
```

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

<pre>
ImagePicker imagePicker = new ImagePicker.Builder(activity, callback)
.useGallery(true)
.useCamera(true)
.useMultiSelection(true)
.build();
</pre>
```kotlin
val imagePicker = ImagePicker.Builder(this.packageName + ".provider", this)
.useGallery(true) // Use gallery picker if true
.useCamera(true) // Use camera picker if true
.multipleSelection() // Allow multiple selection in gallery picker
.minimumSelectionCount(2) // Defines min count of GallerySelector.CUSTOM multiple selection gallery picker
.maximumSelectionCount(3) // Defines max count of GallerySelector.CUSTOM multiple selection gallery picker
.gallerySelector(GallerySelector.CUSTOM) // Available values: GallerySelector.NATIVE, GallerySelector.CUSTOM
.build()
```

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

5. Finally, launch your ImagePicker:
7. Finally, launch your ImagePicker:

<pre>
imagePicker.start();
</pre>
```kotlin
imagePicker.launch(context)
```

## Credits
- Developer: Dmitriy Moroz
Expand Down
20 changes: 12 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
compileSdkVersion 33

defaultConfig {
applicationId "com.shz.imagepicker.imagepickerapp"
minSdkVersion 16
targetSdkVersion 31
targetSdkVersion 33
versionCode 1
versionName "1.0"

Expand All @@ -21,6 +22,9 @@ android {
minifyEnabled false
}
}
buildFeatures {
dataBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand All @@ -29,9 +33,9 @@ android {

dependencies {
implementation project(':imagepicker')
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'com.github.bumptech.glide:glide:4.13.2'
kapt 'com.github.bumptech.glide:compiler:4.13.2'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
18 changes: 18 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
package="com.shz.imagepicker.imagepickerapp">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />

<application
android:allowBackup="false"
Expand All @@ -20,6 +28,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path" />
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.shz.imagepicker.imagepickerapp

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.shz.imagepicker.imagepicker.model.PickedImage
import com.shz.imagepicker.imagepickerapp.databinding.ItemDemoImageBinding

class ImagesDemoAdapter : ListAdapter<PickedImage, ImagesDemoAdapter.ViewHolder>(diff) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
ItemDemoImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
}

inner class ViewHolder(
private val binding: ItemDemoImageBinding,
) : RecyclerView.ViewHolder(binding.root) {

fun bind(item: PickedImage) {
Glide.with(binding.image)
.load(item.file)
.centerCrop()
.into(binding.image)
}
}

companion object {
private val diff = object : DiffUtil.ItemCallback<PickedImage>() {
override fun areItemsTheSame(
oldItem: PickedImage,
newItem: PickedImage
): Boolean = oldItem == newItem

override fun areContentsTheSame(
oldItem: PickedImage,
newItem: PickedImage
): Boolean = oldItem == newItem
}
}
}
105 changes: 0 additions & 105 deletions app/src/main/java/com/shz/imagepicker/imagepickerapp/MainActivity.java

This file was deleted.

Loading

0 comments on commit a3a8ca7

Please sign in to comment.