Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.rosuh.filepicker.adapter

import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -246,19 +245,11 @@ class FileListAdapter(

val resId: Int = itemImpl.fileType?.fileIconResId ?: R.drawable.ic_unknown_file_picker
when (itemImpl.fileType) {
is RasterImageFileType -> {
is RasterImageFileType, is VideoFileType -> {
ImageLoadController.load(
context,
mIcon,
Uri.fromFile(File(itemImpl.filePath)),
resId
)
}
is VideoFileType -> {
ImageLoadController.load(
context,
mIcon,
Uri.fromFile(File(itemImpl.filePath)),
itemImpl.filePath,
resId
)
}
Expand Down Expand Up @@ -300,19 +291,11 @@ class FileListAdapter(

val resId: Int = itemImpl.fileType?.fileIconResId ?: R.drawable.ic_unknown_file_picker
when (itemImpl.fileType) {
is RasterImageFileType -> {
ImageLoadController.load(
context,
mIcon,
Uri.fromFile(File(itemImpl.filePath)),
resId
)
}
is VideoFileType -> {
is RasterImageFileType, is VideoFileType -> {
ImageLoadController.load(
context,
mIcon,
Uri.fromFile(File(itemImpl.filePath)),
itemImpl.filePath,
resId
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.support.annotation.NonNull
import android.support.annotation.StringRes
import me.rosuh.filepicker.FilePickerActivity
import me.rosuh.filepicker.R
import me.rosuh.filepicker.engine.ImageEngine

/**
*
Expand Down Expand Up @@ -112,6 +113,12 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
var emptyListTips: String = contextRes.getString(R.string.empty_list_tips_file_picker)
private set

/**
* 如果您的 Glide 版本低于 4.9, 请使用自定义的 [ImageEngine]
*/
var customImageEngine: ImageEngine? = null
private set

fun showHiddenFiles(isShow: Boolean): FilePickerConfig {
isShowHiddenFiles = isShow
return this
Expand Down Expand Up @@ -216,6 +223,11 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
return this
}

fun imageEngine(ie: ImageEngine): FilePickerConfig {
this.customImageEngine = ie
return this
}

fun forResult(requestCode: Int) {
val activity = pickerManager.contextRef?.get()!!
val fragment = pickerManager.fragmentRef?.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.rosuh.filepicker.config

import android.app.Activity
import android.support.v4.app.Fragment
import me.rosuh.filepicker.engine.ImageLoadController
import java.lang.ref.WeakReference

/**
Expand All @@ -26,11 +27,6 @@ object FilePickerManager {
return config
}

private fun reset() {
contextRef?.clear()
fragmentRef?.clear()
}

/**
* 不能使用 fragmentRef.getContext(),因为无法保证外部的代码环境
*/
Expand Down Expand Up @@ -58,4 +54,10 @@ object FilePickerManager {
fun obtainData(): List<String> {
return dataList
}

private fun reset() {
contextRef?.clear()
fragmentRef?.clear()
ImageLoadController.reset()
}
}
37 changes: 26 additions & 11 deletions filepicker/src/main/java/me/rosuh/filepicker/engine/GlideEngine.kt
Original file line number Diff line number Diff line change
@@ -1,48 +1,63 @@
package me.rosuh.filepicker.engine

import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.graphics.Bitmap
import android.media.ThumbnailUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import me.rosuh.filepicker.R
import me.rosuh.filepicker.utils.ScreenUtils

/**
* @author rosu
* @date 2020-04-15
* An [ImageEngine] implementation by using Glide
*/
class GlideEngine : ImageEngine {
override fun loadImage(context: Context?, imageView: ImageView?, uri: Uri?, placeholder: Int) {
override fun loadImage(
context: Context?,
imageView: ImageView?,
url: String?,
placeholder: Int
) {
if (context == null || imageView == null) {
return
}
Glide.with(context)
.load(uri)
.addListener(object : RequestListener<Drawable> {
.asBitmap()
.load(url)
.addListener(object : RequestListener<Bitmap?> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
target: Target<Bitmap?>?,
isFirstResource: Boolean
): Boolean {
imageView.setImageResource(placeholder)
imageView.setImageResource(R.drawable.ic_unknown_file_picker)
return true
}

override fun onResourceReady(
resource: Drawable?,
resource: Bitmap?,
model: Any?,
target: Target<Drawable>?,
target: Target<Bitmap?>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
return false
// Create thumbnail for better effect.
val thumbnailBitmap =
ThumbnailUtils.extractThumbnail(
resource,
imageView.width.coerceAtLeast(ScreenUtils.dipToPx(context, 40f)),
imageView.height.coerceAtLeast(ScreenUtils.dipToPx(context, 40f))
)
imageView.setImageBitmap(thumbnailBitmap)
return true
}

})
.into(imageView)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import android.widget.ImageView
*/
interface ImageEngine {
/**
* 调用此接口加载图片,一般情况下[uri]参数表示图片的本地路径 path,通过[Uri.parse]得到的值。通常是 file:/// 开头
* 调用此接口加载图片,一般情况下[url]参数表示图片的本地路径 path,通过[Uri.parse]得到的值。通常是 file:/// 开头
* 如果加载失败,将使用[placeholder]
* Call this interface to load the picture. Generally, the [uri] parameter indicates the local
* Call this interface to load the picture. Generally, the [url] parameter indicates the local
* path of the picture, and the value obtained through [Uri.parse].
* Usually starts with file:///
* If loading fails, [placeholder] will be used
*/
fun loadImage(
context: Context?,
imageView: ImageView?,
uri: Uri?,
url: String?,
placeholder: Int
)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package me.rosuh.filepicker.engine

import android.content.Context
import android.net.Uri
import android.util.Log
import android.widget.ImageView
import me.rosuh.filepicker.R
import me.rosuh.filepicker.config.FilePickerManager

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

private var engine: ImageEngine? = null

init {
/**
* 加载图片,如果没有不存在图片加载引擎,那么将使用默认 icon
* Load images, if there is no image loading engine, then the default icon is used
*/
fun load(
context: Context,
iv: ImageView,
url: String,
placeholder: Int? = R.drawable.ic_unknown_file_picker
) {
if (engine == null && !initEngine()) {
iv.setImageResource(placeholder ?: R.drawable.ic_unknown_file_picker)
return
}
try {
engine?.loadImage(context, iv, url, placeholder ?: R.drawable.ic_unknown_file_picker)
} catch (e: NoSuchMethodError) {
Log.d(
"ImageLoadController", """
AndroidFilePicker throw NoSuchMethodError which means current Glide version was not supported.
We recommend using 4.9+ or you should implements your own ImageEngine.
Ref:https://github.com/rosuH/AndroidFilePicker/issues/76
""".trimIndent()
)
iv.setImageResource(placeholder ?: R.drawable.ic_unknown_file_picker)
}
}

/**
* 每次配置更新的时候,都需要重新初始化图片加载器
* Every time the configuration is updated, we need to re-initialize the image loader
*/
private fun initEngine(): Boolean {
engine = when {
FilePickerManager.config.customImageEngine != null -> {
FilePickerManager.config.customImageEngine
}
enableGlide -> {
GlideEngine()
}
Expand All @@ -49,22 +85,10 @@ object ImageLoadController {
null
}
}
return engine != null
}

/**
* 加载图片,如果没有不存在图片加载引擎,那么家使用默认 icon
* Load images, if there is no image loading engine, then the default icon is used
*/
fun load(
context: Context,
iv: ImageView,
uri: Uri,
placeholder: Int? = R.drawable.ic_unknown_file_picker
) {
if (engine == null) {
iv.setImageResource(placeholder ?: R.drawable.ic_unknown_file_picker)
return
}
engine?.loadImage(context, iv, uri, placeholder ?: R.drawable.ic_unknown_file_picker)
fun reset() {
engine = null
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package me.rosuh.filepicker.engine

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.squareup.picasso.Picasso
import java.io.File

/**
* @author rosu
* @date 2020-04-15
* An [ImageEngine] implementation by using Picasso
*/
class PicassoEngine : ImageEngine {
override fun loadImage(context: Context?, imageView: ImageView?, uri: Uri?, placeholder: Int) {
Picasso.with(context)
.load(uri)
.fit()
.centerCrop()
.placeholder(placeholder)
.into(imageView)
override fun loadImage(
context: Context?,
imageView: ImageView?,
url: String?,
placeholder: Int
) {
if (url?.startsWith("http") == true) {
Picasso.with(context)
.load(url)
.placeholder(placeholder)
.into(imageView)
} else {
Picasso.with(context)
.load(File(url))
.placeholder(placeholder)
.into(imageView)
}
}
}
Loading