From e663d76edf2e0c64cbc0a85364549749e7727f50 Mon Sep 17 00:00:00 2001 From: hongbeomi Date: Fri, 5 Nov 2021 02:08:05 +0900 Subject: [PATCH 1/3] =?UTF-8?q?28=20=EB=B2=84=EC=A0=84=20=EC=9D=B4?= =?UTF-8?q?=ED=95=98=EC=9D=98=20=EA=B8=B0=EA=B8=B0=EC=97=90=EC=84=9C=20WRI?= =?UTF-8?q?TE=5FEXTERNAL=5FSTORAGE=20=EA=B6=8C=ED=95=9C=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD,=20?= =?UTF-8?q?=EB=B6=84=EA=B8=B0=EC=B2=98=EB=A6=AC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tedimagepicker/src/main/AndroidManifest.xml | 6 +- .../builder/TedImagePickerBaseBuilder.kt | 19 +++++- .../gun0912/tedimagepicker/util/MediaUtil.kt | 60 +++++-------------- 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/tedimagepicker/src/main/AndroidManifest.xml b/tedimagepicker/src/main/AndroidManifest.xml index 812e786..6032090 100644 --- a/tedimagepicker/src/main/AndroidManifest.xml +++ b/tedimagepicker/src/main/AndroidManifest.xml @@ -1,7 +1,11 @@ - + diff --git a/tedimagepicker/src/main/java/gun0912/tedimagepicker/builder/TedImagePickerBaseBuilder.kt b/tedimagepicker/src/main/java/gun0912/tedimagepicker/builder/TedImagePickerBaseBuilder.kt index 8ecddad..4159534 100644 --- a/tedimagepicker/src/main/java/gun0912/tedimagepicker/builder/TedImagePickerBaseBuilder.kt +++ b/tedimagepicker/src/main/java/gun0912/tedimagepicker/builder/TedImagePickerBaseBuilder.kt @@ -7,11 +7,13 @@ import android.content.Context import android.content.Intent import android.content.pm.ActivityInfo import android.net.Uri +import android.os.Build import android.os.Parcelable import androidx.annotation.AnimRes import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import androidx.annotation.StringRes +import com.gun0912.tedpermission.TedPermissionResult import com.gun0912.tedpermission.rx2.TedPermission import com.tedpark.tedonactivityresult.rx2.TedRxOnActivityResult import gun0912.tedimagepicker.R @@ -25,6 +27,7 @@ import gun0912.tedimagepicker.builder.type.ButtonGravity import gun0912.tedimagepicker.builder.type.MediaType import gun0912.tedimagepicker.builder.type.SelectType import gun0912.tedimagepicker.util.ToastUtil +import io.reactivex.Single import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.Parcelize @@ -102,9 +105,19 @@ open class TedImagePickerBaseBuilder>( }, { throwable -> onErrorListener?.onError(throwable) }) } - private fun checkPermission(context: Context) = TedPermission.create() - .setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE) - .request() + private fun checkPermission(context: Context): Single { + val permissions = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + arrayOf( + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + ) + } else { + arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE) + } + return TedPermission.create() + .setPermissions(*permissions) + .request() + } private fun startActivity(context: Context) { TedImagePickerActivity.getIntent(context, this) diff --git a/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt b/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt index b68fd4f..16fd666 100644 --- a/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt +++ b/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt @@ -7,12 +7,9 @@ import android.content.pm.PackageManager import android.media.MediaScannerConnection import android.net.Uri import android.os.Build -import android.os.Environment import android.provider.MediaStore -import androidx.core.content.FileProvider import gun0912.tedimagepicker.builder.type.MediaType import io.reactivex.Completable -import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -46,53 +43,24 @@ internal class MediaUtil { SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(Date()) val fileName = "${mediaType}_$timeStamp" - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - val directoryName = - if (savedDirectoryName != null) { - "${mediaType.savedDirectoryName}/$savedDirectoryName" - } else { - mediaType.savedDirectoryName - } - - val contentValues = ContentValues().apply { - put(MediaStore.MediaColumns.DISPLAY_NAME, fileName + mediaType.fileSuffix) - put(MediaStore.MediaColumns.MIME_TYPE, mediaType.mimeType) - put(MediaStore.MediaColumns.RELATIVE_PATH, directoryName) - } - val mediaUri = - context.contentResolver.insert(mediaType.externalContentUri, contentValues)!! - cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri) - cameraIntent to mediaUri - } else { - - val directoryName = savedDirectoryName ?: mediaType.savedDirectoryName - val directory = Environment.getExternalStoragePublicDirectory(directoryName) - if (!directory.exists()) { - directory.mkdir() + val directoryName = + if (savedDirectoryName != null) { + "${mediaType.savedDirectoryName}/$savedDirectoryName" + } else { + mediaType.savedDirectoryName } - val file = File.createTempFile(fileName, mediaType.fileSuffix, directory) - - val mediaUri = FileProvider.getUriForFile( - context, - context.applicationContext.packageName + ".provider", - file - ) - - val resolvedIntentActivities = context.packageManager - .queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY) - for (resolvedIntentInfo in resolvedIntentActivities) { - val packageName = resolvedIntentInfo.activityInfo.packageName - context.grantUriPermission( - packageName, - mediaUri, - Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION - ) + val contentValues = ContentValues().apply { + put(MediaStore.MediaColumns.DISPLAY_NAME, fileName + mediaType.fileSuffix) + put(MediaStore.MediaColumns.MIME_TYPE, mediaType.mimeType) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + put(MediaStore.MediaColumns.RELATIVE_PATH, directoryName) } - - cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri) - cameraIntent to Uri.fromFile(file) } + val mediaUri = + context.contentResolver.insert(mediaType.externalContentUri, contentValues)!! + cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri) + return cameraIntent to mediaUri } fun scanMedia(context: Context, uri: Uri): Completable { From e9adde0196484226df61b7cf79c4a3c443ed3140 Mon Sep 17 00:00:00 2001 From: hongbeomi Date: Fri, 5 Nov 2021 03:44:40 +0900 Subject: [PATCH 2/3] =?UTF-8?q?28=20=EB=B2=84=EC=A0=84=20=EC=9D=B4?= =?UTF-8?q?=ED=95=98=EC=9D=98=20=EA=B8=B0=EA=B8=B0=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EA=B0=A4=EB=9F=AC=EB=A6=AC=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=A0=9C=EB=8C=80=EB=A1=9C=20=EB=B6=88=EB=9F=AC=EC=98=A4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20query=20selection=20=EA=B0=92=20=EB=B6=84?= =?UTF-8?q?=EA=B8=B0=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gun0912/tedimagepicker/util/GalleryUtil.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/GalleryUtil.kt b/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/GalleryUtil.kt index d4093d5..c54d2ad 100644 --- a/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/GalleryUtil.kt +++ b/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/GalleryUtil.kt @@ -40,9 +40,18 @@ internal class GalleryUtil { } val sortOrder = "$INDEX_DATE_ADDED DESC" - val projection = - arrayOf(INDEX_MEDIA_ID, INDEX_MEDIA_URI, albumName, INDEX_DATE_ADDED) - val selection = MediaStore.Images.Media.SIZE + " > 0" + + val projection = arrayOf( + INDEX_MEDIA_ID, + INDEX_MEDIA_URI, + albumName, + INDEX_DATE_ADDED + ) + val selection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + MediaStore.Images.Media.SIZE + " > 0" + } else { + null + } val cursor = context.contentResolver.query(uri, projection, selection, null, sortOrder) val albumList: List = cursor?.let { From 8f12b3731703e82f2d98d9eb1dbb6f0ea47bbad5 Mon Sep 17 00:00:00 2001 From: ted Date: Wed, 19 Jan 2022 23:46:23 +0900 Subject: [PATCH 3/3] Revert commit this file by e663d76edf2e0c64cbc0a85364549749e7727f50 --- .../gun0912/tedimagepicker/util/MediaUtil.kt | 60 ++++++++++++++----- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt b/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt index 16fd666..b68fd4f 100644 --- a/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt +++ b/tedimagepicker/src/main/java/gun0912/tedimagepicker/util/MediaUtil.kt @@ -7,9 +7,12 @@ import android.content.pm.PackageManager import android.media.MediaScannerConnection import android.net.Uri import android.os.Build +import android.os.Environment import android.provider.MediaStore +import androidx.core.content.FileProvider import gun0912.tedimagepicker.builder.type.MediaType import io.reactivex.Completable +import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -43,24 +46,53 @@ internal class MediaUtil { SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(Date()) val fileName = "${mediaType}_$timeStamp" - val directoryName = - if (savedDirectoryName != null) { - "${mediaType.savedDirectoryName}/$savedDirectoryName" - } else { - mediaType.savedDirectoryName - } + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val directoryName = + if (savedDirectoryName != null) { + "${mediaType.savedDirectoryName}/$savedDirectoryName" + } else { + mediaType.savedDirectoryName + } - val contentValues = ContentValues().apply { - put(MediaStore.MediaColumns.DISPLAY_NAME, fileName + mediaType.fileSuffix) - put(MediaStore.MediaColumns.MIME_TYPE, mediaType.mimeType) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val contentValues = ContentValues().apply { + put(MediaStore.MediaColumns.DISPLAY_NAME, fileName + mediaType.fileSuffix) + put(MediaStore.MediaColumns.MIME_TYPE, mediaType.mimeType) put(MediaStore.MediaColumns.RELATIVE_PATH, directoryName) } + val mediaUri = + context.contentResolver.insert(mediaType.externalContentUri, contentValues)!! + cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri) + cameraIntent to mediaUri + } else { + + val directoryName = savedDirectoryName ?: mediaType.savedDirectoryName + val directory = Environment.getExternalStoragePublicDirectory(directoryName) + if (!directory.exists()) { + directory.mkdir() + } + + val file = File.createTempFile(fileName, mediaType.fileSuffix, directory) + + val mediaUri = FileProvider.getUriForFile( + context, + context.applicationContext.packageName + ".provider", + file + ) + + val resolvedIntentActivities = context.packageManager + .queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY) + for (resolvedIntentInfo in resolvedIntentActivities) { + val packageName = resolvedIntentInfo.activityInfo.packageName + context.grantUriPermission( + packageName, + mediaUri, + Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION + ) + } + + cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri) + cameraIntent to Uri.fromFile(file) } - val mediaUri = - context.contentResolver.insert(mediaType.externalContentUri, contentValues)!! - cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri) - return cameraIntent to mediaUri } fun scanMedia(context: Context, uri: Uri): Completable {