Android(安卓)Glide4.0以上版本保存Gif文件方法
16lz
2021-01-26
RequestListener 可以直接区别图片类型但保存文件比较繁琐RequestListener 可以直接保存文件但无法区别图片类型以下代码已经实践 有更优的方式请留言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 { override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { return false } override fun onResourceReady(resource: Drawable?, model: Any?, target: Target?, 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 { override fun onLoadFailed(e: GlideException?, model: Any, target: Target, isFirstResource: Boolean): Boolean { return false } override fun onResourceReady(resource: File, model: Any, target: Target, 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(); } }}
更多相关文章
- 搭建 Win7 Android(安卓)NDK 开发环境
- Android(安卓)5.x 权限问题解决方法
- android——点击按钮时更改按钮样式
- Android(安卓)解决BitmapFactory.decodeFile(file) 报OOM问题
- Android(安卓)反编译工具的各种用法
- MediaRecorder流程分析 java层到stagefright层
- git 在android studio中重新关联远程仓库地址的方法
- Android(安卓)Source Code
- 专题一====Android五种数据存储方式