18034727885
2026-07-02 eb6124e310ae8875a0150ee5d2edd52d5a579d87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.driver.igotrip.ui.main
 
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.driver.igotrip.R
import com.driver.igotrip.base.MyBaseActivity
import com.driver.igotrip.bean.StringBean
import com.driver.igotrip.netUtls.Api
import com.driver.igotrip.netUtls.BaseBean
import com.driver.igotrip.netUtls.HttpManager
import com.driver.igotrip.netUtls.MyObserver
import com.driver.igotrip.ui.DialogUtil
import com.driver.igotrip.ui.GetImgActivity
import com.driver.igotrip.ui.adapter.MediaOperationAdapter
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.disposables.Disposable
import org.jetbrains.anko.find
import org.jetbrains.anko.toast
import java.io.File
 
//多媒体操作页
class MediaOperationActivity : MyBaseActivity() {
    //多媒体资源
    private val mediaMap = mutableMapOf<String, String>()
    private val title by lazy { intent.getStringExtra("title") }
    private val operationLabel by lazy { intent.getStringExtra("operationLabel") }
    private val max by lazy { intent.getIntExtra("max", 0) }
    private val min by lazy { intent.getIntExtra("min", 0) }
    private val pictures = mutableListOf<String>("")
    private val adapter by lazy { MediaOperationAdapter() }
    private val rvList by lazy { findViewById<RecyclerView>(R.id.rv_list) }
    private val tvOperation by lazy { findViewById<TextView>(R.id.tv_operation) }
 
    companion object {
        //获取跳转此页面intent
        fun getIntent(
            context: Context,
            title: String,
            operationLabel: String,
            max: Int,
            min: Int
        ): Intent {
            return Intent(context, MediaOperationActivity::class.java).apply {
                putExtra("title", title)
                putExtra("operationLabel", operationLabel)
                putExtra("max", max)
                putExtra("min", min)
            }
        }
    }
 
    override fun setContentView() {
        setContentView(R.layout.activity_media_operation)
    }
 
    override fun initView() {
        setTitleText("请上传图片")
        tv_title.text = title
        rvList.adapter = adapter
        rvList.layoutManager = GridLayoutManager(this, 4)
        adapter.data = pictures
        tvOperation.text = operationLabel
    }
 
    override fun setOnclick() {
        adapter.setOnItemClickListener { _, position ->
            if (pictures[position].isEmpty()) {
                mediaOperation("请选择")
            } else {
                val arrayList = ArrayList<String>()
                arrayList.addAll(pictures)
                startActivity(ShowPictureActivity.getIntent(this, "", arrayList, position))
            }
        }
        adapter.setOnItemDeleteClickListener { position, item ->
            removeMedia(position)
        }
        tvOperation.setOnClickListener {
            val data = pictures.toMutableList().apply {
                removeIf { it.isEmpty() }
            }
            if (data.size < min) {
                toast(getString(R.string.请上传图片))
                return@setOnClickListener
            }
 
            setResult(RESULT_OK, Intent().apply {
                putStringArrayListExtra("result", ArrayList<String>().apply { addAll(data) })
            })
            finish()
        }
    }
 
    //多媒体的操作
    @SuppressLint("CheckResult")
    private fun mediaOperation(title: String) {
        val rxPermissions = RxPermissions(this)
        rxPermissions.request(Manifest.permission.CAMERA)
            .subscribe { aBoolean ->
                if (aBoolean) {
                    val phoneDialog = DialogUtil.getDialog(mContext, R.layout.dialog_phone)
                    val view = DialogUtil.getView(phoneDialog)
                    if (!title.isEmpty()) {
                        view.find<TextView>(R.id.btn_title).text = title
                    } else {
                        view.find<TextView>(R.id.btn_title).text = getString(R.string.选择照片)
                    }
                    view.find<View>(R.id.button_cancel).setOnClickListener {
                        phoneDialog.dismiss()
                    }
                    view.find<View>(R.id.btn_album).setOnClickListener {
                        openAlbum()
                        phoneDialog.dismiss()
                    }
 
                    view.find<View>(R.id.btn_phone).setOnClickListener {
                        openCarmra()
                        phoneDialog.dismiss()
                    }
                } else {
                    showToast(getString(R.string.请在应用权限页面开启相机权限))
                }
            }
    }
 
    //打开相册
    fun openAlbum() {
        startActivityForResult(Intent(this, GetImgActivity::class.java).apply {
            putExtra("type", false)
        }, 321)
    }
 
    //拍照
    private fun openCarmra() {
        startActivityForResult(Intent(this, GetImgActivity::class.java).apply {
            putExtra("type", true)
        }, 123)
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == ComponentActivity.RESULT_OK) {
            if (requestCode == 123) {
                data?.let { it ->
                    it.getStringExtra("img")?.also {
                        operationMedia(it)
                    }
                }
            } else {
                data?.let { it ->
                    it.getStringExtra("img")?.also {
                        operationMedia(it)
                    }
                }
            }
        }
    }
 
    //操作多媒体
    private fun operationMedia(filePath: String) {
        val url = mediaMap[filePath]
        if (url.isNullOrEmpty()) {
            saveMedia(filePath) {
                mediaMap[filePath] = it
                addMedia(it)
            }
            return
        }
        addMedia(url)
    }
 
    //添加多媒体
    private fun addMedia(url: String) {
        pictures.removeIf { it.isEmpty() }
        pictures.add(url)
        if (pictures.size < max) {
            pictures.add(0, "")
        }
        adapter.data = pictures
    }
 
    //移除多媒体
    private fun removeMedia(position: Int) {
        pictures.removeAt(position)
        pictures.removeIf { it.isEmpty() }
        if (pictures.size < max) {
            pictures.add(0, "")
        }
        adapter.data = pictures
    }
 
    /***
     * 保存多媒体,上传oss
     */
    private fun saveMedia(filePath: String, block: (url: String) -> Unit) {
        HttpManager.getInstance()
            .post<BaseBean<String>>(
                Api.upImage,
                File(filePath),
                object : MyObserver(this@MediaOperationActivity) {
                    override fun onSubscribe(d: Disposable) {
                        super.onSubscribe(d)
                        println("上传开始:" + filePath)
                        showDialog()
                    }
 
                    override fun success(responseString: String?) {
                        dismissDialog()
                        val fromJson = gson.fromJson(responseString, StringBean::class.java)
                        val url = fromJson.data
                        if (!url.isNullOrEmpty()) {
                            println("上传成功:" + url)
                            block.invoke(url)
                        } else {
                            println("上传失败:" + url)
                        }
                    }
 
                    override fun onError(e: Throwable) {
                        super.onError(e)
                        dismissDialog()
                        println("上传失败:" + e.message)
                    }
                })
    }
 
}