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();        }    }}

更多相关文章

  1. 搭建 Win7 Android(安卓)NDK 开发环境
  2. Android(安卓)5.x 权限问题解决方法
  3. android——点击按钮时更改按钮样式
  4. Android(安卓)解决BitmapFactory.decodeFile(file) 报OOM问题
  5. Android(安卓)反编译工具的各种用法
  6. MediaRecorder流程分析 java层到stagefright层
  7. git 在android studio中重新关联远程仓库地址的方法
  8. Android(安卓)Source Code
  9. 专题一====Android五种数据存储方式

随机推荐

  1. android 获取网络IP地址
  2. android AsnyTask的使用
  3. Android market:// 链接到Google Play 商
  4. Android查询SIM卡所有信息
  5. android实现 服务器功能
  6. Android获取网络视频文件缩略图
  7. Android 3.0 http网络请求
  8. android 状态栏与标题栏一体化
  9. android MAT使用
  10. android的PowerManager和PowerManager.Wa