|
|
@ -92,8 +92,7 @@ object FilePicker { |
|
|
|
activity: BaseActivity, |
|
|
|
activity: BaseActivity, |
|
|
|
requestCode: Int, |
|
|
|
requestCode: Int, |
|
|
|
title: String = activity.getString(R.string.select_file), |
|
|
|
title: String = activity.getString(R.string.select_file), |
|
|
|
type: Array<String>, |
|
|
|
allowExtensions: Array<String>, |
|
|
|
allowExtensions: Array<String>?, |
|
|
|
|
|
|
|
default: (() -> Unit)? = null |
|
|
|
default: (() -> Unit)? = null |
|
|
|
) { |
|
|
|
) { |
|
|
|
activity.alert(title = title) { |
|
|
|
activity.alert(title = title) { |
|
|
@ -108,7 +107,10 @@ object FilePicker { |
|
|
|
1 -> { |
|
|
|
1 -> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val intent = createSelectFileIntent() |
|
|
|
val intent = createSelectFileIntent() |
|
|
|
intent.putExtra(Intent.EXTRA_MIME_TYPES, type) |
|
|
|
intent.putExtra( |
|
|
|
|
|
|
|
Intent.EXTRA_MIME_TYPES, |
|
|
|
|
|
|
|
typesOfExtensions(allowExtensions) |
|
|
|
|
|
|
|
) |
|
|
|
activity.startActivityForResult(intent, requestCode) |
|
|
|
activity.startActivityForResult(intent, requestCode) |
|
|
|
} catch (e: java.lang.Exception) { |
|
|
|
} catch (e: java.lang.Exception) { |
|
|
|
e.printStackTrace() |
|
|
|
e.printStackTrace() |
|
|
@ -132,7 +134,6 @@ object FilePicker { |
|
|
|
fragment: Fragment, |
|
|
|
fragment: Fragment, |
|
|
|
requestCode: Int, |
|
|
|
requestCode: Int, |
|
|
|
title: String = fragment.getString(R.string.select_file), |
|
|
|
title: String = fragment.getString(R.string.select_file), |
|
|
|
type: Array<String>, |
|
|
|
|
|
|
|
allowExtensions: Array<String>, |
|
|
|
allowExtensions: Array<String>, |
|
|
|
default: (() -> Unit)? = null |
|
|
|
default: (() -> Unit)? = null |
|
|
|
) { |
|
|
|
) { |
|
|
@ -149,7 +150,10 @@ object FilePicker { |
|
|
|
1 -> { |
|
|
|
1 -> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val intent = createSelectFileIntent() |
|
|
|
val intent = createSelectFileIntent() |
|
|
|
intent.putExtra(Intent.EXTRA_MIME_TYPES, type) |
|
|
|
intent.putExtra( |
|
|
|
|
|
|
|
Intent.EXTRA_MIME_TYPES, |
|
|
|
|
|
|
|
typesOfExtensions(allowExtensions) |
|
|
|
|
|
|
|
) |
|
|
|
fragment.startActivityForResult(intent, requestCode) |
|
|
|
fragment.startActivityForResult(intent, requestCode) |
|
|
|
} catch (e: java.lang.Exception) { |
|
|
|
} catch (e: java.lang.Exception) { |
|
|
|
e.printStackTrace() |
|
|
|
e.printStackTrace() |
|
|
@ -202,4 +206,16 @@ object FilePicker { |
|
|
|
} |
|
|
|
} |
|
|
|
.request() |
|
|
|
.request() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun typesOfExtensions(allowExtensions: Array<String>): Array<String> { |
|
|
|
|
|
|
|
val types = hashSetOf<String>() |
|
|
|
|
|
|
|
allowExtensions.forEach { |
|
|
|
|
|
|
|
when (it) { |
|
|
|
|
|
|
|
"txt", "xml" -> types.add("text/*") |
|
|
|
|
|
|
|
"zip" -> types.add("application/x-zip-compressed") |
|
|
|
|
|
|
|
else -> types.add("application/$it") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return types.toTypedArray() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |