@@ -84,6 +84,7 @@ class EasyMedia {
84
84
* 支持图片、音频、视频
85
85
*/
86
86
fun selectFile (activity : Activity ) {
87
+ isCrop = false
87
88
val intent = Intent (Intent .ACTION_PICK , null ).apply {
88
89
setDataAndType(MediaStore .Images .Media .EXTERNAL_CONTENT_URI , " */*" )
89
90
setDataAndType(MediaStore .Audio .Media .EXTERNAL_CONTENT_URI , " */*" )
@@ -100,6 +101,7 @@ class EasyMedia {
100
101
* 选择视频
101
102
*/
102
103
fun selectVideo (activity : Activity ) {
104
+ isCrop = false
103
105
val intent = Intent (Intent .ACTION_PICK , null ).apply {
104
106
setDataAndType(MediaStore .Video .Media .EXTERNAL_CONTENT_URI , " video/*" )
105
107
}
@@ -114,6 +116,7 @@ class EasyMedia {
114
116
* 选择音频
115
117
*/
116
118
fun selectAudio (activity : Activity ) {
119
+ isCrop = false
117
120
val intent = Intent (Intent .ACTION_PICK , null ).apply {
118
121
setDataAndType(MediaStore .Audio .Media .EXTERNAL_CONTENT_URI , " audio/*" )
119
122
}
@@ -256,6 +259,7 @@ class EasyMedia {
256
259
* 音频录制
257
260
*/
258
261
fun takeAudio (activity : Activity ) {
262
+ isCrop = false
259
263
val imgFile = mFilePath ? : File (generateFilePath(activity, 1 ))
260
264
val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
261
265
Uri .fromFile(imgFile)
@@ -272,9 +276,9 @@ class EasyMedia {
272
276
// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
273
277
}
274
278
if (Looper .myLooper() == Looper .getMainLooper()) {
275
- takeFileInternal(imgFile, intent, activity)
279
+ takeFileInternal(imgFile, intent, activity, 1 )
276
280
} else {
277
- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
281
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 1 ) }
278
282
}
279
283
280
284
}
@@ -283,6 +287,7 @@ class EasyMedia {
283
287
* 视频录制
284
288
*/
285
289
fun takeVideo (activity : Activity ) {
290
+ isCrop = false
286
291
val imgFile = mFilePath ? : File (generateFilePath(activity, 2 ))
287
292
val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
288
293
Uri .fromFile(imgFile)
@@ -299,9 +304,9 @@ class EasyMedia {
299
304
// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
300
305
}
301
306
if (Looper .myLooper() == Looper .getMainLooper()) {
302
- takeFileInternal(imgFile, intent, activity)
307
+ takeFileInternal(imgFile, intent, activity, 2 )
303
308
} else {
304
- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
309
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 2 ) }
305
310
}
306
311
307
312
}
@@ -355,6 +360,7 @@ class EasyMedia {
355
360
* 音频录制或选择
356
361
*/
357
362
fun getAudio (activity : Activity ) {
363
+ isCrop = false
358
364
val imgFile = mFilePath ? : File (generateFilePath(activity))
359
365
360
366
val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
@@ -383,16 +389,17 @@ class EasyMedia {
383
389
}
384
390
385
391
if (Looper .myLooper() == Looper .getMainLooper()) {
386
- takeFileInternal(imgFile, intent, activity)
392
+ takeFileInternal(imgFile, intent, activity, 1 )
387
393
} else {
388
- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
394
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 1 ) }
389
395
}
390
396
}
391
397
392
398
/* *
393
399
* 视频拍摄或选择
394
400
*/
395
401
fun getVideo (activity : Activity ) {
402
+ isCrop = false
396
403
val imgFile = mFilePath ? : File (generateFilePath(activity, 2 ))
397
404
398
405
val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
@@ -423,21 +430,27 @@ class EasyMedia {
423
430
}
424
431
425
432
if (Looper .myLooper() == Looper .getMainLooper()) {
426
- takeFileInternal(imgFile, intent, activity)
433
+ takeFileInternal(imgFile, intent, activity, 2 )
427
434
} else {
428
- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
435
+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 2 ) }
429
436
}
430
437
}
431
438
432
439
/* *
433
440
* 向系统发出指令
434
441
*/
435
- private fun takeFileInternal (takePhotoPath : File , intent : Intent , activity : Activity ) {
442
+ private fun takeFileInternal (takePhotoPath : File , intent : Intent , activity : Activity , type : Int = 0 ) {
436
443
val fragment = PhotoFragment .findOrCreate(activity)
437
444
fragment.start(intent, PhotoFragment .REQ_TAKE_FILE ) { requestCode: Int , data: Intent ? ->
438
445
if (requestCode == PhotoFragment .REQ_TAKE_FILE ) {
439
446
if (data?.data != null ){
440
- mFilePath = File (data.data.path)
447
+ mFilePath = when (type) {
448
+ 0 -> {
449
+ uriToFile(activity,data.data)
450
+ }
451
+ else -> uriToFile(activity, data.data,type)
452
+ }
453
+
441
454
442
455
if (isCrop){
443
456
zoomPhoto(takePhotoPath, mFilePath
@@ -458,6 +471,20 @@ class EasyMedia {
458
471
}
459
472
}
460
473
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 )
477
+ val path = if (cursor == null ) {
478
+ data.path
479
+ } else {
480
+ val index = cursor.getColumnIndexOrThrow(if (type == 1 ) MediaStore .Audio .Media .DATA else MediaStore .Video .Media .DATA )
481
+ cursor.moveToFirst()
482
+ cursor.getString(index)
483
+ }
484
+ cursor.close()
485
+ return File (path)
486
+ }
487
+
461
488
/* **
462
489
* 图片裁剪
463
490
*/
0 commit comments