[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复特殊Url导致重复引用相同图片问题 #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean onResourceReady(final File resource, Object model, Target<File> t

@Override
public File getCache(String url) {
File cacheFile = new File(getCacheDir(), getFileName(url));
File cacheFile = new File(getCacheDir(), getCacheFileName(url));
return cacheFile.exists() ? cacheFile : null;
}

Expand All @@ -84,8 +84,14 @@ public File getCacheDir() {
return cacheDir;
}

private String getFileName(String imageUrl) {
String[] nameArray = imageUrl.split("/");
@Override
public String getCacheFileName(String url) {
int paramsStartIndex = url.indexOf("?");
if (paramsStartIndex!=-1){
url = url.substring(0,paramsStartIndex);
}
String[] nameArray = url.split("/");
return nameArray[nameArray.length - 1];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ public void run() {

@Override
public File getCache(String url) {
// 通过分析 OkHttp3Downloader 源码可知缓存文件名是图片 url 经过 md5 加密后的小写字符串,并拼接 ".1"
// .0 表示缓存文件的网络请求 header 描述; .1 是缓存文件本身
String key = EncryptUtils.encryptMD5ToString(url).toLowerCase() + ".1";
File cacheFile = new File(getCacheDir(), key);
File cacheFile = new File(getCacheDir(), getCacheFileName(url));
return cacheFile.exists() ? cacheFile : null;
}

Expand All @@ -179,4 +176,11 @@ public File getCacheDir() {
if (!cacheDir.exists()) cacheDir.mkdirs();
return cacheDir;
}

@Override
public String getCacheFileName(String url) {
// 通过分析 OkHttp3Downloader 源码可知缓存文件名是图片 url 经过 md5 加密后的小写字符串,并拼接 ".1"
// .0 表示缓存文件的网络请求 header 描述; .1 是缓存文件本身
return EncryptUtils.encryptMD5ToString(url).toLowerCase() + ".1";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface ImageLoader {

File getCacheDir();


String getCacheFileName(String url);

/**
* 清除 ImageLoader 缓存
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ private void init() {
}

public void process(Context context, final String key, final File sourceFile, final File savedDir,
final Point displaySize, @NonNull ImageProcessCallback callback) {
final File targetFile = new File(savedDir, getFileName(key));
final Point displaySize,@NonNull String cacheFileName, @NonNull ImageProcessCallback callback) {
final File targetFile = new File(savedDir, cacheFileName);
if (!isNeedProcess(sourceFile, displaySize.x, displaySize.y)
|| ImageUtils.getImageType(sourceFile) == TYPE_GIF) {
// 不需要处理的图片和 gif 图,直接复制到指定文件夹
Expand All @@ -73,7 +73,7 @@ public void run() {
callback.onSuccess(targetFile);
} else {
callbackMap.put(key, callback);
execService.execute(new ImageTask(context, key, sourceFile, savedDir));
execService.execute(new ImageTask(context, key, sourceFile, savedDir,cacheFileName));
}
}

Expand All @@ -92,11 +92,6 @@ private boolean isNeedProcess(final File source, final int maxWidth, final int m
return needAdjustOrientation || w > maxWidth || h > maxHeight;
}

private String getFileName(String imageUrl) {
String[] nameArray = imageUrl.split("/");
return nameArray[nameArray.length - 1];
}

private int getImageOrientation(String sourcePath) {
try {
ExifInterface exifInfo = new ExifInterface(sourcePath);
Expand Down Expand Up @@ -172,15 +167,17 @@ public void handleMessage(Message msg) {
private class ImageTask implements Runnable {
private Context context;
private String key;
private String cacheFileName;
private File originFile;
private File savedDir;


public ImageTask(Context context, String key, File originFile, File savedDir) {
public ImageTask(Context context, String key, File originFile, File savedDir, String cacheFileName) {
this.context = context;
this.key = key;
this.originFile = originFile;
this.savedDir = savedDir;
this.cacheFileName = cacheFileName;
}

@Override
Expand All @@ -192,7 +189,7 @@ public void run() {
.ignoreBy(150)
.setTargetDir(savedDir.getAbsolutePath())
.get();
File targetFile = new File(savedDir, getFileName(key));
File targetFile = new File(savedDir, cacheFileName);
File compressFile;
if (files != null && !files.isEmpty()) {
compressFile = files.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ TransferImage createTransferImage(ImageView originImage, boolean bindListener) {
void startPreview(final TransferImage targetImage, final File source,
final String imgUrl, final StartPreviewCallback callback) {
targetImage.enableGesture();// 启用 TransferImage 的手势缩放功能
final File cacheDir = transfer.getTransConfig().getImageLoader().getCacheDir();
ImageLoader loader = transfer.getTransConfig().getImageLoader();
final File cacheDir = loader.getCacheDir();
String cacheFileName = loader.getCacheFileName(imgUrl);
ImageProcessor.getInstance().process(transfer.getContext(), imgUrl, source,
cacheDir, getDisplaySize(), new ImageProcessor.ImageProcessCallback() {
cacheDir, getDisplaySize(), cacheFileName,new ImageProcessor.ImageProcessCallback() {
@Override
public void onSuccess(File file) {
if (ImageUtils.getImageType(file) == TYPE_GIF) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ public File getCache(String url) {
public File getCacheDir() {
return ImageLoader.getInstance().getDiskCache().getDirectory();
}

@Override
public String getCacheFileName(String url) {
String[] nameArray = url.split("/");
return nameArray[nameArray.length - 1];
}
}