javakam / FileOperator

🔥 涵盖了Android系统文件的创建/删除/复制/打开文件(目录)、获取文件(目录)大小、获取常用目录、获取文件名称及后缀、获取MimeType以及MediaStore和SAF的相关操作等常用功能,并且也处理了获取文件Uri/Path的兼容问题、图片压缩和文件选择等功能。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Result Not Returned, the Callback not Called

JosephSanjaya opened this issue · comments

This is the first time I'm using this library, but somehow, I can't get it working, the selector itself is fine but the Activity Result never called. this is my implementation:

AppClass
FileOperator.init(this,BuildConfig.DEBUG)

Fragment
val optionsImage = FileSelectOptions().apply {
fileType = FileType.IMAGE
fileTypeMismatchTip = "文件类型不匹配"
singleFileMaxSize = 5242880
singleFileMaxSizeTip = "图片最大不超过5M!"
allFilesMaxSize = 10485760
allFilesMaxSizeTip = "总图片大小不超过10M!"//单选条件下无效,只做单个图片大小判断
fileCondition = object : FileSelectCondition {
override fun accept(fileType: IFileType, uri: Uri?): Boolean {
return (fileType == FileType.IMAGE && uri != null && !uri.path.isNullOrBlank() && !FileUtils.isGif(
uri
))
}
}
}
mSelector = FileSelector.with(requireContext())
.setRequestCode(REQUEST_CHOOSE_FILE)
.setTypeMismatchTip("文件类型不匹配")
.setMinCount(1, "至少选一个文件!")
.setMaxCount(10, "最多选十个文件!")
.setOverLimitStrategy(OVER_LIMIT_EXCEPT_ALL)
.setSingleFileMaxSize(1048576, "大小不能超过1M!")
.setAllFilesMaxSize(10485760, "总大小不能超过10M!")
.setMimeTypes("image/*")
.applyOptions(optionsImage)
.filter(object : FileSelectCondition {
override fun accept(fileType: IFileType, uri: Uri?): Boolean {
return when (fileType) {
FileType.IMAGE -> (uri != null && !uri.path.isNullOrBlank() && !FileUtils.isGif(
uri
))
FileType.VIDEO -> false
FileType.AUDIO -> false
else -> false
}
}
})
.callback(object : FileSelectCallBack {
override fun onSuccess(results: List?) {
if (!results.isNullOrEmpty()) {
mMatpel.uploadMateri(File(results[0].filePath ?: ""))
}
}
override fun onError(e: Throwable?) {
requireContext().showErrorToast(e?.message.toString())
}
}).choose()

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
mSelector?.obtainResult(requestCode, resultCode, data)
super.onActivityResult(requestCode, resultCode, data)
}

i thought i already follow the instructions.

bottomFragment.onActivityResult(requestCode, resultCode, data)
still not woking

在Activity 选择文件依然没有CallBack ,应该不是Fragment的原因

@javakam I tried the examples but it still didn't work in the fragment, but it works if I place Selector on Activity, then I checked on your code, I notice that you check if context set on selector is a fragment, correct me if I'm wrong isn't requireContext() return an activity which means that the check always gives you Activity?

在Activity 选择文件依然没有CallBack ,应该不是Fragment的原因

方便发一下你的使用方式吗

Is it convenient to post your usage

@JosephSanjaya #15 v1.3.6

1.3.6版本加入了国际化Fragment的使用方法和案例

The 1.3.6 version adds the usage methods and cases of internationalization and Fragment

fun with(context: Context): Builder {
    return Builder(context)
}
fun with(fragment: Fragment): Builder {
    return Builder(fragment)
}
...

🍎实现跳转 (Implement jump) :

internal fun startActivityForResult(context: Any?, intent: Intent, requestCode: Int) {
    if (context == null) return
    if (context is Activity) {
        if (!context.isFinishing && !context.isDestroyed) {
            context.startActivityForResult(intent, requestCode)
        }
    } else if (context is Fragment) {
        val act: Activity? = context.activity
        if (act?.isFinishing == false && !act.isDestroyed) {
            context.startActivityForResult(intent, requestCode)
        }
    }
}

@javakam Ok thank you for your solution sir