Skip to content

Commit 838ed8c

Browse files
committed
Optimal EasyPhoto
1 parent 0d3647f commit 838ed8c

File tree

4 files changed

+25
-81
lines changed

4 files changed

+25
-81
lines changed

app/src/main/java/com/haoge/sample/easyandroid/DemosActivity.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class DemosActivity : ListActivity() {
3030
Item("测试EasyExecutor", EasyExecutorActivity::class.java),
3131
Item("测试EasyBundle", EasyBundleActivity::class.java),
3232
Item("测试EasySharedPreferences", EasySharedPreferencesActivity::class.java),
33-
Item("测试MVPDemo", MVPDemoActivity::class.java),
34-
Item("测试EasyPhotoDemo", EasyPhotoActivity::class.java)
33+
Item("测试EasyPhoto", EasyPhotoActivity::class.java),
34+
Item("测试MVPDemo", MVPDemoActivity::class.java)
3535
)
3636

3737
override fun onCreate(savedInstanceState: Bundle?) {

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

+11-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.haoge.easyandroid.easy.EasyPhoto
1010
import com.haoge.sample.easyandroid.BaseActivity
1111
import com.haoge.sample.easyandroid.R
1212
import kotlinx.android.synthetic.main.activity_easy_photo.*
13+
import java.io.File
1314

1415

1516
/**
@@ -35,47 +36,42 @@ class EasyPhotoActivity : BaseActivity() {
3536

3637
@OnClick(R.id.takePhoto)
3738
fun takePhoto() {
38-
EasyPhoto(false).setCallback { outputUri: Uri? ->
39-
showImg(outputUri)
39+
EasyPhoto(false).setCallback { outputFile: File? ->
40+
showImg(outputFile)
4041
}.takePhoto(this)
4142

4243
}
4344

4445
@OnClick(R.id.selectPhoto)
4546
fun selectPhoto() {
46-
EasyPhoto(false).setCallback { outputUri: Uri? ->
47-
showImg(outputUri)
47+
EasyPhoto(false).setCallback { outputFile: File? ->
48+
showImg(outputFile)
4849
}.selectPhoto(this)
4950

5051
}
5152

5253
@OnClick(R.id.takePhoto_zoom)
5354
fun takePhotoZoom() {
54-
EasyPhoto(true).setCallback {outputUri: Uri? ->
55-
showImg(outputUri)
55+
EasyPhoto(true).setCallback {outputFile: File? ->
56+
showImg(outputFile)
5657
}.setDimens(800, 400, 2, 1)
5758
.takePhoto(this)
58-
5959
}
6060

6161
@OnClick(R.id.selectPhoto_zoom)
6262
fun selectPhotoZoom() {
63-
EasyPhoto(true).setCallback { outputUri: Uri? ->
64-
showImg(outputUri)
63+
EasyPhoto(true).setCallback { outputFile: File? ->
64+
showImg(outputFile)
6565
}.selectPhoto(this)
6666
}
6767

6868
/**
6969
* 加载图片
7070
*/
7171

72-
private fun showImg(outputUri: Uri?) {
72+
private fun showImg(outputFile: File?) {
7373
//加载图片
74-
Glide.with(showImg).load(outputUri).into(showImg)
75-
76-
74+
Glide.with(showImg).load(outputFile).into(showImg)
7775
}
7876

79-
80-
8177
}

docs/EasyPhoto.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ val photo = EasyPhoto(isCrop)
5555
.setImgPath(imgPath:String)
5656
5757
// 跳转拍照并获取图片
58-
photo.takePhoto(activity)//
58+
photo.takePhoto(activity)
5959
6060
// 或者跳转图库进行图片选择
6161
photo.selectPhoto(activity)
@@ -66,4 +66,6 @@ photo.selectPhoto(activity)
6666
1. isCrop:Boolean -> 是否为裁剪模式, 档位true时。表示是裁剪模式。选择好图片后将会进行裁剪处理
6767
2. aspectX, aspectY -> 剪裁图片的宽高比。
6868
3. outputX, outputY -> 剪裁图片大小
69-
3. imgPath -> 修改图片的存储路径(默认的图片存储路径是SD卡上 Android/data/应用包名/时间戳.jpg)
69+
3. imgPath -> 修改图片的存储路径(默认的图片存储路径是SD卡上 Android/data/应用包名/时间戳.jpg)
70+
71+
请注意:启动拍照或图库选择时。传入的activity需要为当前正在展示的页面的activity实例!

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

+8-62
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EasyPhoto(private val isCrop: Boolean) {
2424
/**
2525
* 设置图片选择结果回调
2626
*/
27-
private var uriCallback: ((outputUri: Uri?) -> Unit)? = null
27+
private var callback: ((file: File?) -> Unit)? = null
2828

2929
/**
3030
* 拍照或剪切后图片的存放位置(参考file_provider_paths.xml中的路径)
@@ -45,8 +45,8 @@ class EasyPhoto(private val isCrop: Boolean) {
4545

4646
private val mainHandler by lazy { return@lazy Handler(Looper.getMainLooper()) }
4747

48-
fun setCallback(callback: ((outputUri: Uri?) -> Unit)): EasyPhoto {
49-
this.uriCallback = callback
48+
fun setCallback(callback: ((file: File?) -> Unit)): EasyPhoto {
49+
this.callback = callback
5050
return this
5151
}
5252

@@ -93,6 +93,7 @@ class EasyPhoto(private val isCrop: Boolean) {
9393

9494
val sourceUri = data.data
9595
val projection = arrayOf(MediaStore.Images.Media.DATA)
96+
@Suppress("DEPRECATION")
9697
val cursor = activity.managedQuery(sourceUri, projection, null, null, null)
9798

9899
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
@@ -103,7 +104,7 @@ class EasyPhoto(private val isCrop: Boolean) {
103104
if (isCrop) {//裁剪
104105
zoomPhoto(inputFile, mImgPath?:File(generateImagePath(activity)), activity)
105106
} else {//不裁剪
106-
uriCallback?.invoke(Uri.fromFile(inputFile))
107+
callback?.invoke(inputFile)
107108
}
108109
}
109110

@@ -145,8 +146,7 @@ class EasyPhoto(private val isCrop: Boolean) {
145146
if (isCrop) {
146147
zoomPhoto(takePhotoPath, mImgPath?: File(generateImagePath(activity)), activity)
147148
} else {
148-
val outputUri = Uri.fromFile(takePhotoPath)
149-
uriCallback?.invoke(outputUri)
149+
callback?.invoke(takePhotoPath)
150150
}
151151
}
152152
}
@@ -184,9 +184,7 @@ class EasyPhoto(private val isCrop: Boolean) {
184184
PhotoFragment.findOrCreate(activity).start(intent, PhotoFragment.REQ_ZOOM_PHOTO) { requestCode: Int, data: Intent? ->
185185
if (requestCode == PhotoFragment.REQ_ZOOM_PHOTO) {
186186
data ?: return@start
187-
188-
val outputUri = Uri.fromFile(outputFile)
189-
uriCallback?.invoke(outputUri)
187+
callback?.invoke(outputFile)
190188
}
191189
}
192190
}
@@ -217,7 +215,6 @@ class EasyPhoto(private val isCrop: Boolean) {
217215
/**
218216
* 安卓7.0裁剪根据文件路径获取uri
219217
*/
220-
221218
private fun getImageContentUri(context: Context, imageFile: File?): Uri? {
222219
val filePath = imageFile?.absolutePath
223220
val cursor = context.contentResolver.query(
@@ -226,7 +223,7 @@ class EasyPhoto(private val isCrop: Boolean) {
226223
MediaStore.Images.Media.DATA + "=? ",
227224
arrayOf(filePath), null)
228225

229-
cursor.use { cursor ->
226+
cursor.use { _ ->
230227
return if (cursor != null && cursor.moveToFirst()) {
231228
val id = cursor.getInt(cursor
232229
.getColumnIndex(MediaStore.MediaColumns._ID))
@@ -248,57 +245,6 @@ class EasyPhoto(private val isCrop: Boolean) {
248245
}
249246
}
250247

251-
252-
companion object {
253-
fun uriToFile(uri: Uri?, context: Context): File? {
254-
uri ?: return null
255-
var path: String? = null
256-
if ("file" == uri.scheme) {
257-
path = uri.encodedPath
258-
if (path != null) {
259-
path = Uri.decode(path)
260-
val cr = context.contentResolver
261-
val buff = StringBuffer()
262-
buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'$path'").append(")")
263-
val cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, arrayOf(MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA), buff.toString(), null, null)
264-
var index = 0
265-
var dataIdx = 0
266-
cur!!.moveToFirst()
267-
while (!cur.isAfterLast) {
268-
index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID)
269-
index = cur.getInt(index)
270-
dataIdx = cur.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
271-
path = cur.getString(dataIdx)
272-
cur.moveToNext()
273-
}
274-
cur.close()
275-
if (index == 0) {
276-
} else {
277-
val u = Uri.parse("content://media/external/images/media/$index")
278-
println("temp uri is :$u")
279-
}
280-
}
281-
if (path != null) {
282-
return File(path)
283-
}
284-
} else if ("content" == uri.scheme) {
285-
// 4.2.2以后
286-
val proj = arrayOf(MediaStore.Images.Media.DATA)
287-
val cursor = context.contentResolver.query(uri, proj, null, null, null)
288-
if (cursor!!.moveToFirst()) {
289-
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
290-
path = cursor.getString(columnIndex)
291-
}
292-
cursor.close()
293-
294-
return File(path!!)
295-
} else {
296-
//Log.i(TAG, "Uri Scheme:" + uri.getScheme());
297-
}
298-
return null
299-
}
300-
}
301-
302248
/**
303249
* 用于获取图片的Fragment
304250
*/

0 commit comments

Comments
 (0)