lmw
2025-04-24 718f31c92e2029d05260810435a2c70cef6e6ce5
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
package cn.sinata.xldutils.activity
 
import android.Manifest
import android.app.Activity
import android.content.ContentValues
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import androidx.core.app.ActivityCompat
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import cn.sinata.xldutils.R
import cn.sinata.xldutils.utils.alertDialog
import cn.sinata.xldutils.utils.getUrlPath
import cn.sinata.xldutils.view.WheelView
import cn.sinata.xldutils.xldUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.toast
import java.io.File
import java.util.*
 
class SelectPhotoWheelDialog : DialogActivity() {
 
    private var tempFile: File? = null
 
    companion object {
        val PATH = "path"
    }
 
    private val useFile by lazy {
        intent.getBooleanExtra("useFile",false)
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        setShowSOP(false)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_single_wheel)
        window.setGravity(Gravity.BOTTOM)
        val wheel = find<WheelView>(R.id.wv_1)
        wheel.setItems(arrayListOf("相册","拍照"))
        wheel.setSeletion(0)
        //请求权限
//        permission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "访问本地存储", 12)
        //拍照
        find<TextView>(R.id.tv_sure).onClick {
            if (wheel.seletedItem == "拍照")
                RxPermissions(this@SelectPhotoWheelDialog).request(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ,Manifest.permission.READ_EXTERNAL_STORAGE).subscribe {
                    if (it) {//有权限
                        //检测路径是否存在,不存在就创建
                        xldUtils.initFilePath()
                        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                        val fileName = System.currentTimeMillis().toString() + ".jpg"
                        tempFile = File(xldUtils.PICDIR, fileName)
                        val u = Uri.fromFile(tempFile)
                        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0)
                        //7.0崩溃问题
                        if (Build.VERSION.SDK_INT < 24) {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, u)
                        } else {
                            val contentValues = ContentValues(1)
                            contentValues.put(MediaStore.Images.Media.DATA, tempFile?.absolutePath)
                            val uri = this@SelectPhotoWheelDialog.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
                        }
                        startActivityForResult(intent, 0)
                    } else {
                        toast("没有访问设备相机权限")
                    }
                }else
                    RxPermissions(this@SelectPhotoWheelDialog).request(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE).subscribe {
                        if (it) {
                            val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)// 调用android的图库
                            intent.type = "image/*"
                            startActivityForResult(intent, 1)
                        }else{
                            toast("没有访问设备存储权限")
                        }
                    }
 
        }
 
//        //选择文件
//        val file = find<TextView>(R.id.btn_file)
//        if (useFile)
//            file.visible()
//        file.onClick {
//            RxPermissions(this@SelectPhotoDialog).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe {
//                if (it) {
//                    val intent = Intent(Intent.ACTION_GET_CONTENT)// 调用android的文件选择器
//                    intent.addCategory(Intent.CATEGORY_OPENABLE)
//                    intent.type = "*/*"
//                    startActivityForResult(intent, 2)
//                }else{
//                    toast("没有访问设备存储权限")
//                }
//            }
//        }
        //取消
        find<TextView>(R.id.tv_cancel).onClick {
            onBackPressed()
        }
    }
 
    override fun exitAnim(): Int {
        return R.anim.popup_out
    }
 
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 12) {
            if (TextUtils.equals(permissions[0], Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                //用户不同意,向用户展示该权限作用
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    alertDialog("请注意", "本应用需要使用访问本地存储权限,否则无法正常使用!", false, "确定", "取消", DialogInterface.OnClickListener { _, _ -> finish() }, DialogInterface.OnClickListener { _, _ -> finish() })
                    return
                }
                finish()
            }
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                0 -> {
                    //拍照
                    if (tempFile != null && tempFile!!.exists()) {
                        val intent = Intent()
                        intent.putExtra(PATH, tempFile!!.absolutePath)
                        setResult(RESULT_OK, intent)
                        onBackPressed()
                        overridePendingTransition(0, 0)
                    }
                }
                2 -> {//文件
                    if (data != null) {
                        val uri = data.data
                        if (uri != null) {
                            val path = getUrlPath(uri)
                            Log.e("mmp","path;"+path)
                            if (path != null) {
                                val typeIndex = path.lastIndexOf(".")
                                if (typeIndex != -1) {
                                    val fileType = path.substring(typeIndex + 1).toLowerCase(Locale.CHINA)
                                    //某些设备选择图片是可以选择一些非图片的文件。然后发送出去或出错。这里简单的通过匹配后缀名来判断是否是图片文件
                                    //如果是图片文件则发送。反之给出提示
                                    if (fileType == "jpg" || fileType == "gif"
                                            || fileType == "png" || fileType == "jpeg"
                                            || fileType == "bmp" || fileType == "wbmp"
                                            || fileType == "ico" || fileType == "jpe"
                                            || fileType == "pdf"|| fileType == "doc"|| fileType == "docx"
                                        || fileType == "xls"|| fileType == "xlsx"
                                        || fileType == "ppt"|| fileType == "pptx") {
                                        val intent = Intent()
                                        intent.putExtra(PATH, path)
                                        setResult(RESULT_OK, intent)
                                        finish()
                                        overridePendingTransition(0, 0)
                                        //                                        cropImage(path);
                                        //                                        BitmapUtil.getInstance(this).loadImage(iv_image, path);
                                    } else {
                                        toast("仅支持png、jpg、pdf、doc、excel、ppt文件!")
                                    }
                                } else {
                                    toast("仅支持png、jpg、pdf、doc、excel、ppt文件!")
                                }
                            } else {
                                toast("无法访问该文件路径")
                            }
                        } else {
                            toast("无法访问该文件路径")
                        }
                    }
                }
                1->{
                    if (data != null) {
                        val uri = data.data
                        if (uri != null) {
                            val path = getUrlPath(uri)
                            if (path != null) {
                                val typeIndex = path.lastIndexOf(".")
                                if (typeIndex != -1) {
                                    val fileType = path.substring(typeIndex + 1).toLowerCase(Locale.CHINA)
                                    //某些设备选择图片是可以选择一些非图片的文件。然后发送出去或出错。这里简单的通过匹配后缀名来判断是否是图片文件
                                    //如果是图片文件则发送。反之给出提示
                                    if (fileType == "jpg" || fileType == "gif"
                                        || fileType == "png" || fileType == "jpeg"
                                        || fileType == "bmp" || fileType == "wbmp"
                                        || fileType == "ico" || fileType == "jpe") {
                                        val intent = Intent()
                                        intent.putExtra(PATH, path)
                                        setResult(RESULT_OK, intent)
                                        finish()
                                        overridePendingTransition(0, 0)
                                        //                                        cropImage(path);
                                        //                                        BitmapUtil.getInstance(this).loadImage(iv_image, path);
                                    } else {
                                        toast("无法识别的图片类型!")
                                    }
                                } else {
                                    toast("无法识别的图片类型!")
                                }
                            } else {
                                toast("无法识别的图片类型或路径!")
                            }
                        } else {
                            toast("无法识别的图片类型!")
                        }
                    }
                }
            }
        }
    }
}