Skip to content

Commit

Permalink
added pdf and doc file support to select
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishith2008 committed Jan 27, 2025
1 parent c45d951 commit 2668c35
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 26 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
compileSdk = 34

defaultConfig {
applicationId = "com.nishith.justtestmodule"
applicationId = "com.nishith.photopickerDemoApp"
minSdk = 23
targetSdk = 34
versionCode = 1
Expand Down Expand Up @@ -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")
Expand Down
30 changes: 25 additions & 5 deletions app/src/main/java/com/nishith/photopickerDemoApp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -87,6 +87,10 @@ class MainActivity : BaseActivity() {
override fun onVideoURIList(uriArrayList: ArrayList<Uri>) {
super.onVideoURIList(uriArrayList)
}

override fun onAnyFileSelected(outPutFileAny: OutPutFileAny) {
super.onAnyFileSelected(outPutFileAny)
}
}, supportFragmentManager)
}

Expand All @@ -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)
}
}
1 change: 1 addition & 0 deletions photopicker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,3 @@ import com.bumptech.glide.Glide
fun Activity.showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

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)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nishith.photopicker.fileselector

import android.Manifest
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.annotation.SuppressLint
import android.app.Activity.RESULT_OK
import android.content.ContentResolver
Expand Down Expand Up @@ -46,8 +47,10 @@ class MediaSelectHelper(private var mActivity: AppCompatActivity) :
private var fragmentManager: FragmentManager? = null
private var fileForCameraIntent: String = ""
private var photoFile: File? = null
private var cropType = Constant.CROP_SQUARE
private var cropType = CROP_SQUARE
private var isCrop = false
private var activityResultPdfLauncher: ActivityResultLauncher<Array<String>>
private var anyFilePicker: ActivityResultLauncher<Intent>

private val cameraPermissionList = arrayOf(
Manifest.permission.CAMERA
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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<String>): 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.
**/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -577,6 +671,7 @@ interface FileSelectorMethods {
fun openCameraPictureIntent(isCrop1: Boolean, cropType: String)
fun openCameraVideoIntent()
fun getThumbnailFromVideo(uri: Uri): File
fun openPdfIntent()
}


Expand All @@ -592,5 +687,8 @@ interface MediaSelector {
fun onImageUriList(uriArrayList: ArrayList<Uri>) {}
fun onVideoURIList(uriArrayList: ArrayList<Uri>) {

}
fun onAnyFileSelected(outPutFileAny: OutPutFileAny) {

}
}

0 comments on commit 2668c35

Please sign in to comment.