Skip to content

Commit 4d37779

Browse files
authored
- issues fixes and improvements. Co-authored-by: AKASH PATEL <akash.patel@mindinventory.com>
1 parent 4934336 commit 4d37779

23 files changed

+224
-29
lines changed

README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
1717
* Filter by particular media type
1818
* Filter videos by min and max time
1919
* Enable/disable camera from LassiOption
20+
* You can open System Default view for file selection by using MediaType.FILE_TYPE_WITH_SYSTEM_VIEW
2021

2122
# Usage
2223

@@ -51,18 +52,19 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
5152
```groovy
5253
dependencies {
5354
...
54-
implementation 'com.github.Mindinventory:Lassi:0.3.0'
55+
implementation 'com.github.Mindinventory:Lassi:X.X.X'
5556
}
5657
```
5758
5859
### Implementation
5960
6061
61-
* Step 1. Add Lassi in to your activity class:
62+
* Step 1.
63+
To open a app color theme view then add Lassi in to your activity class:
6264
6365
```kotlin
6466
val intent = Lassi(this)
65-
.with(LassiOption.CAMERA_AND_GALLERY) // choose Option CAMERA, GALLERY or CAMERA_AND_GALLERY
67+
.with(LassiOption.CAMERA_AND_GALLERY) // choose Option CAMERA or CAMERA_AND_GALLERY
6668
.setMaxCount(5)
6769
.setGridSize(3)
6870
.setMediaType(MediaType.VIDEO) // MediaType : VIDEO IMAGE, AUDIO OR DOC
@@ -90,7 +92,18 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
9092
.build()
9193
receiveData.launch(intent)
9294
```
95+
`OR` To open a system default view then add Lassi in to your activity class:
9396
97+
```kotlin
98+
val intent = Lassi(this)
99+
.setMediaType(MediaType.FILE_TYPE_WITH_SYSTEM_VIEW)
100+
.setSupportedFileTypes(
101+
"jpg", "jpeg", "png", "webp", "gif", "mp4", "mkv", "webm", "avi", "flv", "3gp",
102+
"pdf", "odt", "doc", "docs", "docx", "txt", "ppt", "pptx", "rtf", "xlsx", "xls"
103+
) // Filter by required media format (Mandatory)
104+
.build()
105+
receiveData.launch(intent)
106+
```
94107

95108
* Step 2. Get Lassi result in ActivityResultCallback lambda function.
96109

@@ -110,6 +123,10 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
110123

111124
### Document access permission note
112125
If Android device SDK is >= 30 and wants to access document (only for choose the non media file) then add ```android.permission.MANAGE_EXTERNAL_STORAGE``` permission in your app otherwise library won't allow to access documents. Kindly check sample app for more detail.
126+
If you don't want to give Manage External Storage permission and wants to get files with system default view then You can use `OR` option from Step 1 and give required file type of document.
127+
128+
### MediaType.FILE_TYPE_WITH_SYSTEM_VIEW (for System Default View)
129+
Using this MediaType you can choose multiple files from system default view. You can't set max count limit for file choose. Give file type into setSupportedFileTypes and you can choose only those types of file from system view.
113130
114131
### Guideline for contributors
115132
Contribution towards our repository is always welcome, we request contributors to create a pull request to the **develop** branch only.
@@ -125,7 +142,7 @@ It would be great for us if the reporter can share the below things to understan
125142
126143
### Requirements
127144
128-
* minSdkVersion >= 17
145+
* minSdkVersion >= 19
129146
* Androidx
130147
131148
### Library used

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
compileSdkVersion 32
99
defaultConfig {
1010
applicationId "com.lassi.app"
11-
minSdkVersion 17
11+
minSdkVersion 19
1212
targetSdkVersion 32
1313
versionCode 1
1414
versionName "1.0"

app/src/main/java/com/lassi/app/MainActivity.kt

+31-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
3737
btnVideoPicker.setOnClickListener(this)
3838
btnAudioPicker.setOnClickListener(this)
3939
btnDocPicker.setOnClickListener(this)
40+
btnDocumentSystemIntent.setOnClickListener(this)
4041
rvSelectedMedia.adapter = selectedMediaAdapter
4142
rvSelectedMedia.addItemDecoration(GridSpacingItemDecoration(2, 10))
4243
}
@@ -108,6 +109,36 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
108109
R.id.btnDocPicker -> {
109110
requestPermissionForDocument()
110111
}
112+
R.id.btnDocumentSystemIntent -> {
113+
val intent = Lassi(this)
114+
.setMediaType(MediaType.FILE_TYPE_WITH_SYSTEM_VIEW)
115+
.setSupportedFileTypes(
116+
"jpg",
117+
"jpeg",
118+
"png",
119+
"webp",
120+
"gif",
121+
"mp4",
122+
"mkv",
123+
"webm",
124+
"avi",
125+
"flv",
126+
"3gp",
127+
"pdf",
128+
"odt",
129+
"doc",
130+
"docs",
131+
"docx",
132+
"txt",
133+
"ppt",
134+
"pptx",
135+
"rtf",
136+
"xlsx",
137+
"xls"
138+
)
139+
.build()
140+
receiveData.launch(intent)
141+
}
111142
}
112143
}
113144

