ImageUtils created with helper functions.

This commit is contained in:
onurays 2020-03-24 11:31:27 +03:00
parent 2651f82337
commit 727d86236b
10 changed files with 75 additions and 91 deletions

View File

@ -3,7 +3,7 @@
<application>
<provider
android:name=".MultiPickerFileProvider"
android:name=".provider.MultiPickerFileProvider"
android:authorities="${applicationId}.multipicker.fileprovider"
android:exported="false"
android:grantUriPermissions="true">

View File

@ -27,14 +27,6 @@ import im.vector.riotx.multipicker.entity.MultiPickerAudioType
class AudioPicker(override val requestCode: Int) : Picker<MultiPickerAudioType>(requestCode) {
override fun startWith(activity: Activity) {
activity.startActivityForResult(createIntent(), requestCode)
}
override fun startWith(fragment: Fragment) {
fragment.startActivityForResult(createIntent(), requestCode)
}
override fun getSelectedFiles(context: Context, requestCode: Int, resultCode: Int, data: Intent?): List<MultiPickerAudioType> {
if (requestCode != this.requestCode && resultCode != Activity.RESULT_OK) {
return emptyList()
@ -84,7 +76,7 @@ class AudioPicker(override val requestCode: Int) : Picker<MultiPickerAudioType>(
return audioList
}
private fun createIntent(): Intent {
override fun createIntent(): Intent {
return Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, !single)

View File

@ -28,6 +28,7 @@ import androidx.core.content.FileProvider
import androidx.exifinterface.media.ExifInterface
import androidx.fragment.app.Fragment
import im.vector.riotx.multipicker.entity.MultiPickerImageType
import im.vector.riotx.multipicker.utils.ImageUtils
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
@ -74,25 +75,8 @@ class CameraPicker(val requestCode: Int) {
val name = cursor.getString(nameColumn)
val size = cursor.getLong(sizeColumn)
var orientation = 0
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, photoUri))
} else {
context.contentResolver.openInputStream(photoUri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
}
}
context.contentResolver.openInputStream(photoUri)?.use { inputStream ->
try {
ExifInterface(inputStream).let {
orientation = it.rotationDegrees
}
} catch (e: Exception) {
e.printStackTrace()
}
}
val bitmap = ImageUtils.getBitmap(context, photoUri)
val orientation = ImageUtils.getOrientation(context, photoUri)
return MultiPickerImageType(
name,

View File

@ -26,14 +26,6 @@ import im.vector.riotx.multipicker.entity.MultiPickerContactType
class ContactPicker(override val requestCode: Int) : Picker<MultiPickerContactType>(requestCode) {
override fun startWith(activity: Activity) {
activity.startActivityForResult(createIntent(), requestCode)
}
override fun startWith(fragment: Fragment) {
fragment.startActivityForResult(createIntent(), requestCode)
}
override fun getSelectedFiles(context: Context, requestCode: Int, resultCode: Int, data: Intent?): List<MultiPickerContactType> {
if (requestCode != this.requestCode && resultCode != Activity.RESULT_OK) {
return emptyList()
@ -126,7 +118,7 @@ class ContactPicker(override val requestCode: Int) : Picker<MultiPickerContactTy
}
}
private fun createIntent(): Intent {
override fun createIntent(): Intent {
return Intent(Intent.ACTION_PICK).apply {
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, !single)
type = ContactsContract.Contacts.CONTENT_TYPE

View File

@ -19,21 +19,11 @@ package im.vector.riotx.multipicker
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.OpenableColumns
import androidx.fragment.app.Fragment
import im.vector.riotx.multipicker.entity.MultiPickerFileType
class FilePicker(override val requestCode: Int) : Picker<MultiPickerFileType>(requestCode) {
override fun startWith(activity: Activity) {
activity.startActivityForResult(createIntent(), requestCode)
}
override fun startWith(fragment: Fragment) {
fragment.startActivityForResult(createIntent(), requestCode)
}
override fun getSelectedFiles(context: Context, requestCode: Int, resultCode: Int, data: Intent?): List<MultiPickerFileType> {
if (requestCode != this.requestCode && resultCode != Activity.RESULT_OK) {
return emptyList()
@ -64,7 +54,7 @@ class FilePicker(override val requestCode: Int) : Picker<MultiPickerFileType>(re
return fileList
}
private fun createIntent(): Intent {
override fun createIntent(): Intent {
return Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, !single)

View File

@ -27,17 +27,10 @@ import android.provider.MediaStore
import androidx.exifinterface.media.ExifInterface
import androidx.fragment.app.Fragment
import im.vector.riotx.multipicker.entity.MultiPickerImageType
import im.vector.riotx.multipicker.utils.ImageUtils
class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(requestCode) {
override fun startWith(activity: Activity) {
activity.startActivityForResult(createIntent(), requestCode)
}
override fun startWith(fragment: Fragment) {
fragment.startActivityForResult(createIntent(), requestCode)
}
override fun getSelectedFiles(context: Context, requestCode: Int, resultCode: Int, data: Intent?): List<MultiPickerImageType> {
if (requestCode != this.requestCode && resultCode != Activity.RESULT_OK) {
return emptyList()
@ -65,25 +58,8 @@ class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(
val name = cursor.getString(nameColumn)
val size = cursor.getLong(sizeColumn)
var orientation = 0
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, selectedUri))
} else {
context.contentResolver.openInputStream(selectedUri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
}
}
context.contentResolver.openInputStream(selectedUri)?.use { inputStream ->
try {
ExifInterface(inputStream).let {
orientation = it.rotationDegrees
}
} catch (e: Exception) {
e.printStackTrace()
}
}
val bitmap = ImageUtils.getBitmap(context, selectedUri)
val orientation = ImageUtils.getOrientation(context, selectedUri)
imageList.add(
MultiPickerImageType(
@ -102,7 +78,7 @@ class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(
return imageList
}
private fun createIntent(): Intent {
override fun createIntent(): Intent {
return Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, !single)

View File

@ -26,10 +26,6 @@ abstract class Picker<T>(open val requestCode: Int) {
protected var single = false
abstract fun startWith(activity: Activity)
abstract fun startWith(fragment: Fragment)
open fun startWithExpectingFile(activity: Activity): Uri? = null
open fun startWithExpectingFile(fragment: Fragment): Uri? = null
@ -45,6 +41,16 @@ abstract class Picker<T>(open val requestCode: Int) {
return this
}
abstract fun createIntent(): Intent
fun startWith(activity: Activity) {
activity.startActivityForResult(createIntent(), requestCode)
}
fun startWith(fragment: Fragment) {
fragment.startActivityForResult(createIntent(), requestCode)
}
protected fun getSelectedUriList(data: Intent?): List<Uri> {
val selectedUriList = mutableListOf<Uri>()
val dataUri = data?.data

View File

@ -27,14 +27,6 @@ import im.vector.riotx.multipicker.entity.MultiPickerVideoType
class VideoPicker(override val requestCode: Int) : Picker<MultiPickerVideoType>(requestCode) {
override fun startWith(activity: Activity) {
activity.startActivityForResult(createIntent(), requestCode)
}
override fun startWith(fragment: Fragment) {
fragment.startActivityForResult(createIntent(), requestCode)
}
override fun getSelectedFiles(context: Context, requestCode: Int, resultCode: Int, data: Intent?): List<MultiPickerVideoType> {
if (requestCode != this.requestCode && resultCode != Activity.RESULT_OK) {
return emptyList()
@ -93,7 +85,7 @@ class VideoPicker(override val requestCode: Int) : Picker<MultiPickerVideoType>(
return videoList
}
private fun createIntent(): Intent {
override fun createIntent(): Intent {
return Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, !single)

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package im.vector.riotx.multipicker
package im.vector.riotx.multipicker.provider
import androidx.core.content.FileProvider

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.multipicker.utils
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import androidx.exifinterface.media.ExifInterface
object ImageUtils {
fun getBitmap(context: Context, uri: Uri): Bitmap? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, uri))
} else {
context.contentResolver.openInputStream(uri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
}
}
}
fun getOrientation(context: Context, uri: Uri): Int {
var orientation = 0
context.contentResolver.openInputStream(uri)?.use { inputStream ->
try {
ExifInterface(inputStream).let {
orientation = it.rotationDegrees
}
} catch (e: Exception) {
e.printStackTrace()
}
}
return orientation
}
}