@@ -20,6 +20,7 @@ package com.haoge.easyandroid.easy
20
20
import android.app.Activity
21
21
import android.app.Fragment
22
22
import android.content.*
23
+ import android.content.pm.PackageManager
23
24
import android.graphics.Bitmap
24
25
import android.net.Uri
25
26
import android.os.*
@@ -33,7 +34,7 @@ import java.util.*
33
34
* 描述:多媒体选择工具类
34
35
* @author:Vincent
35
36
*/
36
- class EasyMedia {
37
+ class EasyMediaFile {
37
38
/* *
38
39
* 设置图片选择结果回调
39
40
*/
@@ -48,17 +49,17 @@ class EasyMedia {
48
49
49
50
private val mainHandler = Handler (Looper .getMainLooper())
50
51
51
- fun setError (error : ((error: Exception ) -> Unit )? ): EasyMedia {
52
+ fun setError (error : ((error: Exception ) -> Unit )? ): EasyMediaFile {
52
53
this .error = error
53
54
return this
54
55
}
55
56
56
- fun setCallback (callback : ((file: File ) -> Unit )): EasyMedia {
57
+ fun setCallback (callback : ((file: File ) -> Unit )): EasyMediaFile {
57
58
this .callback = callback
58
59
return this
59
60
}
60
61
61
- fun setCrop (isCrop : Boolean ): EasyMedia {
62
+ fun setCrop (isCrop : Boolean ): EasyMediaFile {
62
63
this .isCrop = isCrop
63
64
return this
64
65
}
@@ -68,7 +69,7 @@ class EasyMedia {
68
69
*
69
70
* @param imgPath 图片的存储路径(包括文件名和后缀)
70
71
*/
71
- fun setFilePath (imgPath : String? ): EasyMedia {
72
+ fun setFilePath (imgPath : String? ): EasyMediaFile {
72
73
if (imgPath.isNullOrEmpty()) {
73
74
this .mFilePath = null
74
75
} else {
@@ -83,19 +84,19 @@ class EasyMedia {
83
84
* 选择文件
84
85
* 支持图片、音频、视频
85
86
*/
86
- fun selectFile (activity : Activity ) {
87
- isCrop = false
88
- val intent = Intent (Intent .ACTION_PICK , null ).apply {
89
- setDataAndType(MediaStore .Images .Media .EXTERNAL_CONTENT_URI , " */*" )
90
- setDataAndType(MediaStore .Audio .Media .EXTERNAL_CONTENT_URI , " */*" )
91
- setDataAndType(MediaStore .Video .Media .EXTERNAL_CONTENT_URI , " */*" )
92
- }
93
- if (Looper .myLooper() == Looper .getMainLooper()) {
94
- selectFileInternal(intent, activity)
95
- } else {
96
- mainHandler.post { selectFileInternal(intent, activity) }
97
- }
98
- }
87
+ // fun selectFile(activity: Activity) {
88
+ // isCrop = false
89
+ // val intent = Intent(Intent.ACTION_PICK, null).apply {
90
+ // setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "*/*")
91
+ // setDataAndType(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, "*/*")
92
+ // setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "*/*")
93
+ // }
94
+ // if (Looper.myLooper() == Looper.getMainLooper()) {
95
+ // selectFileInternal(intent, activity, -1 )
96
+ // } else {
97
+ // mainHandler.post { selectFileInternal(intent, activity, -1 ) }
98
+ // }
99
+ // }
99
100
100
101
/* *
101
102
* 选择视频
@@ -106,9 +107,9 @@ class EasyMedia {
106
107
setDataAndType(MediaStore .Video .Media .EXTERNAL_CONTENT_URI , " video/*" )
107
108
}
108
109
if (Looper .myLooper() == Looper .getMainLooper()) {
109
- selectFileInternal(intent, activity)
110
+ selectFileInternal(intent, activity, 2 )
110
111
} else {
111
- mainHandler.post { selectFileInternal(intent, activity) }
112
+ mainHandler.post { selectFileInternal(intent, activity, 2 ) }
112
113
}
113
114
}
114
115
@@ -121,9 +122,9 @@ class EasyMedia {
121
122
setDataAndType(MediaStore .Audio .Media .EXTERNAL_CONTENT_URI , " audio/*" )
122
123
}
123
124
if (Looper .myLooper() == Looper .getMainLooper()) {
124
- selectFileInternal(intent, activity)
125
+ selectFileInternal(intent, activity, 1 )
125
126
} else {
126
- mainHandler.post { selectFileInternal(intent, activity) }
127
+ mainHandler.post { selectFileInternal(intent, activity, 1 ) }
127
128
}
128
129
}
129
130
@@ -135,35 +136,59 @@ class EasyMedia {
135
136
setDataAndType(MediaStore .Images .Media .EXTERNAL_CONTENT_URI , " image/*" )
136
137
}
137
138
if (Looper .myLooper() == Looper .getMainLooper()) {
138
- selectFileInternal(intent, activity)
139
+ selectFileInternal(intent, activity, 0 )
139
140
} else {
140
- mainHandler.post { selectFileInternal(intent, activity) }
141
+ mainHandler.post { selectFileInternal(intent, activity, 0 ) }
141
142
}
142
143
}
143
144
144
145
145
-
146
146
/* *
147
147
* 选择文件
148
148
*/
149
- private fun selectFileInternal (intent : Intent , activity : Activity ) {
150
- PhotoFragment .findOrCreate(activity).start(intent, PhotoFragment .REQ_SELECT_FILE ) { requestCode: Int , data: Intent ? ->
151
- if (requestCode != PhotoFragment .REQ_SELECT_FILE ) {
152
- return @start
153
- }
154
- data ? : return @start
155
- try {
156
- val inputFile = File (data.data.path)
157
-
158
- if (isCrop) {// 裁剪
159
- zoomPhoto(inputFile, mFilePath ? : File (generateFilePath(activity)), activity)
160
- } else {// 不裁剪
161
- callback?.invoke(inputFile)
149
+ private fun selectFileInternal (intent : Intent , activity : Activity , type : Int ) {
150
+ val resolveInfoList = activity.packageManager.queryIntentActivities(intent, PackageManager .MATCH_DEFAULT_ONLY )
151
+ if (resolveInfoList.isEmpty()) {
152
+ error?.invoke(IllegalStateException (" No Activity found to handle Intent " ))
153
+ } else {
154
+ PhotoFragment .findOrCreate(activity).start(intent, PhotoFragment .REQ_SELECT_FILE ) { requestCode: Int , data: Intent ? ->
155
+ if (requestCode != PhotoFragment .REQ_SELECT_FILE ) {
156
+ return @start
157
+ }
158
+ data ? : return @start
159
+ data.data ? : return @start
160
+ try {
161
+ val inputFile = if (type != - 1 ) {
162
+ uriToFile(activity, data.data!! , type)
163
+ } else {
164
+ if (data.data!! .path!! .contains(" ." )) {
165
+ File (data.data!! .path!! )
166
+ } else {
167
+ when {
168
+ data.data!! .path!! .contains(" images" ) -> {
169
+ uriToFile(activity, data.data!! , 0 )
170
+ }
171
+ data.data!! .path!! .contains(" video" ) -> {
172
+ uriToFile(activity, data.data!! , 2 )
173
+ }
174
+ else -> {
175
+ uriToFile(activity, data.data!! , 1 )
176
+ }
177
+ }
178
+ }
179
+ }
180
+ if (isCrop) {// 裁剪
181
+ zoomPhoto(inputFile, mFilePath
182
+ ? : File (generateFilePath(activity)), activity)
183
+ } else {// 不裁剪
184
+ callback?.invoke(inputFile)
185
+ }
186
+ } catch (e: Exception ) {
187
+ error?.invoke(e)
162
188
}
163
- } catch (e: Exception ) {
164
- error?.invoke(e)
165
189
}
166
190
}
191
+
167
192
}
168
193
169
194
private fun uriToFile (activity : Activity , uri : Uri ): File {
@@ -276,9 +301,9 @@ class EasyMedia {
276
301
// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
277
302
}
278
303
if (Looper .myLooper() == Looper .getMainLooper()) {
279
- takeFileInternal(imgFile, intent, activity,1 )
304
+ takeFileInternal(imgFile, intent, activity, 1 )
280
305
} else {
281
- mainHandler.post { takeFileInternal(imgFile, intent, activity,1 ) }
306
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 1 ) }
282
307
}
283
308
284
309
}
@@ -304,9 +329,9 @@ class EasyMedia {
304
329
// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
305
330
}
306
331
if (Looper .myLooper() == Looper .getMainLooper()) {
307
- takeFileInternal(imgFile, intent, activity,2 )
332
+ takeFileInternal(imgFile, intent, activity, 2 )
308
333
} else {
309
- mainHandler.post { takeFileInternal(imgFile, intent, activity,2 ) }
334
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 2 ) }
310
335
}
311
336
312
337
}
@@ -389,9 +414,9 @@ class EasyMedia {
389
414
}
390
415
391
416
if (Looper .myLooper() == Looper .getMainLooper()) {
392
- takeFileInternal(imgFile, intent, activity,1 )
417
+ takeFileInternal(imgFile, intent, activity, 1 )
393
418
} else {
394
- mainHandler.post { takeFileInternal(imgFile, intent, activity,1 ) }
419
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 1 ) }
395
420
}
396
421
}
397
422
@@ -430,32 +455,32 @@ class EasyMedia {
430
455
}
431
456
432
457
if (Looper .myLooper() == Looper .getMainLooper()) {
433
- takeFileInternal(imgFile, intent, activity,2 )
458
+ takeFileInternal(imgFile, intent, activity, 2 )
434
459
} else {
435
- mainHandler.post { takeFileInternal(imgFile, intent, activity,2 ) }
460
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 2 ) }
436
461
}
437
462
}
438
463
439
464
/* *
440
465
* 向系统发出指令
441
466
*/
442
- private fun takeFileInternal (takePhotoPath : File , intent : Intent , activity : Activity ,type : Int = 0) {
467
+ private fun takeFileInternal (takePhotoPath : File , intent : Intent , activity : Activity , type : Int = 0) {
443
468
val fragment = PhotoFragment .findOrCreate(activity)
444
469
fragment.start(intent, PhotoFragment .REQ_TAKE_FILE ) { requestCode: Int , data: Intent ? ->
445
470
if (requestCode == PhotoFragment .REQ_TAKE_FILE ) {
446
- if (data?.data != null ){
471
+ if (data?.data != null ) {
447
472
mFilePath = when (type) {
448
473
0 -> {
449
- uriToFile(activity,data.data)
474
+ uriToFile(activity, data.data!! )
450
475
}
451
- else -> uriToFile(activity, data.data, type)
476
+ else -> uriToFile(activity, data.data!! , type)
452
477
}
453
478
454
479
455
- if (isCrop){
480
+ if (isCrop) {
456
481
zoomPhoto(takePhotoPath, mFilePath
457
482
? : File (generateFilePath(activity)), activity)
458
- }else {
483
+ } else {
459
484
callback?.invoke(mFilePath!! )
460
485
mFilePath = null
461
486
}
@@ -471,20 +496,20 @@ class EasyMedia {
471
496
}
472
497
}
473
498
474
- private fun uriToFile (activity : Activity , data : Uri ,type : Int ):File {
475
- val cursor = activity.managedQuery(data, arrayOf(if (type == 1 )MediaStore .Audio .Media .DATA else MediaStore .Video .Media .DATA ), null ,
476
- null , null )
499
+ private fun uriToFile (activity : Activity , data : Uri , type : Int ): File {
500
+ val cursor = activity.managedQuery(data, arrayOf(if (type == 1 ) MediaStore .Audio .Media .DATA else MediaStore .Video .Media .DATA ), null ,
501
+ null , null )
477
502
val path = if (cursor == null ) {
478
503
data.path
479
504
} else {
480
- val index = cursor.getColumnIndexOrThrow(if (type == 1 ) MediaStore .Audio .Media .DATA else MediaStore .Video .Media .DATA )
505
+ val index = cursor.getColumnIndexOrThrow(if (type == 1 ) MediaStore .Audio .Media .DATA else MediaStore .Video .Media .DATA )
481
506
cursor.moveToFirst()
482
507
cursor.getString(index)
483
508
}
484
509
// 手动关掉报错如下
485
510
// Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
486
511
// cursor.close()
487
- return File (path)
512
+ return File (path!! )
488
513
}
489
514
490
515
/* **
0 commit comments