Skip to content

Commit

Permalink
fix: 防止图片加载某些情况下无限制的FileLockedException重试
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Apr 7, 2020
1 parent f1b8604 commit 5296bc5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
42 changes: 24 additions & 18 deletions xutils/src/main/java/org/xutils/image/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
private MemCacheKey key;
private ImageOptions options;
private WeakReference<ImageView> viewRef;
private int fileLockedExceptionRetryCount = 0;

private final static AtomicLong SEQ_SEEK = new AtomicLong(0);
private final long seq = SEQ_SEEK.incrementAndGet();
Expand Down Expand Up @@ -137,7 +138,7 @@ static Cancelable doLoadDrawable(final String url,
}

FakeImageView fakeImageView = new FakeImageView();
return doBind(fakeImageView, url, options, callback);
return doBind(fakeImageView, url, options, 0, callback);
}

/**
Expand All @@ -163,6 +164,7 @@ static Cancelable doLoadFile(final String url,
static Cancelable doBind(final ImageView view,
final String url,
final ImageOptions options,
final int fileLockedExceptionRetryCount,
final Callback.CommonCallback<Drawable> callback) {

// check params
Expand Down Expand Up @@ -254,16 +256,18 @@ static Cancelable doBind(final ImageView view,
// not trust the cache
// load from Network or DiskCache
ImageLoader loader = new ImageLoader();
loader.fileLockedExceptionRetryCount = fileLockedExceptionRetryCount;
loader.skipOnWaitingCallback = true;
return loader.doLoad(view, url, localOptions, callback);
return loader.doLoadRequest(view, url, localOptions, callback);
}
} catch (Throwable ex) {
LogUtil.e(ex.getMessage(), ex);
// try load from Network or DiskCache
trustMemCache = false;
ImageLoader loader = new ImageLoader();
loader.fileLockedExceptionRetryCount = fileLockedExceptionRetryCount;
loader.skipOnWaitingCallback = true;
return loader.doLoad(view, url, localOptions, callback);
return loader.doLoadRequest(view, url, localOptions, callback);
} finally {
if (trustMemCache && callback != null) {
try {
Expand All @@ -275,18 +279,20 @@ static Cancelable doBind(final ImageView view,
}
} else { /* memDrawable == null */
// load from Network or DiskCache
return new ImageLoader().doLoad(view, url, localOptions, callback);
ImageLoader loader = new ImageLoader();
loader.fileLockedExceptionRetryCount = fileLockedExceptionRetryCount;
return loader.doLoadRequest(view, url, localOptions, callback);
}
return null;
}

/**
* load from Network or DiskCache
*/
private Cancelable doLoad(ImageView view,
String url,
ImageOptions options,
Callback.CommonCallback<Drawable> callback) {
private Cancelable doLoadRequest(ImageView view,
String url,
ImageOptions options,
Callback.CommonCallback<Drawable> callback) {

this.viewRef = new WeakReference<ImageView>(view);
this.options = options;
Expand Down Expand Up @@ -429,27 +435,27 @@ public void onError(Throwable ex, boolean isOnCallback) {
stopped = true;
if (!validView4Callback(false)) return;

if (ex instanceof FileLockedException) {
fileLockedExceptionRetryCount++;
if (ex instanceof FileLockedException && fileLockedExceptionRetryCount < 5) {
LogUtil.d("ImageFileLocked: " + key.url);
x.task().postDelayed(new Runnable() {
@Override
public void run() {
ImageView imageView = viewRef.get();
if (imageView != null) {
doBind(imageView, key.url, options, callback);
doBind(imageView, key.url, options, fileLockedExceptionRetryCount, callback);
} else {
ImageLoader.this.onFinished();
}
}
}, 10);
}, 10 + (fileLockedExceptionRetryCount - 1) * 100);
skipOnFinishedCallback = true;
return;
}

LogUtil.e(key.url, ex);
setErrorDrawable4Callback();
if (callback != null) {
callback.onError(ex, isOnCallback);
} else {
LogUtil.e(key.url, ex);
setErrorDrawable4Callback();
if (callback != null) {
callback.onError(ex, isOnCallback);
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions xutils/src/main/java/org/xutils/image/ImageManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void bind(final ImageView view, final String url) {
x.task().autoPost(new Runnable() {
@Override
public void run() {
ImageLoader.doBind(view, url, null, null);
ImageLoader.doBind(view, url, null, 0, null);
}
});
}
Expand All @@ -47,7 +47,7 @@ public void bind(final ImageView view, final String url, final ImageOptions opti
x.task().autoPost(new Runnable() {
@Override
public void run() {
ImageLoader.doBind(view, url, options, null);
ImageLoader.doBind(view, url, options, 0, null);
}
});
}
Expand All @@ -57,7 +57,7 @@ public void bind(final ImageView view, final String url, final Callback.CommonCa
x.task().autoPost(new Runnable() {
@Override
public void run() {
ImageLoader.doBind(view, url, null, callback);
ImageLoader.doBind(view, url, null, 0, callback);
}
});
}
Expand All @@ -67,7 +67,7 @@ public void bind(final ImageView view, final String url, final ImageOptions opti
x.task().autoPost(new Runnable() {
@Override
public void run() {
ImageLoader.doBind(view, url, options, callback);
ImageLoader.doBind(view, url, options, 0, callback);
}
});
}
Expand Down

0 comments on commit 5296bc5

Please sign in to comment.