| | |
| | | package com.example.smart.image |
| | | |
| | | import FileUploader |
| | | import android.R |
| | | import android.app.Activity |
| | | import android.content.Context |
| | | import android.content.Intent |
| | | import android.graphics.Color |
| | | import android.net.Uri |
| | | import android.os.Build |
| | | import android.os.Environment |
| | | import android.provider.Settings |
| | | import androidx.activity.result.contract.ActivityResultContracts |
| | | import androidx.appcompat.app.AppCompatActivity |
| | | import androidx.fragment.app.FragmentActivity |
| | | import com.donkingliang.imageselector.ImageSelectorActivity |
| | | import com.donkingliang.imageselector.entry.RequestConfig |
| | | import com.donkingliang.imageselector.utils.ImageSelector |
| | | import com.example.basecore.util.FileTools |
| | | import com.example.basecore.util.LogUtils |
| | | import com.example.smart.bean.ImageRef |
| | | import com.example.smart.widget.dialog.LoadingDialog |
| | | import java.util.ArrayList |
| | | import com.luck.picture.lib.basic.PictureSelector |
| | | import com.luck.picture.lib.config.SelectMimeType |
| | | import com.luck.picture.lib.entity.LocalMedia |
| | | import com.luck.picture.lib.interfaces.OnResultCallbackListener |
| | | import com.luck.picture.lib.style.PictureSelectorStyle |
| | | import com.luck.picture.lib.style.SelectMainStyle |
| | | import com.luck.picture.lib.style.TitleBarStyle |
| | | |
| | | |
| | | class ImageProvider(val activity: Activity, val callback: IImageCallback) { |
| | | |
| | | private val REQUEST_CODE: Int = 6666 |
| | | |
| | | private var ossObjectName = "" |
| | | |
| | | private var maxCount = 1 |
| | | val loadingDialog: LoadingDialog = LoadingDialog(activity) |
| | | |
| | | /** |
| | | * oss object name |
| | | */ |
| | |
| | | } |
| | | |
| | | fun showImageSelect(maxCount: Int = 9) { |
| | | // .backResId(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_mtrl_am_alpha) // 标题 |
| | | // val config = ISListConfig.Builder() // 是否多选, 默认true |
| | | // .multiSelect(false) // 是否记住上次选中记录, 仅当multiSelect为true的时候配置,默认为true |
| | | // .rememberSelected(false) // “确定”按钮背景色 |
| | | // .btnBgColor(Color.GRAY) // “确定”按钮文字颜色 |
| | | // .btnTextColor(Color.BLUE) // 使用沉浸式状态栏 |
| | | // .statusBarColor(Color.parseColor("#3F51B5")) // 返回图标ResId |
| | | // |
| | | // .title("图片") // 标题文字颜色 |
| | | // .titleColor(Color.WHITE) // TitleBar背景色 |
| | | // .titleBgColor(Color.parseColor("#3F51B5")) // 裁剪大小。needCrop为true的时候配置 |
| | | // .cropSize(1, 1, 200, 200) |
| | | // .needCrop(true) // 第一个是否显示相机,默认true |
| | | // .needCamera(false) // 最大选择图片数量,默认9 |
| | | // .maxNum(maxCount) |
| | | // .build() |
| | | // |
| | | // |
| | | //// 跳转到图片选择器 |
| | | // ISNav.getInstance().toListActivity(activity, config, REQUEST_CODE) |
| | | |
| | | |
| | | |
| | | if (!checkPer()) { |
| | | |
| | | val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) |
| | | intent.data = Uri.parse("package:" + activity.packageName) |
| | | activity.startActivityForResult(intent, 0x00000012) |
| | | return |
| | | } |
| | | |
| | | |
| | | val config = RequestConfig() |
| | | config.useCamera = true |
| | | config.isSingle = false |
| | | config.maxSelectCount = maxCount |
| | | config.canPreview = true |
| | | |
| | | this.maxCount = maxCount |
| | | val intent = Intent(activity, ImageSelectorActivity::class.java) |
| | | intent.putExtra(ImageSelector.KEY_CONFIG, config) |
| | | if (activity is FragmentActivity) { |
| | | val register = activity.activityResultRegistry.register( |
| | | "101", |
| | | ActivityResultContracts.StartActivityForResult() |
| | | ) { |
| | | if (it == null) { |
| | | return@register |
| | | //https://gitcode.com/gh_mirrors/pict/PictureSelector/?utm_source=gitcode_aigc_v1_t1&index=bottom&type=card&&uuid_tt_dd=10_37197039080-1778553766662-368769&isLogin=9&from_id=156450437&from_link=2b5eaab318b433d2570db6b0ce95c381 |
| | | PictureSelector.create(activity) |
| | | .openGallery(SelectMimeType.ofImage()) |
| | | .setImageEngine(GlideEngine.createGlideEngine()) |
| | | .setCompressEngine(CompressEngine()) |
| | | .setSandboxFileEngine(SandboxEngine()) |
| | | .isPreviewImage(true) |
| | | .isDisplayCamera(true) |
| | | .setImageSpanCount(3) |
| | | .setSelectorUIStyle(PictureSelectorStyle().apply {//https://github.com/LuckSiege/PictureSelector/wiki/PictureSelector-3.0-%E4%B8%BB%E9%A2%98api%E8%AF%B4%E6%98%8E |
| | | titleBarStyle = TitleBarStyle().apply { |
| | | isHideCancelButton = true |
| | | } |
| | | onActivityResult(REQUEST_CODE, it.resultCode, it.data) |
| | | selectMainStyle = SelectMainStyle().apply { |
| | | isCompleteSelectRelativeTop = true//完成按钮从底部放在右上角 |
| | | isPreviewSelectRelativeBottom = true//预览页选择按钮从顶部放在右下角 |
| | | } |
| | | register.launch(intent) |
| | | } |
| | | } |
| | | |
| | | fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
| | | if (resultCode != AppCompatActivity.RESULT_OK) { |
| | | }) |
| | | .setMaxSelectNum(maxCount) |
| | | .forResult(object : OnResultCallbackListener<LocalMedia?> { |
| | | override fun onResult(result: ArrayList<LocalMedia?>?) { |
| | | if (result == null) { |
| | | return |
| | | } |
| | | if (requestCode == REQUEST_CODE && data != null) { |
| | | onSelectPhotoSuccess(data) |
| | | val filterNotNull = result.filterNotNull() |
| | | if (filterNotNull.isEmpty()) { |
| | | return |
| | | } |
| | | } |
| | | |
| | | private fun onSelectPhotoSuccess(data: Intent) { |
| | | //获取选择器返回的数据 |
| | | val images: ArrayList<String> = |
| | | data.getStringArrayListExtra(ImageSelector.SELECT_RESULT) as ArrayList<String> |
| | | val isCameraImage: Boolean = data.getBooleanExtra(ImageSelector.IS_CAMERA_IMAGE, false) |
| | | |
| | | |
| | | |
| | | val images = filterNotNull.mapNotNull { it.getAvailablePath() } |
| | | LogUtils.e("images:$images") |
| | | if (images.isEmpty()) { |
| | | return |
| | | } |
| | | loadingDialog.show() |
| | | if (maxCount == 1) { |
| | | FileUploader.uploadFile(images[0], callback) { |
| | |
| | | } |
| | | } |
| | | |
| | | override fun onCancel() { |
| | | } |
| | | }) |
| | | } |
| | | |
| | | /** |
| | | * 上传绘制后的图片 |
| | |
| | | loadingDialog.dismiss() |
| | | } |
| | | } |
| | | |
| | | // private val callBack = OssCallBack() |
| | | // |
| | | // private inner class OssCallBack : IOssCallBack { |
| | | // override fun onAllListSuccess(ossPaths: MutableList<EntityRef>) { |
| | | // super.onAllListSuccess(ossPaths) |
| | | // loadingDialog.dismiss() |
| | | // } |
| | | // |
| | | // override fun onSuccess(entityRef: EntityRef) { |
| | | // activity.runOnUiThread { |
| | | // if (maxCount == 1) { |
| | | // loadingDialog.dismiss() |
| | | // } |
| | | // onUploadSuccess(entityRef) |
| | | // } |
| | | // } |
| | | // |
| | | // override fun onFailed() { |
| | | // activity.runOnUiThread { |
| | | // loadingDialog.dismiss() |
| | | // onUploadFailed() |
| | | // activity.toast("图片上传失败,请重新选择") |
| | | // } |
| | | // } |
| | | // } |
| | | // |
| | | // private fun onUploadSuccess(entity: EntityRef) { |
| | | // synchronized(this) { |
| | | // callback.onUploadSuccess(entity) |
| | | // } |
| | | // } |
| | | // |
| | | // private fun onUploadFailed() { |
| | | // synchronized(this) { |
| | | // callback.onUploadFailed() |
| | | // } |
| | | // } |
| | | |
| | | interface IImageCallback { |
| | | |