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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package cn.sinata.xldutils.activity
 
import android.Manifest
import android.annotation.TargetApi
import android.app.Activity
import android.content.*
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.provider.Settings
import androidx.core.app.ActivityCompat
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import androidx.loader.content.CursorLoader
import cn.sinata.xldutils.R
import cn.sinata.xldutils.utils.*
import cn.sinata.xldutils.widget.TipDialog
import com.tbruyelle.rxpermissions2.RxPermissions
import com.ypx.imagepicker.ImagePicker
import org.jetbrains.anko.bundleOf
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.toast
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStream
import java.util.*
 
class SelectPhotoDialog : DialogActivity() {
 
    private var tempFile: File? = null
 
    private var isRefused = false //全部文件管理权限
 
    companion object {
        val PATH = "path"
    }
 
    private val useFile by lazy {
        intent.getBooleanExtra("useFile", false)
    }
    private val title by lazy {
        intent.getStringExtra("title") ?: "上传头像"
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        setShowSOP(false)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_photo_dialog)
        window.setGravity(Gravity.BOTTOM)
        find<TextView>(R.id.tv_title).text = title
        find<TextView>(android.R.id.button1).onClick {
            val rxPermissions = RxPermissions(this@SelectPhotoDialog)
            val fileGranted = true
            val cameraGranted = rxPermissions.isGranted(Manifest.permission.CAMERA)
            if ((SPUtils.instance().getBoolean("FILE_REFUSE") && !fileGranted)) {
                myToast("缺少文件权限")
                return@onClick
            }
            if ((SPUtils.instance().getBoolean("CAMERA_REFUSE") && !cameraGranted)) {
                myToast("缺少相机权限")
                return@onClick
            }
            if (fileGranted && cameraGranted) {
                takePhoto()
            } else {
                val tipDialog = TipDialog()
                tipDialog.arguments =
                    bundleOf("msg" to "为了拍摄照片上传,我们需要获取相机访问权限", "ok" to "去授权", "cancel" to "不需要")
                tipDialog.setCallback(object : TipDialog.OnClickCallback {
                    override fun onOk() {
                        rxPermissions.request(Manifest.permission.CAMERA).subscribe {
                            if (it) {//有权限
                                //检测路径是否存在,不存在就创建
                                takePhoto()
                            } else {
                                toast("缺少相机权限")
                                SPUtils.instance().put("CAMERA_REFUSE", true).apply()
                            }
                        }
                    }
 
                    override fun onCancel() {
                        SPUtils.instance().put("CAMERA_REFUSE", true).apply()
                    }
                })
                tipDialog.show(supportFragmentManager, "camera")
            }
        }
 
        find<TextView>(android.R.id.button2).onClick {
            val rxPermissions = RxPermissions(this@SelectPhotoDialog)
            val fileGranted = rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            if (SPUtils.instance().getBoolean("FILE_REFUSE") && !fileGranted) {
                myToast("缺少文件权限")
                return@onClick
            }
            if (fileGranted) {
                val intent = Intent(
                    Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                )// 调用android的图库
                intent.type = "image/*"
                startActivityForResult(intent, 1)
            } else {
                val tipDialog = TipDialog()
                tipDialog.arguments =
                    bundleOf("msg" to "为了访问相册选择图片上传,我们需要获取文件读写权限", "ok" to "去授权", "cancel" to "不需要")
                tipDialog.setCallback(object : TipDialog.OnClickCallback {
                    override fun onOk() {
                        rxPermissions.request(Manifest.permission.WRITE_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 {
                                    myToast("缺少文件权限")
                                    SPUtils.instance().put("FILE_REFUSE", true).apply()
                                }
                            }
                    }
 
                    override fun onCancel() {
                        SPUtils.instance().put("FILE_REFUSE", true).apply()
                    }
                })
                tipDialog.show(supportFragmentManager, "photo")
            }
        }
 
