Skip to content

Commit b747544

Browse files
authored
Merge pull request #78 from rosuH/dev
Update ImageEngine
2 parents 645f5a5 + ca9a35f commit b747544

11 files changed

Lines changed: 137 additions & 114 deletions

File tree

filepicker/src/main/java/me/rosuh/filepicker/adapter/FileListAdapter.kt

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.rosuh.filepicker.adapter
22

3-
import android.net.Uri
43
import android.support.v7.widget.RecyclerView
54
import android.view.LayoutInflater
65
import android.view.View
@@ -246,19 +245,11 @@ class FileListAdapter(
246245

247246
val resId: Int = itemImpl.fileType?.fileIconResId ?: R.drawable.ic_unknown_file_picker
248247
when (itemImpl.fileType) {
249-
is RasterImageFileType -> {
248+
is RasterImageFileType, is VideoFileType -> {
250249
ImageLoadController.load(
251250
context,
252251
mIcon,
253-
Uri.fromFile(File(itemImpl.filePath)),
254-
resId
255-
)
256-
}
257-
is VideoFileType -> {
258-
ImageLoadController.load(
259-
context,
260-
mIcon,
261-
Uri.fromFile(File(itemImpl.filePath)),
252+
itemImpl.filePath,
262253
resId
263254
)
264255
}
@@ -300,19 +291,11 @@ class FileListAdapter(
300291

301292
val resId: Int = itemImpl.fileType?.fileIconResId ?: R.drawable.ic_unknown_file_picker
302293
when (itemImpl.fileType) {
303-
is RasterImageFileType -> {
304-
ImageLoadController.load(
305-
context,
306-
mIcon,
307-
Uri.fromFile(File(itemImpl.filePath)),
308-
resId
309-
)
310-
}
311-
is VideoFileType -> {
294+
is RasterImageFileType, is VideoFileType -> {
312295
ImageLoadController.load(
313296
context,
314297
mIcon,
315-
Uri.fromFile(File(itemImpl.filePath)),
298+
itemImpl.filePath,
316299
resId
317300
)
318301
}

filepicker/src/main/java/me/rosuh/filepicker/config/FilePickerConfig.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.support.annotation.NonNull
55
import android.support.annotation.StringRes
66
import me.rosuh.filepicker.FilePickerActivity
77
import me.rosuh.filepicker.R
8+
import me.rosuh.filepicker.engine.ImageEngine
89

910
/**
1011
*
@@ -112,6 +113,12 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
112113
var emptyListTips: String = contextRes.getString(R.string.empty_list_tips_file_picker)
113114
private set
114115

116+
/**
117+
* 如果您的 Glide 版本低于 4.9, 请使用自定义的 [ImageEngine]
118+
*/
119+
var customImageEngine: ImageEngine? = null
120+
private set
121+
115122
fun showHiddenFiles(isShow: Boolean): FilePickerConfig {
116123
isShowHiddenFiles = isShow
117124
return this
@@ -216,6 +223,11 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
216223
return this
217224
}
218225

226+
fun imageEngine(ie: ImageEngine): FilePickerConfig {
227+
this.customImageEngine = ie
228+
return this
229+
}
230+
219231
fun forResult(requestCode: Int) {
220232
val activity = pickerManager.contextRef?.get()!!
221233
val fragment = pickerManager.fragmentRef?.get()

filepicker/src/main/java/me/rosuh/filepicker/config/FilePickerManager.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package me.rosuh.filepicker.config
22

33
import android.app.Activity
44
import android.support.v4.app.Fragment
5+
import me.rosuh.filepicker.engine.ImageLoadController
56
import java.lang.ref.WeakReference
67

78
/**
@@ -26,11 +27,6 @@ object FilePickerManager {
2627
return config
2728
}
2829

29-
private fun reset() {
30-
contextRef?.clear()
31-
fragmentRef?.clear()
32-
}
33-
3430
/**
3531
* 不能使用 fragmentRef.getContext(),因为无法保证外部的代码环境
3632
*/
@@ -58,4 +54,10 @@ object FilePickerManager {
5854
fun obtainData(): List<String> {
5955
return dataList
6056
}
57+
58+
private fun reset() {
59+
contextRef?.clear()
60+
fragmentRef?.clear()
61+
ImageLoadController.reset()
62+
}
6163
}

filepicker/src/main/java/me/rosuh/filepicker/engine/GlideEngine.kt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,63 @@
11
package me.rosuh.filepicker.engine
22

33
import android.content.Context
4-
import android.graphics.drawable.Drawable
5-
import android.net.Uri
4+
import android.graphics.Bitmap
5+
import android.media.ThumbnailUtils
66
import android.widget.ImageView
77
import com.bumptech.glide.Glide
88
import com.bumptech.glide.load.DataSource
99
import com.bumptech.glide.load.engine.GlideException
1010
import com.bumptech.glide.request.RequestListener
1111
import com.bumptech.glide.request.target.Target
12+
import me.rosuh.filepicker.R
13+
import me.rosuh.filepicker.utils.ScreenUtils
1214

1315
/**
1416
* @author rosu
1517
* @date 2020-04-15
1618
* An [ImageEngine] implementation by using Glide
1719
*/
1820
class GlideEngine : ImageEngine {
19-
override fun loadImage(context: Context?, imageView: ImageView?, uri: Uri?, placeholder: Int) {
21+
override fun loadImage(
22+
context: Context?,
23+
imageView: ImageView?,
24+
url: String?,
25+
placeholder: Int
26+
) {
2027
if (context == null || imageView == null) {
2128
return
2229
}
2330
Glide.with(context)
24-
.load(uri)
25-
.addListener(object : RequestListener<Drawable> {
31+
.asBitmap()
32+
.load(url)
33+
.addListener(object : RequestListener<Bitmap?> {
2634
override fun onLoadFailed(
2735
e: GlideException?,
2836
model: Any?,
29-
target: Target<Drawable>?,
37+
target: Target<Bitmap?>?,
3038
isFirstResource: Boolean
3139
): Boolean {
32-
imageView.setImageResource(placeholder)
40+
imageView.setImageResource(R.drawable.ic_unknown_file_picker)
3341
return true
3442
}
3543

3644
override fun onResourceReady(
37-
resource: Drawable?,
45+
resource: Bitmap?,
3846
model: Any?,
39-
target: Target<Drawable>?,
47+
target: Target<Bitmap?>?,
4048
dataSource: DataSource?,
4149
isFirstResource: Boolean
4250
): Boolean {
43-
return false
51+
// Create thumbnail for better effect.
52+
val thumbnailBitmap =
53+
ThumbnailUtils.extractThumbnail(
54+
resource,
55+
imageView.width.coerceAtLeast(ScreenUtils.dipToPx(context, 40f)),
56+
imageView.height.coerceAtLeast(ScreenUtils.dipToPx(context, 40f))
57+
)
58+
imageView.setImageBitmap(thumbnailBitmap)
59+
return true
4460
}
45-
4661
})
4762
.into(imageView)
4863
}

filepicker/src/main/java/me/rosuh/filepicker/engine/ImageEngine.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import android.widget.ImageView
1212
*/
1313
interface ImageEngine {
1414
/**
15-
* 调用此接口加载图片,一般情况下[uri]参数表示图片的本地路径 path,通过[Uri.parse]得到的值。通常是 file:/// 开头
15+
* 调用此接口加载图片,一般情况下[url]参数表示图片的本地路径 path,通过[Uri.parse]得到的值。通常是 file:/// 开头
1616
* 如果加载失败,将使用[placeholder]
17-
* Call this interface to load the picture. Generally, the [uri] parameter indicates the local
17+
* Call this interface to load the picture. Generally, the [url] parameter indicates the local
1818
* path of the picture, and the value obtained through [Uri.parse].
1919
* Usually starts with file:///
2020
* If loading fails, [placeholder] will be used
2121
*/
2222
fun loadImage(
2323
context: Context?,
2424
imageView: ImageView?,
25-
uri: Uri?,
25+
url: String?,
2626
placeholder: Int
2727
)
2828
}

filepicker/src/main/java/me/rosuh/filepicker/engine/ImageLoadController.kt

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package me.rosuh.filepicker.engine
22

33
import android.content.Context
4-
import android.net.Uri
4+
import android.util.Log
55
import android.widget.ImageView
66
import me.rosuh.filepicker.R
7+
import me.rosuh.filepicker.config.FilePickerManager
78

89
/**
910
* @author rosu
1011
* @date 2020-04-15
1112
* 一个全局的图片加载控制类,包含了判断是否存在以及存在哪种图片加载引擎。
12-
* A global image loading control class, including determining whether there is and what kind of
13+
* A global image loading controller, including determining whether there is and what kind of
1314
* image loading engine exists.
1415
*/
1516
object ImageLoadController {
@@ -37,8 +38,43 @@ object ImageLoadController {
3738

3839
private var engine: ImageEngine? = null
3940

40-
init {
41+
/**
42+
* 加载图片,如果没有不存在图片加载引擎,那么将使用默认 icon
43+
* Load images, if there is no image loading engine, then the default icon is used
44+
*/
45+
fun load(
46+
context: Context,
47+
iv: ImageView,
48+
url: String,
49+
placeholder: Int? = R.drawable.ic_unknown_file_picker
50+
) {
51+
if (engine == null && !initEngine()) {
52+
iv.setImageResource(placeholder ?: R.drawable.ic_unknown_file_picker)
53+
return
54+
}
55+
try {
56+
engine?.loadImage(context, iv, url, placeholder ?: R.drawable.ic_unknown_file_picker)
57+
} catch (e: NoSuchMethodError) {
58+
Log.d(
59+
"ImageLoadController", """
60+
AndroidFilePicker throw NoSuchMethodError which means current Glide version was not supported.
61+
We recommend using 4.9+ or you should implements your own ImageEngine.
62+
Ref:https://github.com/rosuH/AndroidFilePicker/issues/76
63+
""".trimIndent()
64+
)
65+
iv.setImageResource(placeholder ?: R.drawable.ic_unknown_file_picker)
66+
}
67+
}
68+
69+
/**
70+
* 每次配置更新的时候,都需要重新初始化图片加载器
71+
* Every time the configuration is updated, we need to re-initialize the image loader
72+
*/
73+
private fun initEngine(): Boolean {
4174
engine = when {
75+
FilePickerManager.config.customImageEngine != null -> {
76+
FilePickerManager.config.customImageEngine
77+
}
4278
enableGlide -> {
4379
GlideEngine()
4480
}
@@ -49,22 +85,10 @@ object ImageLoadController {
4985
null
5086
}
5187
}
88+
return engine != null
5289
}
5390

54-
/**
55-
* 加载图片,如果没有不存在图片加载引擎,那么家使用默认 icon
56-
* Load images, if there is no image loading engine, then the default icon is used
57-
*/
58-
fun load(
59-
context: Context,
60-
iv: ImageView,
61-
uri: Uri,
62-
placeholder: Int? = R.drawable.ic_unknown_file_picker
63-
) {
64-
if (engine == null) {
65-
iv.setImageResource(placeholder ?: R.drawable.ic_unknown_file_picker)
66-
return
67-
}
68-
engine?.loadImage(context, iv, uri, placeholder ?: R.drawable.ic_unknown_file_picker)
91+
fun reset() {
92+
engine = null
6993
}
7094
}
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package me.rosuh.filepicker.engine
22

33
import android.content.Context
4-
import android.net.Uri
54
import android.widget.ImageView
65
import com.squareup.picasso.Picasso
6+
import java.io.File
77

88
/**
99
* @author rosu
1010
* @date 2020-04-15
1111
* An [ImageEngine] implementation by using Picasso
1212
*/
1313
class PicassoEngine : ImageEngine {
14-
override fun loadImage(context: Context?, imageView: ImageView?, uri: Uri?, placeholder: Int) {
15-
Picasso.with(context)
16-
.load(uri)
17-
.fit()
18-
.centerCrop()
19-
.placeholder(placeholder)
20-
.into(imageView)
14+
override fun loadImage(
15+
context: Context?,
16+
imageView: ImageView?,
17+
url: String?,
18+
placeholder: Int
19+
) {
20+
if (url?.startsWith("http") == true) {
21+
Picasso.with(context)
22+
.load(url)
23+
.placeholder(placeholder)
24+
.into(imageView)
25+
} else {
26+
Picasso.with(context)
27+
.load(File(url))
28+
.placeholder(placeholder)
29+
.into(imageView)
30+
}
2131
}
2232
}

0 commit comments

Comments
 (0)