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)
|
}
|
})
|
}
|
|
}
|