https://github.com/wrathchaos/mediapickerlib
Kotlin based media picker library, to pick multiple images and/or vidoes from built-in gallery. Easy to implement and use :)
https://github.com/wrathchaos/mediapickerlib
Last synced: 5 months ago
JSON representation
Kotlin based media picker library, to pick multiple images and/or vidoes from built-in gallery. Easy to implement and use :)
- Host: GitHub
- URL: https://github.com/wrathchaos/mediapickerlib
- Owner: WrathChaos
- License: mit
- Created: 2018-02-27T08:40:54.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-03T11:13:40.000Z (almost 6 years ago)
- Last Synced: 2025-03-31T08:12:04.689Z (6 months ago)
- Language: Kotlin
- Homepage: https://www.freakycoder.com
- Size: 5.43 MB
- Stars: 49
- Watchers: 3
- Forks: 16
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MediaPickerLib
### Kotlin based media picker library, to pick multiple images and/or vidoes from built-in gallery. Easy to implement and use :)
![]()
## Setup
Add this on your module:app build.gradle
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
## Gradle```kotlin
implementation 'com.github.WrathChaos:MediaPickerLib:0.2.0'
```## Usage
##### This is the request code to handle and get the picked images/videos
```kotlin
private val OPEN_MEDIA_PICKER = 1 // Request code
```##### You need to import Gallery from MediaPickerLib and send mandatory intents to work
```kotlin
val intent = Intent(this, Gallery::class.java)
// Set the title for toolbar
intent.putExtra("title", "Select media")
// Mode 1 for both images and videos selection, 2 for images only and 3 for videos!
intent.putExtra("mode", 1)
intent.putExtra("maxSelection", 3) // Optional
startActivityForResult(intent, OPEN_MEDIA_PICKER)
```##### Receive what you picked here: This is an example from sample project, you can handle whatever you want with the path :)
```kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Check which request we're responding to
if (requestCode == OPEN_MEDIA_PICKER) {
// Make sure the request was successful
if (resultCode == Activity.RESULT_OK && data != null) {
val selectionResult = data.getStringArrayListExtra("result")
selectionResult.forEach {
try {
Log.d("MyApp", "Image Path : " + it)
val uriFromPath = Uri.fromFile(File(it))
Log.d("MyApp", "Image URI : " + uriFromPath)
// Convert URI to Bitmap
val bm = BitmapFactory.decodeStream(
contentResolver.openInputStream(uriFromPath))
image.setImageBitmap(bm)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
}
}
```## Full Working Example
If you need more spesific example, please look at the [Example](https://github.com/WrathChaos/MediaPickerLib/tree/master/app)```kotlin
package com.example.myapplicationimport android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import com.coursion.freakycoder.mediapicker.galleries.Gallery
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import java.io.File
import java.io.FileNotFoundException
import android.provider.MediaStore
import java.io.ByteArrayOutputStreamclass MainActivity : AppCompatActivity() {
private val OPEN_MEDIA_PICKER = 1 // Request code
private val MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 100 // Request code for read external storageoverride fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)setButtonTint(fab, ContextCompat.getColorStateList(applicationContext, R.color.fabColor)!!)
fab.setOnClickListener { view ->
if (!permissionIfNeeded()) {
val intent = Intent(this, Gallery::class.java)
// Set the title
intent.putExtra("title", "Select media")
// Mode 1 for both images and videos selection, 2 for images only and 3 for videos!
intent.putExtra("mode", 1)
intent.putExtra("maxSelection", 3) // Optional
intent.putExtra("tabBarHidden", true) //Optional - default value is false
startActivityForResult(intent, OPEN_MEDIA_PICKER)
}
}
}fun setButtonTint(button: FloatingActionButton, tint: ColorStateList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
button.backgroundTintList = tint
} else {
ViewCompat.setBackgroundTintList(button, tint)
}
}private fun permissionIfNeeded(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Explain to the user why we need to read the contacts
}requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE)
return true
}
}
return false
}override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Check which request we're responding to
if (requestCode == OPEN_MEDIA_PICKER) {
// Make sure the request was successful
if (resultCode == Activity.RESULT_OK && data != null) {
val selectionResult = data.getStringArrayListExtra("result")
selectionResult.forEach {
try {
Log.d("MyApp", "Image Path : " + it)
val uriFromPath = Uri.fromFile(File(it))
Log.d("MyApp", "Image URI : " + uriFromPath)
// Convert URI to Bitmap
val bm = BitmapFactory.decodeStream(
contentResolver.openInputStream(uriFromPath))
val uri = getImageUriFromBitmap(applicationContext, bm)
Log.d("MyApp", "URI: " + uri)
image.setImageBitmap(bm)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
}
}
}fun getImageUriFromBitmap(context: Context, bitmap: Bitmap): Uri{
val bytes = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
val path = MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, "Title", null)
return Uri.parse(path.toString())
}
}```
## Customization
#### You can customize the color of the library (More customization features is coming soon)
Title and back's button color
```xml
#ffffff
```
Unselected image and video's tab title
```xml
#afafaf
```
Selected image and video's tab title
```xml
#ffffff
```
Gallery's fab button color
```xml
#931931
```
## Fork
This is original a fork from [multiple-media-picker](https://github.com/erikagtierrez/multiple-media-picker) | Currently not working
* Re-written on Kotlin
* Added new features
* Tablet support
* Fixed bugs#### Thanks for inspiration [Erikagtierrez](https://github.com/erikagtierrez) :)
## Author
FreakyCoder, kurayogun@gmail.com
## License
MediaPickerLib is available under the MIT license. See the LICENSE file for more info.