@@ -206,7 +237,6 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
206237
}
207238
}
208239
}
209-
210240
else -> {
211241
launchDocPicker()
212242
}

app/src/main/res/layout/activity_main.xml

+18
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@
8686
app:layout_constraintHorizontal_bias="0.5"
8787
app:layout_constraintStart_toStartOf="parent"
8888
app:layout_constraintTop_toBottomOf="@+id/btnImagePicker" />
89+
90+
<Button
91+
android:id="@+id/btnDocumentSystemIntent"
92+
android:layout_width="wrap_content"
93+
android:layout_height="wrap_content"
94+
android:layout_marginTop="16dp"
95+
android:background="@drawable/bg_rounded_button"
96+
android:drawableStart="@drawable/ic_document"
97+
android:padding="10dp"
98+
android:text="@string/open_system_view"
99+
android:textAllCaps="false"
100+
android:textColor="@android:color/white"
101+
android:textSize="16sp"
102+
android:textStyle="bold"
103+
app:layout_constraintHorizontal_bias="0.5"
104+
app:layout_constraintStart_toStartOf="parent"
105+
app:layout_constraintEnd_toEndOf="parent"
106+
app:layout_constraintTop_toBottomOf="@+id/btnAudioPicker" />
89107
</androidx.constraintlayout.widget.ConstraintLayout>
90108

91109
<androidx.recyclerview.widget.RecyclerView

app/src/main/res/mipmap-hdpi/ic_launcher.png

100644100755
File mode changed.

app/src/main/res/mipmap-hdpi/ic_launcher_round.png

100644100755
File mode changed.

app/src/main/res/mipmap-mdpi/ic_launcher.png

100644100755
File mode changed.

app/src/main/res/mipmap-mdpi/ic_launcher_round.png

100644100755
File mode changed.

app/src/main/res/mipmap-xhdpi/ic_launcher.png

100644100755
File mode changed.

app/src/main/res/mipmap-xhdpi/ic_launcher_round.png

100644100755
File mode changed.

app/src/main/res/mipmap-xxhdpi/ic_launcher.png

100644100755
File mode changed.

app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png

100644100755
File mode changed.

app/src/main/res/mipmap-xxxhdpi/ic_launcher.png

100644100755
File mode changed.

app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png

100644100755
File mode changed.

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
<string name="video">Video</string>
88
<string name="document">Document</string>
99
<string name="audio">Audio</string>
10+
<string name="open_system_view">Open System View</string>
1011
</resources>

