Android Glide4.0以上のバージョンでGifファイルを保存する方法


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