@@ -84,6 +84,7 @@ class EasyMedia {
8484 * 支持图片、音频、视频
8585 */
8686 fun selectFile (activity : Activity ) {
87+ isCrop = false
8788 val intent = Intent (Intent .ACTION_PICK , null ).apply {
8889 setDataAndType(MediaStore .Images .Media .EXTERNAL_CONTENT_URI , " */*" )
8990 setDataAndType(MediaStore .Audio .Media .EXTERNAL_CONTENT_URI , " */*" )
@@ -100,6 +101,7 @@ class EasyMedia {
100101 * 选择视频
101102 */
102103 fun selectVideo (activity : Activity ) {
104+ isCrop = false
103105 val intent = Intent (Intent .ACTION_PICK , null ).apply {
104106 setDataAndType(MediaStore .Video .Media .EXTERNAL_CONTENT_URI , " video/*" )
105107 }
@@ -114,6 +116,7 @@ class EasyMedia {
114116 * 选择音频
115117 */
116118 fun selectAudio (activity : Activity ) {
119+ isCrop = false
117120 val intent = Intent (Intent .ACTION_PICK , null ).apply {
118121 setDataAndType(MediaStore .Audio .Media .EXTERNAL_CONTENT_URI , " audio/*" )
119122 }
@@ -256,6 +259,7 @@ class EasyMedia {
256259 * 音频录制
257260 */
258261 fun takeAudio (activity : Activity ) {
262+ isCrop = false
259263 val imgFile = mFilePath ? : File (generateFilePath(activity, 1 ))
260264 val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
261265 Uri .fromFile(imgFile)
@@ -272,9 +276,9 @@ class EasyMedia {
272276// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
273277 }
274278 if (Looper .myLooper() == Looper .getMainLooper()) {
275- takeFileInternal(imgFile, intent, activity)
279+ takeFileInternal(imgFile, intent, activity, 1 )
276280 } else {
277- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
281+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 1 ) }
278282 }
279283
280284 }
@@ -283,6 +287,7 @@ class EasyMedia {
283287 * 视频录制
284288 */
285289 fun takeVideo (activity : Activity ) {
290+ isCrop = false
286291 val imgFile = mFilePath ? : File (generateFilePath(activity, 2 ))
287292 val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
288293 Uri .fromFile(imgFile)
@@ -299,9 +304,9 @@ class EasyMedia {
299304// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
300305 }
301306 if (Looper .myLooper() == Looper .getMainLooper()) {
302- takeFileInternal(imgFile, intent, activity)
307+ takeFileInternal(imgFile, intent, activity, 2 )
303308 } else {
304- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
309+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 2 ) }
305310 }
306311
307312 }
@@ -355,6 +360,7 @@ class EasyMedia {
355360 * 音频录制或选择
356361 */
357362 fun getAudio (activity : Activity ) {
363+ isCrop = false
358364 val imgFile = mFilePath ? : File (generateFilePath(activity))
359365
360366 val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
@@ -383,16 +389,17 @@ class EasyMedia {
383389 }
384390
385391 if (Looper .myLooper() == Looper .getMainLooper()) {
386- takeFileInternal(imgFile, intent, activity)
392+ takeFileInternal(imgFile, intent, activity, 1 )
387393 } else {
388- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
394+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 1 ) }
389395 }
390396 }
391397
392398 /* *
393399 * 视频拍摄或选择
394400 */
395401 fun getVideo (activity : Activity ) {
402+ isCrop = false
396403 val imgFile = mFilePath ? : File (generateFilePath(activity, 2 ))
397404
398405 val imgUri = if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N ) {
@@ -423,21 +430,27 @@ class EasyMedia {
423430 }
424431
425432 if (Looper .myLooper() == Looper .getMainLooper()) {
426- takeFileInternal(imgFile, intent, activity)
433+ takeFileInternal(imgFile, intent, activity, 2 )
427434 } else {
428- mainHandler.post { takeFileInternal(imgFile, intent, activity) }
435+ mainHandler.post { takeFileInternal(imgFile, intent, activity, 2 ) }
429436 }
430437 }
431438
432439 /* *
433440 * 向系统发出指令
434441 */
435- private fun takeFileInternal (takePhotoPath : File , intent : Intent , activity : Activity ) {
442+ private fun takeFileInternal (takePhotoPath : File , intent : Intent , activity : Activity , type : Int = 0 ) {
436443 val fragment = PhotoFragment .findOrCreate(activity)
437444 fragment.start(intent, PhotoFragment .REQ_TAKE_FILE ) { requestCode: Int , data: Intent ? ->
438445 if (requestCode == PhotoFragment .REQ_TAKE_FILE ) {
439446 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+
441454
442455 if (isCrop){
443456 zoomPhoto(takePhotoPath, mFilePath
@@ -458,6 +471,20 @@ class EasyMedia {
458471 }
459472 }
460473
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+
461488 /* **
462489 * 图片裁剪
463490 */
0 commit comments