        //取消
        find<TextView>(android.R.id.button3).onClick {
            onBackPressed()
        }
    }
 
    private fun takePhoto() {
        var name = System.currentTimeMillis().toString();//可为null
        var isCopyInDCIM = true;//copy一份保存到系统相册文件
        ImagePicker.takePhoto(this, name, isCopyInDCIM) {
            var pathItem = getRealPathFromUri(this, it[0].uri)
            var uri =  it[0].uri
            pathItem?.let {
                //拍照
                val intent = Intent()
                intent.putExtra(PATH, pathItem)
                setResult(RESULT_OK, intent)
                finish()
                overridePendingTransition(0, 0)
            }
        }
    }
 
    open fun copyFile(oldPath: String?, newPath: String?) {
        try {
            var bytesum = 0
            var byteread = 0
            val oldfile = File(oldPath)
            if (oldfile.exists()) { //文件存在时
                val inStream: InputStream = FileInputStream(oldPath) //读入原文件
                val fs = FileOutputStream(newPath)
                val buffer = ByteArray(1444)
                while (inStream.read(buffer).also { byteread = it } != -1) {
                    bytesum += byteread //字节数 文件大小
                    println(bytesum)
                    fs.write(buffer, 0, byteread)
                }
                inStream.close()
            }
        } catch (e: java.lang.Exception) {
            println("复制单个文件操作出错")
            e.printStackTrace()
        }
    }
 
    //复杂版处理  (适配多种API)
    open fun getRealPathFromUri(
        context: Context,
        uri: Uri
    ): String? {
        val sdkVersion: Int = Build.VERSION.SDK_INT
        if (sdkVersion < 11) return getRealPathFromUri_BelowApi11(context, uri)
        return if (sdkVersion < 19) getRealPathFromUri_Api11To18(
            context,
            uri
        ) else getRealPathFromUri_AboveApi19(context, uri)
    }
    /**
     * 适配api11以下(不包括api11),根据uri获取图片的绝对路径
     */
    private fun getRealPathFromUri_BelowApi11(
        context: Context,
        uri: Uri
    ): String? {
        var filePath: String? = null
        val projection =
            arrayOf<String>(MediaStore.Images.Media.DATA)
        val cursor: Cursor? = context.contentResolver.query(uri, projection, null, null, null)
        if (cursor != null && cursor.moveToFirst()) {
            filePath = cursor.getString(cursor.getColumnIndex(projection[0]))
            cursor.close()
        }
        return filePath
    }
    /**
     * 适配api11-api18,根据uri获取图片的绝对路径
     */
    private fun getRealPathFromUri_Api11To18(
        context: Context,
        uri: Uri
    ): String? {
        var filePath: String? = null
        val projection =
            arrayOf<String>(MediaStore.Images.Media.DATA)
        //这个有两个包不知道是哪个。。。。不过这个复杂版一般用不到
        val loader = CursorLoader(context, uri, projection, null, null, null)
        val cursor: Cursor? = loader.loadInBackground()
        if (cursor != null) {
            cursor.moveToFirst()
            filePath = cursor.getString(cursor.getColumnIndex(projection[0]))
            cursor.close()
        }
        return filePath
    }
    /**
     * 适配api19以上,根据uri获取图片的绝对路径
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private fun getRealPathFromUri_AboveApi19(
        context: Context,
        uri: Uri
    ): String? {
        if (DocumentsContract.isDocumentUri(context, uri)) {
            if (isExternalStorageDocument(uri)) {
                val docId: String = DocumentsContract.getDocumentId(uri)
                val split = docId.split(":").toTypedArray()
                val type = split[0]
                if ("primary".equals(type, ignoreCase = true)) {
                    return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
                }
            } else if (isDownloadsDocument(uri)) {
                val id: String = DocumentsContract.getDocumentId(uri)
                val contentUri: Uri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"),
                    java.lang.Long.valueOf(id)
                )
                return getDataColumn(context, contentUri, null, null)
            } else if (isMediaDocument(uri)) {
                val docId: String = DocumentsContract.getDocumentId(uri)
                val split = docId.split(":").toTypedArray()
                val type = split[0]
                val contentUri: Uri
                contentUri = if ("image" == type) {
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                } else if ("video" == type) {
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI
                } else if ("audio" == type) {
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
                } else {
                    MediaStore.Files.getContentUri("external")
                }
                val selection = "_id=?"
                val selectionArgs =
                    arrayOf(split[1])
                return getDataColumn(context, contentUri, selection, selectionArgs)
            }
        } else if ("content".equals(uri.scheme, ignoreCase = true)) {
            return getDataColumn(context, uri, null, null)
        } else if ("file".equals(uri.scheme, ignoreCase = true)) {
            return uri.path
        }
        return null
    }
 
    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)
                        finish()
                        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("无法识别的图片类型!")
                        }
                    }
                }
            }
        }
    }
}