lassi/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ android {
1010
compileSdkVersion 32
1111

1212
defaultConfig {
13-
minSdkVersion 17
13+
minSdkVersion 19
1414
targetSdkVersion 32
15-
versionCode 20
16-
versionName "0.3.0"
15+
versionCode 21
16+
versionName "0.4.0"
1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
vectorDrawables.useSupportLibrary = true
1919
multiDexEnabled true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.lassi.common.extenstions
2+
3+
import android.content.Context
4+
import android.database.Cursor
5+
import android.net.Uri
6+
import android.provider.OpenableColumns
7+
8+
fun Context.getFileName(uri: Uri): String? {
9+
val returnCursor: Cursor? = this.contentResolver.query(uri, null, null, null, null)
10+
val nameIndex: Int? = returnCursor?.getColumnIndex(OpenableColumns.DISPLAY_NAME)
11+
returnCursor?.moveToFirst()
12+
val name: String? = nameIndex?.let { returnCursor.getString(it) }
13+
returnCursor?.close()
14+
return name
15+
}
16+
17+
fun Context.getFileSize(uri: Uri): Long {
18+
val returnCursor: Cursor? = this.contentResolver.query(uri, null, null, null, null)
19+
val sizeIndex: Int? = returnCursor?.getColumnIndex(OpenableColumns.SIZE)
20+
returnCursor?.moveToFirst()
21+
val size: Long? = sizeIndex?.let { returnCursor.getLong(it) }
22+
returnCursor?.close()
23+
return size ?: 0L
24+
}

lassi/src/main/java/com/lassi/common/utils/FilePickerUtils.kt

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.lassi.common.utils
22

3+
import android.content.ContentResolver
34
import android.content.Context
45
import android.media.MediaScannerConnection
56
import android.net.Uri
67
import android.util.Log
8+
import android.webkit.MimeTypeMap
9+
import java.io.File
10+
import java.io.FileOutputStream
11+
import java.io.InputStream
12+
import java.io.OutputStream
713
import java.util.*
814

915
object FilePickerUtils {
@@ -19,7 +25,7 @@ object FilePickerUtils {
1925
context: Context,
2026
filePath: String,
2127
mimeType: String = "image/*",
22-
onFileScanComplete: (uri: Uri?, path:String?) -> Unit
28+
onFileScanComplete: (uri: Uri?, path: String?) -> Unit
2329
) {
2430
context.let {
2531
MediaScannerConnection.scanFile(
@@ -32,4 +38,60 @@ object FilePickerUtils {
3238
}
3339
}
3440
}
41+
42+
private fun getFileExtension(context: Context, uri: Uri): String? =
43+
if (uri.scheme == ContentResolver.SCHEME_CONTENT)
44+
MimeTypeMap.getSingleton()
45+
.getExtensionFromMimeType(context.contentResolver.getType(uri))
46+
else uri.path?.let {
47+
MimeTypeMap.getFileExtensionFromUrl(
48+
Uri.fromFile(File(it)).toString()
49+
)
50+
}
51+
52+
fun getFilePathFromUri(context: Context, uri: Uri, uniqueName: Boolean): String =
53+
if (uri.path?.contains("file://") == true) uri.path!!
54+
else getFileFromContentUri(context, uri, uniqueName).path
55+
56+
private fun getFileFromContentUri(
57+
context: Context,
58+
contentUri: Uri,
59+
uniqueName: Boolean,
60+
): File {
61+
// Preparing Temp file name
62+
val fileExtension = getFileExtension(context, contentUri) ?: ""
63+
val fileName =
64+
("file" + if (uniqueName) System.currentTimeMillis() else "") + ".$fileExtension"
65+
66+
// Creating Temp file
67+
val tempFile = File(context.cacheDir, fileName)
68+
tempFile.createNewFile()
69+
// Initialize streams
70+
var oStream: FileOutputStream? = null
71+
var inputStream: InputStream? = null
72+
73+
try {
74+
oStream = FileOutputStream(tempFile)
75+
inputStream = context.contentResolver.openInputStream(contentUri)
76+
77+
inputStream?.let { copy(inputStream, oStream) }
78+
oStream.flush()
79+
} catch (e: Exception) {
80+
e.printStackTrace()
81+
} finally {
82+
// Close streams
83+
inputStream?.close()
84+
oStream?.close()
85+
}
86+
return tempFile
87+
}
88+
89+
private fun copy(source: InputStream, target: OutputStream) {
90+
val buf = ByteArray(8192)
91+
var length: Int
92+
while (source.read(buf).also { length = it } > 0) {
93+
target.write(buf, 0, length)
94+
}
95+
}
96+
3597
}

lassi/src/main/java/com/lassi/data/media/MediaRepositoryImpl.kt

+3
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ class MediaRepositoryImpl(private val context: Context) : MediaRepository {
308308
MediaStore.Video.Media.DATE_ADDED
309309
)
310310
}
311+
else ->{
312+
null
313+
}
311314
}
312315
}
313316

lassi/src/main/java/com/lassi/domain/media/LassiOption.kt

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ package com.lassi.domain.media
22

33
enum class LassiOption {
44
CAMERA,
5-
GALLERY,
65
CAMERA_AND_GALLERY
76
}

lassi/src/main/java/com/lassi/domain/media/MediaType.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ enum class MediaType(val value: Int) {
44
IMAGE(1),
55
VIDEO(2),
66
AUDIO(3),
7-
DOC(4)
7+
DOC(4),
8+
FILE_TYPE_WITH_SYSTEM_VIEW(5)
89
}

lassi/src/main/java/com/lassi/presentation/mediadirectory/FolderFragment.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ class FolderFragment : LassiBaseViewModelFragment<FolderViewModel>() {
4343

4444
private val requestPermission =
4545
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { map ->
46-
if (map.entries.all {
47-
it.value == true
48-
}) {
46+
if (map.entries.all { it.value }) {
4947
fetchFolders()
5048
} else {
5149
showPermissionDisableAlert()

0 commit comments

Comments
 (0)