riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
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
package cn.flightfeather.thirdappmodule.module.base
 
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ImageView
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.activity.PhotoViewerActivity
import cn.flightfeather.thirdappmodule.util.UUIDGenerator
import cn.flightfeather.thirdappmodule.util.file.FileUtil
import cn.flightfeather.thirdappmodule.util.photo.PhotoUtil
import com.bumptech.glide.Glide
import com.lcw.library.imagepicker.ImagePicker
import org.jetbrains.anko.toast
import java.io.File
import java.io.IOException
import java.io.Serializable
 
/**
 * @author riku
 * Date: 2019/8/2
 */
const val VIEW_PHOTO = 100
const val TAKE_PHOTO = 101
 
abstract class BaseTakePicActivity : BaseActivity() {
 
    companion object {
        const val EXTRA_SELECT_IMAGES = ImagePicker.EXTRA_SELECT_IMAGES
    }
 
    //拍摄的待上传的图片文件列表, true: 代表需要上传;false: 代表不需要上传
    val pathTempList = mutableListOf<Pair<File, Boolean>>()
 
    // fixme: 2019/8/2  由于原代码中大部分图片拍摄固定死了最多3张,所以没有使用列表结构,而是手动设置了3个ImageView,此处暂时延用,不做修改
    var imageViewList = mutableListOf<ImageView>()
 
    //图片是否可删除
    abstract val picDeletable: Boolean
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        imageViewList.addAll(getImageViews())
 
        refreshImageView()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        when (requestCode) {
            VIEW_PHOTO -> {
                //查看临时拍摄图片可以删除
                data?.getIntExtra("position", -1)?.let {
                    if (it > -1) {
                        val file = pathTempList[it].first
                        pathTempList.removeAt(it)
                        if (file.exists()) {
                            file.delete()
                        }
                        refreshImageView()
                    }
                }
            }
            TAKE_PHOTO -> {
                val paths = data?.getStringArrayListExtra(EXTRA_SELECT_IMAGES)
                paths?.forEach {
                    val oldFile = File(it)
                    if (it.contains("FlightFeather/Temp")) {
                        pathTempList.add(Pair(oldFile, true))
                    } else {
                        val newFile = File(Environment.getExternalStorageDirectory(), "FlightFeather/Temp/" + UUIDGenerator.generateUUID(4) + ".jpg")
                        try {
                            FileUtil.copyFile(oldFile, newFile)
                            pathTempList.add(Pair(newFile, true))
                        } catch (e: IOException) {
                            e.printStackTrace()
                            application.toast("复制文件失败")
                        }
                    }
                }
                refreshImageView()
            }
        }
    }
 
    fun refreshImageView() {
        val picSize = pathTempList.size
        for (i in imageViewList.indices) {
            when {
                i < picSize -> {
                    imageViewList[i].setOnClickListener(viewPhotoClickListener(i))
                    imageViewList[i].scaleType = ImageView.ScaleType.CENTER_CROP
                    Glide.with(this)
                            .load(pathTempList[i].first)
                            .placeholder(R.drawable.icon_add_photo_waite)
                            .into(imageViewList[i])
                }
                i == picSize -> imageViewList[i].run {
                    setOnClickListener(takePhotoClickListener(i))
                    scaleType = ImageView.ScaleType.FIT_CENTER
                    setImageResource(R.drawable.icon_add_photo)
                }
                else -> imageViewList[i].run {
                    setOnClickListener(null)
                    scaleType = ImageView.ScaleType.FIT_CENTER
                    setImageResource(R.drawable.icon_add_photo_blank)
                }
            }
        }
    }
 
    //有图片时,查看图片的点击事件(temp)
    fun viewPhotoClickListener(position: Int): View.OnClickListener {
        return View.OnClickListener {
            val fileList = mutableListOf<File>()
            pathTempList.forEach {
                fileList.add(it.first)
            }
            val intent = Intent(this, PhotoViewerActivity::class.java)
            intent.putExtra("position", position)
            intent.putExtra("type", PhotoViewerActivity.EVIDENCE_PHOTO_TEMP)
            intent.putExtra("deletable", picDeletable)
            intent.putExtra(PhotoViewerActivity.PARA_FILES, fileList as Serializable)
            startActivityForResult(intent, VIEW_PHOTO)
        }
    }
 
    //添加图片用的点击事件
    fun takePhotoClickListener(position: Int): View.OnClickListener {
        return View.OnClickListener {
            val t = imageViewList.size - pathTempList.size
            val picNum = if (t >= 0) t else 0
            PhotoUtil.pickPhoto(this, TAKE_PHOTO, picNum)
        }
    }
 
    abstract fun getImageViews(): MutableList<ImageView>
}