Skip to content

Commit

Permalink
Replace InlineActivityResult with ActivityResultContract
Browse files Browse the repository at this point in the history
Replace InlineActivityResult with ActivityResultContract

#32 #78
  • Loading branch information
Dhaval2404 committed May 4, 2020
1 parent 2a558b3 commit 071dccf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2019-2020, Dhaval Patel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
45 changes: 21 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
[![API](https://img.shields.io/badge/API-19%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=19)
![Language](https://img.shields.io/badge/language-Kotlin-orange.svg)
[![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-ImagePicker-green.svg?style=flat )]( https://android-arsenal.com/details/1/7510 )
[![ktlint](https://img.shields.io/badge/code%20style-%E2%9D%A4-FF4081.svg)](https://ktlint.github.io/)
[![PRWelcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Dhaval2404/ImagePicker)
[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/Dhaval2404)
[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=102)](https://opensource.org/licenses/Apache-2.0)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/Dhaval2404/ImagePicker/blob/master/LICENSE)
[![Twitter](https://img.shields.io/twitter/url/https/github.com/Dhaval2404/ImagePicker.svg?style=social)](https://twitter.com/intent/tweet?text=Check+out+an+ImagePicker+library+to+Pick+an+image+from+the+Gallery+or+Capture+an+image+with+Camera.+https%3A%2F%2Fgithub.com%2FDhaval2404%2FImagePicker+%40dhaval2404+%23Android+%23Kotlin+%23AndroidDev)

<div align="center">
Expand All @@ -23,7 +25,7 @@ Easy to use and configurable library to **Pick an image from the Gallery or Capt
Almost 90% of the app that I have developed has an Image upload feature. Along with the image selection, Sometimes I needed a crop feature for profile image for that I've used uCrop. Most of the time I need to compress the image as the image captured from the camera is more than 5-10 MBs and sometimes we have a requirement to upload images with specific resolution/size, in that case, image compress is the way to go option. To simplify the image pick/capture option I have created ImagePicker library. I hope it will be useful to all.

# 🐱‍🏍Features:

* Pick Gallery Image
* Pick Image from Google Drive
* Capture Camera Image
Expand Down Expand Up @@ -56,17 +58,12 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
```groovy
implementation 'com.github.dhaval2404:imagepicker:1.7.1'
```

**If you are yet to Migrate on AndroidX, Use support build artifact:**
```groovy
implementation 'com.github.dhaval2404:imagepicker-support:1.7.1'
```
**If you want to get the activity result inline in a modern way (lambda) install [InlineActivityResult](https://github.com/florent37/InlineActivityResult) library:**
```groovy
implementation 'com.github.florent37:inline-activity-result-kotlin:1.0.1'
```
2. <span style="color:red">**If you target Android 10 or higher(targetSdkVersion >= 29)**</span>, set the value of ``requestLegacyExternalStorage`` to true in your app's manifest file:
```xml
Expand All @@ -82,29 +79,29 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
3. The ImagePicker configuration is created using the builder pattern.
**Kotlin**
```kotlin
ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
```
**Java**
```kotlin
ImagePicker.Companion.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.start()
```
4. Handling results
**Default method(Preferred way)**<br>
**Default method**<br>
Override `onActivityResult` method and handle ImagePicker result.
```kotlin
Expand All @@ -114,12 +111,12 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
//Image Uri will not be null for RESULT_OK
val fileUri = data?.data
imgProfile.setImageURI(fileUri)
//You can get File object from intent
val file:File = ImagePicker.getFile(data)
//You can also get File Path from intent
val filePath:String = ImagePicker.getFilePath(data)
val filePath:String = ImagePicker.getFilePath(data)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
} else {
Expand All @@ -128,8 +125,8 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
}
```
**Inline method (with InlineActivityResult library, Only Works with FragmentActivity and AppCompatActivity) (Not to be used with crop. See [#32](https://github.com/Dhaval2404/ImagePicker/issues/32))**
**Inline method (with ActivityResultContract, Only Works with FragmentActivity and AppCompatActivity)**
```kotlin
ImagePicker.with(this)
.compress(1024) //Final image size will be less than 1 MB(Optional)
Expand All @@ -139,10 +136,10 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
//Image Uri will not be null for RESULT_OK
val fileUri = data?.data
imgProfile.setImageURI(fileUri)
//You can get File object from intent
val file:File = ImagePicker.getFile(data)
//You can also get File Path from intent
val filePath:String = ImagePicker.getFilePath(data)
} else if (resultCode == ImagePicker.RESULT_ERROR) {
Expand Down Expand Up @@ -171,7 +168,7 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
.start()
```
* Crop image
```kotlin
ImagePicker.with(this)
.crop() //Crop image and let user choose aspect ratio.
Expand Down Expand Up @@ -300,7 +297,7 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w
* Added Support for Inline Activity Result(Special Thanks to [soareseneves](https://github.com/soareseneves))
* Fixed issue [#6](https://github.com/Dhaval2404/ImagePicker/issues/6)
### Version: 1.1
* Optimized Compression Logic
Expand All @@ -321,7 +318,7 @@ We'll be really happy if you sent us links to your projects where you use our co
## License
Copyright 2019, The Android Open Source Project
Copyright 2019-2020, Dhaval Patel
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
6 changes: 3 additions & 3 deletions imagepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation "androidx.exifinterface:exifinterface:1.1.0"

implementation 'androidx.activity:activity:1.2.0-alpha04'
implementation 'androidx.fragment:fragment:1.3.0-alpha04'

//More Info: https://github.com/Yalantis/uCrop
implementation 'com.github.yalantis:ucrop:2.2.4'

//More Info: https://github.com/florent37/InlineActivityResult
compileOnly 'com.github.florent37:inline-activity-result-kotlin:1.0.3'

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:core:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.github.dhaval2404.imagepicker
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.github.dhaval2404.imagepicker.constant.ImageProvider
import com.github.dhaval2404.imagepicker.listener.ResultListener
import com.github.dhaval2404.imagepicker.util.DialogHelper
import com.github.florent37.inlineactivityresult.kotlin.startForResult
import com.github.dhaval2404.imagepicker.util.startActivityForResult
import java.io.File

/**
Expand Down Expand Up @@ -338,29 +337,15 @@ open class ImagePicker {
* Start ImagePickerActivity with given Argument
*/
private fun startActivity(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) {

try {
val intent = Intent(activity, ImagePickerActivity::class.java)
intent.putExtras(getBundle())
if (fragment != null) {

fragment?.startForResult(intent) { result ->
completionHandler?.invoke(result.resultCode, result.data)
}?.onFailed { result ->
completionHandler?.invoke(result.resultCode, result.data)
}
} else {
(activity as AppCompatActivity).startForResult(intent) { result ->
completionHandler?.invoke(result.resultCode, result.data)
}.onFailed { result ->
completionHandler?.invoke(result.resultCode, result.data)
}
val intent = Intent(activity, ImagePickerActivity::class.java)
intent.putExtras(getBundle())
if (fragment != null) {
fragment?.startActivityForResult(intent) { resultCode, data ->
completionHandler?.invoke(resultCode, data)
}
} catch (e: Exception) {
if (e is ClassNotFoundException) {
Toast.makeText(if (fragment != null) fragment!!.context else activity, "InlineActivityResult library not installed falling back to default method, please install " +
"it from https://github.com/florent37/InlineActivityResult if you want to get inline activity results.", Toast.LENGTH_LONG).show()
startActivity(REQUEST_CODE)
} else {
(activity as AppCompatActivity).startActivityForResult(intent) { resultCode, data ->
completionHandler?.invoke(resultCode, data)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.dhaval2404.imagepicker.util

import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

/**
* Inline Activity Result
*
* @author Dhaval Patel
* @version 2.0
* @since 29 April 2020
*/
fun AppCompatActivity.startActivityForResult(
intent: Intent,
listener: (Int, Intent?) -> Unit?
) {
registerForActivityResult(StartActivityForResult()) { result: ActivityResult ->
listener.invoke(result.resultCode, result.data)
}.launch(intent)
}

fun Fragment.startActivityForResult(intent: Intent, listener: (Int, Intent?) -> Unit?) {
registerForActivityResult(StartActivityForResult()) { result: ActivityResult ->
listener.invoke(result.resultCode, result.data)
}.launch(intent)
}

0 comments on commit 071dccf

Please sign in to comment.