Skip to content

Commit a041668

Browse files
committed
解决bug
1 parent b467d4d commit a041668

File tree

3 files changed

+90
-63
lines changed

3 files changed

+90
-63
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import butterknife.OnClick
99
import com.bumptech.glide.Glide
1010
import com.haoge.easyandroid.easy.EasyLog
1111
import com.haoge.easyandroid.easy.EasyPermissions
12-
import com.haoge.easyandroid.easy.EasyMedia
12+
import com.haoge.easyandroid.easy.EasyMediaFile
1313
import com.haoge.easyandroid.easy.EasyToast
1414
import com.haoge.sample.easyandroid.BaseActivity
1515
import com.haoge.sample.easyandroid.R
@@ -25,7 +25,9 @@ class EasyPhotoActivity : BaseActivity() {
2525

2626
private val switcher by lazy { findViewById<TextView>(R.id.indicate_img_path) }
2727
private var indicatePath:String? = null
28-
private val photo = EasyMedia().setCallback {
28+
private val photo = EasyMediaFile().setError {
29+
EasyLog.DEFAULT.e(it.message)
30+
}.setCallback {
2931
showImg(it)
3032
}
3133

@@ -114,7 +116,7 @@ class EasyPhotoActivity : BaseActivity() {
114116

115117
@OnClick(R.id.selectNormalFile)
116118
fun selectNormalFile() {
117-
photo.selectFile(this)
119+
// photo.selectFile(this)
118120
}
119121

120122
/**

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.3.10'
4+
ext.kotlin_version = '1.3.50'
55
repositories {
66
mavenCentral()
77
google()

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

+84-59
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.haoge.easyandroid.easy
2020
import android.app.Activity
2121
import android.app.Fragment
2222
import android.content.*
23+
import android.content.pm.PackageManager
2324
import android.graphics.Bitmap
2425
import android.net.Uri
2526
import android.os.*
@@ -33,7 +34,7 @@ import java.util.*
3334
* 描述:多媒体选择工具类
3435
* @author:Vincent
3536
*/
36-
class EasyMedia {
37+
class EasyMediaFile {
3738
/**
3839
* 设置图片选择结果回调
3940
*/
@@ -48,17 +49,17 @@ class EasyMedia {
4849

4950
private val mainHandler = Handler(Looper.getMainLooper())
5051

51-
fun setError(error: ((error: Exception) -> Unit)?): EasyMedia {
52+
fun setError(error: ((error: Exception) -> Unit)?): EasyMediaFile {
5253
this.error = error
5354
return this
5455
}
5556

56-
fun setCallback(callback: ((file: File) -> Unit)): EasyMedia {
57+
fun setCallback(callback: ((file: File) -> Unit)): EasyMediaFile {
5758
this.callback = callback
5859
return this
5960
}
6061

61-
fun setCrop(isCrop: Boolean): EasyMedia {
62+
fun setCrop(isCrop: Boolean): EasyMediaFile {
6263
this.isCrop = isCrop
6364
return this
6465
}
@@ -68,7 +69,7 @@ class EasyMedia {
6869
*
6970
* @param imgPath 图片的存储路径(包括文件名和后缀)
7071
*/
71-
fun setFilePath(imgPath: String?): EasyMedia {
72+
fun setFilePath(imgPath: String?): EasyMediaFile {
7273
if (imgPath.isNullOrEmpty()) {
7374
this.mFilePath = null
7475
} else {
@@ -83,19 +84,19 @@ class EasyMedia {
8384
* 选择文件
8485
* 支持图片、音频、视频
8586
*/
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+
// }
99100

100101
/**
101102
* 选择视频
@@ -106,9 +107,9 @@ class EasyMedia {
106107
setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*")
107108
}
108109
if (Looper.myLooper() == Looper.getMainLooper()) {
109-
selectFileInternal(intent, activity)
110+
selectFileInternal(intent, activity, 2)
110111
} else {
111-
mainHandler.post { selectFileInternal(intent, activity) }
112+
mainHandler.post { selectFileInternal(intent, activity, 2) }
112113
}
113114
}
114115

@@ -121,9 +122,9 @@ class EasyMedia {
121122
setDataAndType(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, "audio/*")
122123
}
123124
if (Looper.myLooper() == Looper.getMainLooper()) {
124-
selectFileInternal(intent, activity)
125+
selectFileInternal(intent, activity, 1)
125126
} else {
126-
mainHandler.post { selectFileInternal(intent, activity) }
127+
mainHandler.post { selectFileInternal(intent, activity, 1) }
127128
}
128129
}
129130

@@ -135,35 +136,59 @@ class EasyMedia {
135136
setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
136137
}
137138
if (Looper.myLooper() == Looper.getMainLooper()) {
138-
selectFileInternal(intent, activity)
139+
selectFileInternal(intent, activity, 0)
139140
} else {
140-
mainHandler.post { selectFileInternal(intent, activity) }
141+
mainHandler.post { selectFileInternal(intent, activity, 0) }
141142
}
142143
}
143144

144145

145-
146146
/**
147147
* 选择文件
148148
*/
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)
162188
}
163-
} catch (e: Exception) {
164-
error?.invoke(e)
165189
}
166190
}
191+
167192
}
168193

169194
private fun uriToFile(activity: Activity, uri: Uri): File {
@@ -276,9 +301,9 @@ class EasyMedia {
276301
// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
277302
}
278303
if (Looper.myLooper() == Looper.getMainLooper()) {
279-
takeFileInternal(imgFile, intent, activity,1)
304+
takeFileInternal(imgFile, intent, activity, 1)
280305
} else {
281-
mainHandler.post { takeFileInternal(imgFile, intent, activity,1) }
306+
mainHandler.post { takeFileInternal(imgFile, intent, activity, 1) }
282307
}
283308

284309
}
@@ -304,9 +329,9 @@ class EasyMedia {
304329
// putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10000)
305330
}
306331
if (Looper.myLooper() == Looper.getMainLooper()) {
307-
takeFileInternal(imgFile, intent, activity,2)
332+
takeFileInternal(imgFile, intent, activity, 2)
308333
} else {
309-
mainHandler.post { takeFileInternal(imgFile, intent, activity,2) }
334+
mainHandler.post { takeFileInternal(imgFile, intent, activity, 2) }
310335
}
311336

312337
}
@@ -389,9 +414,9 @@ class EasyMedia {
389414
}
390415

391416
if (Looper.myLooper() == Looper.getMainLooper()) {
392-
takeFileInternal(imgFile, intent, activity,1)
417+
takeFileInternal(imgFile, intent, activity, 1)
393418
} else {
394-
mainHandler.post { takeFileInternal(imgFile, intent, activity,1) }
419+
mainHandler.post { takeFileInternal(imgFile, intent, activity, 1) }
395420
}
396421
}
397422

@@ -430,32 +455,32 @@ class EasyMedia {
430455
}
431456

432457
if (Looper.myLooper() == Looper.getMainLooper()) {
433-
takeFileInternal(imgFile, intent, activity,2)
458+
takeFileInternal(imgFile, intent, activity, 2)
434459
} else {
435-
mainHandler.post { takeFileInternal(imgFile, intent, activity,2) }
460+
mainHandler.post { takeFileInternal(imgFile, intent, activity, 2) }
436461
}
437462
}
438463

439464
/**
440465
* 向系统发出指令
441466
*/
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) {
443468
val fragment = PhotoFragment.findOrCreate(activity)
444469
fragment.start(intent, PhotoFragment.REQ_TAKE_FILE) { requestCode: Int, data: Intent? ->
445470
if (requestCode == PhotoFragment.REQ_TAKE_FILE) {
446-
if(data?.data != null){
471+
if (data?.data != null) {
447472
mFilePath = when (type) {
448473
0 -> {
449-
uriToFile(activity,data.data)
474+
uriToFile(activity, data.data!!)
450475
}
451-
else -> uriToFile(activity, data.data,type)
476+
else -> uriToFile(activity, data.data!!, type)
452477
}
453478

454479

455-
if(isCrop){
480+
if (isCrop) {
456481
zoomPhoto(takePhotoPath, mFilePath
457482
?: File(generateFilePath(activity)), activity)
458-
}else{
483+
} else {
459484
callback?.invoke(mFilePath!!)
460485
mFilePath = null
461486
}
@@ -471,20 +496,20 @@ class EasyMedia {
471496
}
472497
}
473498

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)
477502
val path = if (cursor == null) {
478503
data.path
479504
} 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)
481506
cursor.moveToFirst()
482507
cursor.getString(index)
483508
}
484509
// 手动关掉报错如下
485510
// Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
486511
// cursor.close()
487-
return File(path)
512+
return File(path!!)
488513
}
489514

490515
/***

0 commit comments

Comments
 (0)