RequestListener<Drawable> 可以直接区别图片类型但保存文件比较繁琐
RequestListener<File> 可以直接保存文件但无法区别图片类型
以下代码已经实践 有更优的方式请留言
Kotlin代码完成:
private val PATH_CAMERA_IMAGE = "/hanzhi/myImage"
public fun loadGlideImageByImageUrl(context: Context, url: String) {
var isGif: Boolean
GlideApp.with(context)
// Actually it won't download another time if the file has been cached
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
isGif = resource is GifDrawable
downloadImage(context, url, isGif)
return false
}
}).submit()
}
private fun downloadImage(context: Context, url: String, isGif: Boolean) {
GlideApp.with(context)
.download(url)
.listener(object : RequestListener<File> {
override fun onLoadFailed(e: GlideException?, model: Any, target: Target<File>, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: File, model: Any, target: Target<File>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
//Use this uri for the Commit Content API
//获取到下载得到的图片,进行本地保存
val pictureFolder = Environment.getExternalStorageDirectory()
val appDir = File(pictureFolder, PATH_CAMERA_IMAGE)
if (!appDir.exists()) {
appDir.mkdirs()
}
var childName = ""
if (isGif) {
childName = "${System.currentTimeMillis()}.gif"
} else {
childName = "${System.currentTimeMillis()}.jpg"
}
val destFile = File(appDir, childName)
//复制文件
copyFile(resource, destFile)
if (!TextUtils.isEmpty(resource.toString())) {
showToast(context, "保存图片成功")
Util.broadcase(context, destFile)
} else {
showToast(context, "保存图片失败")
}
return false
}
}
)
.submit()
}
//刷新相册
public static void broadcase(Context context, File file) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri; //out is your output file
contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
} else {
context.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param oldFile 输入文件
* @param newFile 输出文件
*/
public static void copyFile(File oldFile, File newFile) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
while (fileInputStream.read(buffer) > 0) {
fileOutputStream.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Android Glide4.0以上版本保存Gif文件方法
最新推荐文章于 2021-09-23 09:41:41 发布