Skip to content

Commit 797dbed

Browse files
committed
解决EasyPhoto异常并重新命名
1 parent 703b8d4 commit 797dbed

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ EasyAndroid.init(application)
6767
- [EasyPermissions](#easypermissions): 动态权限申请组件
6868
- [EasyExecutor](#easyexecutor): 线程池封装组件
6969
- [EasyBundle](#easybundle): Bundle数据存取组件
70-
- [EasyPhoto](#easyphoto): 从拍照、图库进行图片选择组件。
70+
- [EasyMedia](#EasyMedia): 从拍照、图库进行图片选择组件。
7171
- [EasyImageGetter](#easyimagegetter): TextView加载html标签时,提供`img`标签的图片加载功能
7272
- [MVP](#mvp): 简单MVP架构
7373

@@ -144,11 +144,11 @@ EasyImageGetter.create()
144144
145145
```
146146

147-
### [EasyPhoto](./docs/EasyPhoto.md)
147+
### [EasyMedia](./docs/EasyPhoto.md)
148148

149149
> [点我查看完整使用文档](./docs/EasyPhoto.md)
150150
151-
> [点击下载组件源码文件进行使用](https://raw.githubusercontent.com/yjfnypeu/EasyAndroid/master/utils/src/main/java/com/haoge/easyandroid/easy/EasyPhoto.kt)
151+
> [点击下载组件源码文件进行使用](https://raw.githubusercontent.com/yjfnypeu/EasyAndroid/master/utils/src/main/java/com/haoge/easyandroid/easy/EasyMedia.kt)
152152
153153
> 从图库或者使用相机拍照获取图片的组件。
154154
@@ -161,7 +161,7 @@ EasyImageGetter.create()
161161
用户实例:
162162

163163
```
164-
val photo = EasyPhoto()// 创建EasyPhoto实例
164+
val photo = EasyMedia()// 创建EasyMedia实例
165165
// 是否需要进行裁剪
166166
.setCrop(true|false)
167167
// 指定创建的图片地址。

app/src/main/java/com/haoge/sample/easyandroid/activities/EasyPhotoActivity.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.haoge.sample.easyandroid.activities
22

33
import android.Manifest
4-
import android.content.Intent
54
import android.os.Bundle
65
import android.os.Environment
76
import android.widget.ImageView
@@ -10,7 +9,7 @@ import butterknife.OnClick
109
import com.bumptech.glide.Glide
1110
import com.haoge.easyandroid.easy.EasyLog
1211
import com.haoge.easyandroid.easy.EasyPermissions
13-
import com.haoge.easyandroid.easy.EasyPhoto
12+
import com.haoge.easyandroid.easy.EasyMedia
1413
import com.haoge.easyandroid.easy.EasyToast
1514
import com.haoge.sample.easyandroid.BaseActivity
1615
import com.haoge.sample.easyandroid.R
@@ -26,7 +25,7 @@ class EasyPhotoActivity : BaseActivity() {
2625

2726
private val switcher by lazy { findViewById<TextView>(R.id.indicate_img_path) }
2827
private var indicatePath:String? = null
29-
private val photo = EasyPhoto().setCallback {
28+
private val photo = EasyMedia().setCallback {
3029
showImg(it)
3130
}
3231

utils/src/main/java/com/haoge/easyandroid/easy/EasyMedia.kt

+37-10
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)