From 2668c353b0872bdc020db7cbecab3fd97bab0d95 Mon Sep 17 00:00:00 2001 From: Nishith Darji Date: Mon, 27 Jan 2025 18:20:45 +0530 Subject: [PATCH] added pdf and doc file support to select --- app/build.gradle.kts | 5 +- .../photopickerDemoApp/MainActivity.kt | 30 +++++- photopicker/src/main/AndroidManifest.xml | 1 + .../photopicker/extention/Extention.kt | 16 --- .../fileselector/MediaSelectHelper.kt | 102 +++++++++++++++++- 5 files changed, 128 insertions(+), 26 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 88705bc..cb81ed9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -12,7 +12,7 @@ android { compileSdk = 34 defaultConfig { - applicationId = "com.nishith.justtestmodule" + applicationId = "com.nishith.photopickerDemoApp" minSdk = 23 targetSdk = 34 versionCode = 1 @@ -49,8 +49,7 @@ dependencies { implementation(libs.material) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) - //implementation(libs.hilt.android) - //kapt(libs.hilt.android.compiler) + implementation(libs.glide) androidTestImplementation(libs.androidx.espresso.core) implementation("androidx.media3:media3-exoplayer:1.3.1") implementation("androidx.media3:media3-exoplayer-dash:1.3.1") diff --git a/app/src/main/java/com/nishith/photopickerDemoApp/MainActivity.kt b/app/src/main/java/com/nishith/photopickerDemoApp/MainActivity.kt index 17fe727..f57d91d 100644 --- a/app/src/main/java/com/nishith/photopickerDemoApp/MainActivity.kt +++ b/app/src/main/java/com/nishith/photopickerDemoApp/MainActivity.kt @@ -3,22 +3,22 @@ package com.nishith.photopickerDemoApp import android.net.Uri import android.os.Bundle import android.view.View +import android.widget.ImageView import androidx.media3.common.MediaItem import androidx.media3.exoplayer.ExoPlayer -import com.nishith.photopickerDemoApp.databinding.ActivityMainBinding +import com.bumptech.glide.Glide import com.nishith.photopicker.base.BaseActivity -import com.nishith.photopicker.extention.hide -import com.nishith.photopicker.extention.loadImagefromServerAny -import com.nishith.photopicker.extention.show import com.nishith.photopicker.fileselector.MediaSelectHelper import com.nishith.photopicker.fileselector.MediaSelector +import com.nishith.photopicker.fileselector.OutPutFileAny import com.nishith.photopicker.utils.FileHelperKit.getPath +import com.nishith.photopickerDemoApp.databinding.ActivityMainBinding class MainActivity : BaseActivity() { private lateinit var binding: ActivityMainBinding - lateinit var mediaSelectHelper: MediaSelectHelper + private lateinit var mediaSelectHelper: MediaSelectHelper private var player: ExoPlayer? = null @@ -87,6 +87,10 @@ class MainActivity : BaseActivity() { override fun onVideoURIList(uriArrayList: ArrayList) { super.onVideoURIList(uriArrayList) } + + override fun onAnyFileSelected(outPutFileAny: OutPutFileAny) { + super.onAnyFileSelected(outPutFileAny) + } }, supportFragmentManager) } @@ -97,3 +101,19 @@ class MainActivity : BaseActivity() { player!!.playWhenReady = true } } + +fun View.show() { + this.visibility = View.VISIBLE +} + +fun View.hide() { + this.visibility = View.GONE +} + +fun ImageView?.loadImagefromServerAny(path: Any?) { + this?.context?.let { + Glide.with(it) + .load(path) + .into(this) + } +} \ No newline at end of file diff --git a/photopicker/src/main/AndroidManifest.xml b/photopicker/src/main/AndroidManifest.xml index b9f7ffe..c595db5 100644 --- a/photopicker/src/main/AndroidManifest.xml +++ b/photopicker/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools"> + > + private var anyFilePicker: ActivityResultLauncher private val cameraPermissionList = arrayOf( Manifest.permission.CAMERA @@ -190,6 +193,64 @@ class MediaSelectHelper(private var mActivity: AppCompatActivity) : } } + activityResultPdfLauncher = + mActivity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + if (ContextCompat.checkSelfPermission( + mActivity, + READ_EXTERNAL_STORAGE + ) == PERMISSION_GRANTED + ) { + openPdfIntent() + } else { + mActivity.showToast("Please allow permission from setting") + } + } + + anyFilePicker = + mActivity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult -> + if (result.resultCode == RESULT_OK) { + result.data?.apply { + val selectedMedia: Uri? = data + val cR = mActivity.contentResolver + val mime = MimeTypeMap.getSingleton() + mime.getExtensionFromMimeType(cR.getType(selectedMedia!!)) + + val contentResolver = mActivity.contentResolver + when (contentResolver?.getType(selectedMedia)) { + "application/pdf" -> { + val copyFile = + getActualPath(selectedMedia, createAnyFile(".pdf")) + mMediaSelector?.onAnyFileSelected( + OutPutFileAny( + Uri.fromFile( + copyFile + ), FileType.Pdf + ) + ) + + } + + "application/msword" -> { + val copyFile = + getActualPath(selectedMedia, createAnyFile(".doc")) + mMediaSelector?.onAnyFileSelected( + OutPutFileAny( + Uri.fromFile( + copyFile + ), FileType.Doc + ) + ) + + } + + else -> { + + } + } + } + } + } + canSelectMultipleImages(false) } @@ -289,6 +350,33 @@ class MediaSelectHelper(private var mActivity: AppCompatActivity) : dispatchTakeVideoIntent() } + override fun openPdfIntent() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission( + mActivity, READ_EXTERNAL_STORAGE + ) != PERMISSION_GRANTED + ) { + activityResultPdfLauncher.launch(arrayOf(READ_EXTERNAL_STORAGE)) + } else { + anyFilePicker.launch( + getFileChooserIntent( + arrayOf( + "application/msword", "application/pdf" + ) + ) + ) + } + } + + private fun getFileChooserIntent(type: Array): Intent { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.type = if (type.size == 1) type[0] else "*/*" + if (type.isNotEmpty()) { + intent.putExtra(Intent.EXTRA_MIME_TYPES, type) + } + return intent + } + /** * Check permission and Open camera to take image. **/ @@ -413,7 +501,7 @@ class MediaSelectHelper(private var mActivity: AppCompatActivity) : private fun openCropViewOrNot(file: Uri) { if (isCrop) { val intent: Intent = when (this.cropType) { - Constant.CROP_SQUARE -> CropImage.activity(file).setAspectRatio(4, 4) + CROP_SQUARE -> CropImage.activity(file).setAspectRatio(4, 4) .getIntent(mActivity) Constant.CROP_RECTANGLE -> CropImage.activity(file) @@ -563,6 +651,12 @@ class MediaSelectHelper(private var mActivity: AppCompatActivity) : } +enum class FileType { + Pdf, Doc +} + +data class OutPutFileAny(val uri: Uri, val type: FileType, val thumbImage: String? = null) + interface FileSelectorMethods { fun setLifecycle(lifecycleOwner: LifecycleOwner) fun registerCallback(mMediaSelector: MediaSelector, fragmentManager: FragmentManager) @@ -577,6 +671,7 @@ interface FileSelectorMethods { fun openCameraPictureIntent(isCrop1: Boolean, cropType: String) fun openCameraVideoIntent() fun getThumbnailFromVideo(uri: Uri): File + fun openPdfIntent() } @@ -592,5 +687,8 @@ interface MediaSelector { fun onImageUriList(uriArrayList: ArrayList) {} fun onVideoURIList(uriArrayList: ArrayList) { + } + fun onAnyFileSelected(outPutFileAny: OutPutFileAny) { + } }