| | |
| | | package com.dayu.pipirrapp.utils; |
| | | |
| | | import android.content.Context; |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.BitmapFactory; |
| | | import android.net.Uri; |
| | | import android.util.Log; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | |
| | | import java.io.OutputStream; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import okhttp3.ResponseBody; |
| | | |
| | |
| | | * 备注: 完成地图缓存策略 |
| | | */ |
| | | public class MapJpgUtils { |
| | | static MapJpgUtils mapJpgUtils; |
| | | Context context; |
| | | static String TAG = "MapJpgUtils"; |
| | | |
| | | public static MapJpgUtils getInsatance(Context context) { |
| | | static MapJpgUtils mapJpgUtils; |
| | | |
| | | String childName = "mapCache"; |
| | | String fileType = ".jpg"; |
| | | |
| | | public static MapJpgUtils getInsatance() { |
| | | if (mapJpgUtils == null) { |
| | | mapJpgUtils = new MapJpgUtils(); |
| | | } |
| | | mapJpgUtils.context = context; |
| | | return mapJpgUtils; |
| | | } |
| | | |
| | |
| | | // 获取缓存瓦片文件 |
| | | public File getCachedTile(String url) { |
| | | String fileName = hashUrl(url); // URL 转换为唯一的哈希值或文件名 |
| | | File cacheDir = new File(context.getCacheDir(), "mapCache"); // 瓦片存储目录 |
| | | String folderPath = extractTileInfoFromUrl(url); |
| | | File cacheDir = new File(MyApplication.myApplication.getCacheDir() + File.separator + childName + File.separator + folderPath); // 瓦片存储目录 |
| | | if (!cacheDir.exists()) { |
| | | cacheDir.mkdirs(); |
| | | } |
| | |
| | | for (byte b : hashBytes) { |
| | | sb.append(String.format("%02x", b)); |
| | | } |
| | | return sb.toString(); |
| | | return sb.toString() + fileType; |
| | | } catch (NoSuchAlgorithmException e) { |
| | | e.printStackTrace(); |
| | | return String.valueOf(url.hashCode()); |
| | |
| | | public boolean saveTileToCache(String url, ResponseBody body) { |
| | | |
| | | try { |
| | | File tileFile = new File(context.getCacheDir() + File.separator + hashUrl(url) + ".png"); |
| | | //从 URL 中提取瓦片的 Z/X/Y 值 |
| | | String folderPath = extractTileInfoFromUrl(url); |
| | | |
| | | File tileFile = new File(MyApplication.myApplication.getCacheDir() + File.separator + childName + File.separator + folderPath + File.separator + hashUrl(url)); |
| | | if (!tileFile.getParentFile().exists()) { |
| | | tileFile.getParentFile().mkdirs(); |
| | | } |
| | | |
| | | InputStream inputStream = null; |
| | | OutputStream outputStream = null; |
| | | |
| | |
| | | outputStream.flush(); |
| | | // 保存完成后,记录缓存创建时间 |
| | | tileFile.setLastModified(System.currentTimeMillis()); |
| | | return true; |
| | | if (validateImageFile(tileFile)) { |
| | | // 校验下载文件大小和实际大小是否一致 |
| | | long actualFileSize = tileFile.length(); |
| | | if (fileSizeDownloaded == actualFileSize) { |
| | | Log.d(TAG, "文件大小匹配,下载大小:" + fileSizeDownloaded + " 字节,实际大小:" + actualFileSize + " 字节"); |
| | | return true; |
| | | } else { |
| | | //保存失败,删除文件 |
| | | tileFile.delete(); |
| | | Log.e(TAG, "文件大小不匹配,下载大小:" + fileSizeDownloaded + " 字节,实际大小:" + actualFileSize + " 字节"); |
| | | return false; |
| | | } |
| | | } else { |
| | | //保存失败,删除文件 |
| | | tileFile.delete(); |
| | | Log.d(TAG, "保存失败,文件没有保存完整"); |
| | | return false; |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | return false; |
| | | } finally { |
| | |
| | | */ |
| | | public boolean isHasFiles(String url) { |
| | | String fileName = hashUrl(url); // URL 转换为唯一的哈希值或文件名 |
| | | File cacheDir = new File(context.getCacheDir(), "mapCache"); // 瓦片存储目录 |
| | | File cacheDir = new File(MyApplication.myApplication.getCacheDir(), "mapCache"); // 瓦片存储目录 |
| | | if (!cacheDir.exists()) { |
| | | cacheDir.mkdirs(); |
| | | } |
| | |
| | | return (System.currentTimeMillis() - lastModified) > cacheValidity; |
| | | } |
| | | |
| | | // 清理旧缓存 |
| | | private void clearOldCacheFiles() { |
| | | File cacheDir = new File(context.getCacheDir(), "mapCache"); |
| | | /** |
| | | * 判断当前文件是否完整 |
| | | * |
| | | * @param url |
| | | * @return |
| | | */ |
| | | public boolean isComplete(String url, long size) { |
| | | return false; |
| | | } |
| | | |
| | | // 清理所有旧缓存 |
| | | public void clearOldCacheFiles() { |
| | | File cacheDir = new File(MyApplication.myApplication.getCacheDir(), "mapCache"); |
| | | if (cacheDir.exists()) { |
| | | File[] files = cacheDir.listFiles(); |
| | | if (files != null) { |
| | | for (File file : files) { |
| | | // 清理超过 7 天的缓存文件 |
| | | if (System.currentTimeMillis() - file.lastModified() > 7 * 24 * 60 * 60 * 1000) { |
| | | file.delete(); |
| | | } |
| | | file.delete(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //清理单个缓存 |
| | | public void clearCacheFile(String url) { |
| | | File file = new File(MyApplication.myApplication.getCacheDir() + File.separator + childName + File.separator + hashUrl(url) + fileType); |
| | | if (file.exists()) { |
| | | file.delete(); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 校验图片的完整性 |
| | | public boolean validateImageFile(File tileFile) { |
| | | if (!tileFile.exists()) { |
| | | // 文件不存在,视为不完整 |
| | | return false; |
| | | } |
| | | // 尝试解析图像 |
| | | Bitmap bitmap = BitmapFactory.decodeFile(tileFile.getAbsolutePath()); |
| | | if (bitmap != null) { |
| | | // 图像解析成功,文件完整 |
| | | Log.d(TAG, "图像解析成功,文件完整"); |
| | | // 解析完图像后,手动回收Bitmap的内存 |
| | | bitmap.recycle(); |
| | | return true; |
| | | } else { |
| | | // 图像解析失败,文件不完整或损坏 |
| | | Log.e(TAG, "图像解析失败,文件不完整或损坏"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 判断请求是否为天地图瓦片 |
| | | * |
| | | * @param url |
| | | * @return |
| | | */ |
| | | public boolean isTianDiTuTileRequest(String url) { |
| | | return url.contains("tianditu.gov.cn") && url.contains("wmts") && url.contains("TILEMATRIX"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 从 URL 中提取瓦片的 Z/X/Y 值 |
| | | * |
| | | * @param url |
| | | * @return |
| | | */ |
| | | public String extractTileInfoFromUrl(String url) { |
| | | Uri uri = Uri.parse(url); |
| | | Map<String, String> tileInfo = new HashMap<>(); |
| | | String path = uri.getQueryParameter("TILEMATRIX") + File.separator + uri.getQueryParameter("TILEROW") + File.separator + uri.getQueryParameter("TILECOL"); |
| | | return path; |
| | | } |
| | | |
| | | } |