Catch gif/ssiv OutOfMemoryError and show the user a oom message.

material
Floens 11 years ago
parent 53c4d66d92
commit 28674bc83f
  1. 12
      Clover/app/src/main/java/com/davemorrissey/labs/subscaleview/SubsamplingScaleImageView.java
  2. 23
      Clover/app/src/main/java/org/floens/chan/ui/view/CustomScaleImageView.java
  3. 20
      Clover/app/src/main/java/org/floens/chan/ui/view/ThumbnailImageView.java
  4. 1
      Clover/app/src/main/res/values/strings.xml

@ -1028,6 +1028,10 @@ public class SubsamplingScaleImageView extends View {
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
Log.e(TAG, "OOM in decode tile", e); Log.e(TAG, "OOM in decode tile", e);
System.gc(); System.gc();
final SubsamplingScaleImageView view = viewRef.get();
if (view != null) {
view.onOutOfMemory();
}
} }
return null; return null;
} }
@ -1092,6 +1096,10 @@ public class SubsamplingScaleImageView extends View {
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
Log.e(TAG, "OOM in decode tile", e); Log.e(TAG, "OOM in decode tile", e);
System.gc(); System.gc();
final SubsamplingScaleImageView view = viewRef.get();
if (view != null) {
view.onOutOfMemory();
}
} }
return null; return null;
} }
@ -1538,6 +1546,10 @@ public class SubsamplingScaleImageView extends View {
} }
protected void onOutOfMemory() {
}
/** /**
* Call to find whether the view is initialised and ready for rendering tiles. * Call to find whether the view is initialised and ready for rendering tiles.
*/ */

@ -20,6 +20,8 @@ import android.util.AttributeSet;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import org.floens.chan.utils.Utils;
public class CustomScaleImageView extends SubsamplingScaleImageView { public class CustomScaleImageView extends SubsamplingScaleImageView {
private InitedCallback initCallback; private InitedCallback initCallback;
@ -39,12 +41,33 @@ public class CustomScaleImageView extends SubsamplingScaleImageView {
protected void onImageReady() { protected void onImageReady() {
super.onImageReady(); super.onImageReady();
Utils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (initCallback != null) { if (initCallback != null) {
initCallback.onInit(); initCallback.onInit();
} }
} }
});
}
@Override
protected void onOutOfMemory() {
super.onOutOfMemory();
Utils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (initCallback != null) {
initCallback.onOutOfMemory();
}
}
});
}
public interface InitedCallback { public interface InitedCallback {
public void onInit(); public void onInit();
public void onOutOfMemory();
} }
} }

@ -158,14 +158,14 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener
image.setInitCallback(new CustomScaleImageView.InitedCallback() { image.setInitCallback(new CustomScaleImageView.InitedCallback() {
@Override @Override
public void onInit() { public void onInit() {
Utils.runOnUiThread(new Runnable() {
@Override
public void run() {
removeAllViews(); removeAllViews();
addView(image); addView(image);
callback.setProgress(false); callback.setProgress(false);
} }
});
@Override
public void onOutOfMemory() {
onOutOfMemoryError();
} }
}); });
} }
@ -213,6 +213,11 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener
e.printStackTrace(); e.printStackTrace();
onError(); onError();
return; return;
} catch (OutOfMemoryError e) {
System.gc();
e.printStackTrace();
onOutOfMemoryError();
return;
} }
GifImageView view = new GifImageView(getContext()); GifImageView view = new GifImageView(getContext());
@ -302,7 +307,7 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener
} }
public void onError() { public void onError() {
Toast.makeText(getContext(), R.string.image_preview_failed, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.image_preview_failed, Toast.LENGTH_SHORT).show();
callback.setProgress(false); callback.setProgress(false);
} }
@ -311,6 +316,11 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener
callback.setProgress(false); callback.setProgress(false);
} }
public void onOutOfMemoryError() {
Toast.makeText(getContext(), R.string.image_preview_failed_oom, Toast.LENGTH_SHORT).show();
callback.setProgress(false);
}
public void cancelLoad() { public void cancelLoad() {
if (imageRequest != null) { if (imageRequest != null) {
imageRequest.cancel(); imageRequest.cancel();

@ -61,6 +61,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<string name="image_play_state">Start / stop playing</string> <string name="image_play_state">Start / stop playing</string>
<string name="image_preview_failed">Failed to show image</string> <string name="image_preview_failed">Failed to show image</string>
<string name="image_preview_failed_oom">Failed to show image, out of memory</string>
<string name="image_not_found">Image not found</string> <string name="image_not_found">Image not found</string>
<string name="image_open_failed">Failed to open image</string> <string name="image_open_failed">Failed to open image</string>

Loading…
Cancel
Save