Skip to content

Instantly share code, notes, and snippets.

@vihuela
Created August 11, 2017 08:58
Show Gist options
  • Save vihuela/ee973db7b43e085a1c197a4621847b65 to your computer and use it in GitHub Desktop.
Save vihuela/ee973db7b43e085a1c197a4621847b65 to your computer and use it in GitHub Desktop.
fun getTargetFile(context: Context, uri: Uri, getFile: (f: File?) -> Unit) {
val targetPath = when (uri.authority) {
//拍照
AUTHORITY -> "${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)}/${uri.path.substring(uri.path.lastIndexOf("/") + 1, uri.path.length)}"
//图库选择
"media" -> PhotoMetadataUtils.getPath(context.contentResolver, uri)
else -> {
getFile.invoke(null)
null
}
}
//异步压缩
Luban.with(context)
.load(File(targetPath ?: return))
.setCompressListener(object : OnCompressListener {
override fun onSuccess(f: File?) {
getFile.invoke(f)
}
override fun onError(e: Throwable?) {
getFile.invoke(null)
}
override fun onStart() {
}
}).launch()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment