Skip to content

Commit 3f10a6b

Browse files
authored
Merge pull request #1803 from daniJimen/feature/fix_issue_select_folder_1802
fix issue allow users to select folder
2 parents 8468869 + 3f8be30 commit 3f10a6b

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 10.1.8
2+
### Android
3+
- Fixed an issue when a folder is selected [#1802](https://github.com/miguelpruivo/flutter_file_picker/issues/1802)
4+
15
## 10.1.7
26
### Web
37
- Fixed a memory leak on the web.

android/src/main/kotlin/com/mr/flutter/plugin/filepicker/FileUtils.kt

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.os.Bundle
1111
import android.os.Environment
1212
import android.os.Parcelable
1313
import android.provider.DocumentsContract
14-
import android.provider.MediaStore
1514
import android.provider.OpenableColumns
1615
import android.util.Log
1716
import android.webkit.MimeTypeMap
@@ -68,8 +67,7 @@ object FileUtils {
6867
}
6968

7069
data.data != null -> {
71-
var uri = data.data!!
72-
uri = processUri(activity, uri, compressionQuality)
70+
var uri = processUri(activity, data.data!!, compressionQuality)
7371

7472
if (type == "dir") {
7573
uri = DocumentsContract.buildDocumentUriUsingTree(
@@ -108,7 +106,7 @@ object FileUtils {
108106
context: Context,
109107
uri: Uri,
110108
bytes: ByteArray?
111-
): Uri? {
109+
): Uri {
112110
context.contentResolver.openOutputStream(uri)?.use { output ->
113111
bytes?.let {
114112
output.write(it)
@@ -118,7 +116,7 @@ object FileUtils {
118116
return uri
119117
}
120118

121-
fun FilePickerDelegate.handleFileResult(files: List<FileInfo>) {
119+
private fun FilePickerDelegate.handleFileResult(files: List<FileInfo>) {
122120
if (files.isNotEmpty()) {
123121
finishWithSuccess(files)
124122
} else {
@@ -138,7 +136,7 @@ object FileUtils {
138136
intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
139137
} else {
140138
if (type == "image/*") {
141-
intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
139+
intent = Intent(Intent.ACTION_PICK)
142140
val uri = (Environment.getExternalStorageDirectory().path + File.separator).toUri()
143141
intent.setDataAndType(uri, type)
144142
intent.type = this.type
@@ -168,15 +166,16 @@ object FileUtils {
168166
}
169167

170168

171-
if (intent.resolveActivity(activity.packageManager) != null) {
172-
activity.startActivityForResult(intent, REQUEST_CODE)
173-
} else {
174-
Log.e(
175-
FilePickerDelegate.TAG,
176-
"Can't find a valid activity to handle the request. Make sure you've a file explorer installed."
177-
)
178-
finishWithError("invalid_format_type", "Can't handle the provided file type.")
179-
}
169+
170+
}
171+
if (intent.resolveActivity(activity.packageManager) != null) {
172+
activity.startActivityForResult(intent, REQUEST_CODE)
173+
} else {
174+
Log.e(
175+
FilePickerDelegate.TAG,
176+
"Can't find a valid activity to handle the request. Make sure you've a file explorer installed."
177+
)
178+
finishWithError("invalid_format_type", "Can't handle the provided file type.")
180179
}
181180
}
182181
fun FilePickerDelegate?.startFileExplorer(
@@ -212,7 +211,7 @@ object FileUtils {
212211
return mimeType.substringAfter("/")
213212
}
214213

215-
fun getMimeTypeForBytes(fileName: String?, bytes: ByteArray?): String {
214+
private fun getMimeTypeForBytes(fileName: String?, bytes: ByteArray?): String {
216215
val tika = Tika()
217216

218217
if (fileName.isNullOrEmpty()) {
@@ -262,15 +261,15 @@ object FileUtils {
262261
}
263262
}
264263

265-
fun processUri(activity: Activity, uri: Uri, compressionQuality: Int): Uri {
264+
private fun processUri(activity: Activity, uri: Uri, compressionQuality: Int): Uri {
266265
return if (compressionQuality > 0 && isImage(activity.applicationContext, uri)) {
267266
compressImage(uri, compressionQuality, activity.applicationContext)
268267
} else {
269268
uri
270269
}
271270
}
272271

273-
fun addFile(
272+
private fun addFile(
274273
activity: Activity,
275274
uri: Uri,
276275
loadDataToMemory: Boolean,
@@ -282,7 +281,7 @@ object FileUtils {
282281
}
283282

284283
@Suppress("deprecation")
285-
fun getSelectedItems(bundle: Bundle): ArrayList<Parcelable>? {
284+
private fun getSelectedItems(bundle: Bundle): ArrayList<Parcelable>? {
286285
if (Build.VERSION.SDK_INT >= 33) {
287286
return bundle.getParcelableArrayList("selectedItems", Parcelable::class.java)
288287
}
@@ -348,11 +347,7 @@ object FileUtils {
348347

349348
@JvmStatic
350349
fun isImage(context: Context, uri: Uri): Boolean {
351-
val extension = getFileExtension(context, uri)
352-
353-
if (extension == null) {
354-
return false
355-
}
350+
val extension = getFileExtension(context, uri) ?: return false
356351

357352
return extension.contentEquals("jpg") || extension.contentEquals("jpeg")
358353
|| extension.contentEquals("png") || extension.contentEquals("webp")
@@ -409,7 +404,7 @@ object FileUtils {
409404
* @param uri The Uri to check.
410405
* @return Whether the Uri authority is DownloadsProvider.
411406
*/
412-
fun isDownloadsDocument(uri: Uri): Boolean {
407+
private fun isDownloadsDocument(uri: Uri): Boolean {
413408
return uri.authority == "com.android.providers.downloads.documents"
414409
}
415410

@@ -428,7 +423,7 @@ object FileUtils {
428423
return true
429424
}
430425

431-
fun loadData(file: File, fileInfo: FileInfo.Builder) {
426+
private fun loadData(file: File, fileInfo: FileInfo.Builder) {
432427
try {
433428
val size = file.length().toInt()
434429
val bytes = ByteArray(size)
@@ -505,7 +500,7 @@ object FileUtils {
505500
return fileInfo.build()
506501
}
507502

508-
fun getPathFromTreeUri(uri: Uri): String? {
503+
private fun getPathFromTreeUri(uri: Uri): String {
509504
val docId = DocumentsContract.getTreeDocumentId(uri)
510505
val parts = docId.split(":")
511506
return "${Environment.getExternalStorageDirectory()}/${parts.last()}"
@@ -524,7 +519,7 @@ object FileUtils {
524519
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path
525520
if (docId == "downloads") {
526521
return extPath
527-
} else if (docId.matches("^ms[df]\\:.*".toRegex())) {
522+
} else if (docId.matches("^ms[df]:.*".toRegex())) {
528523
val fileName = getFileName(treeUri, con)
529524
return "$extPath/$fileName"
530525
} else if (docId.startsWith("raw:")) {
@@ -535,7 +530,6 @@ object FileUtils {
535530
}
536531
}
537532
var volumePath = getPathFromTreeUri(treeUri)
538-
?: return File.separator
539533

540534
if (volumePath.endsWith(File.separator)) {
541535
volumePath = volumePath.substring(0, volumePath.length - 1)
@@ -546,7 +540,7 @@ object FileUtils {
546540
if (documentPath.endsWith(File.separator)) {
547541
documentPath = documentPath.substring(0, documentPath.length - 1)
548542
}
549-
return if (!documentPath.isEmpty()) {
543+
return if (documentPath.isNotEmpty()) {
550544
if(volumePath.endsWith(documentPath)){
551545
volumePath
552546
}else {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin
33
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
44
repository: https://github.com/miguelpruivo/flutter_file_picker
55
issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues
6-
version: 10.1.7
6+
version: 10.1.8
77

88
dependencies:
99
flutter:

0 commit comments

Comments
 (0)