Browse Source

Glide error handling for FileNotFoundException

pull/70/head
Ezerous 5 years ago
parent
commit
d1191d6006
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 3
      app/src/main/java/gr/thmmy/mthmmy/utils/ui/ImageDownloadDialogBuilder.java
  2. 33
      app/src/main/java/gr/thmmy/mthmmy/utils/ui/PhotoViewUtils.java
  3. 5
      app/src/main/res/drawable/ic_file_not_found.xml

3
app/src/main/java/gr/thmmy/mthmmy/utils/ui/ImageDownloadDialogBuilder.java

@ -56,9 +56,8 @@ public class ImageDownloadDialogBuilder extends AlertDialog.Builder{
private BaseActivity getBaseActivity() { private BaseActivity getBaseActivity() {
Context baseActivityContext = context; Context baseActivityContext = context;
while (baseActivityContext instanceof ContextWrapper) { while (baseActivityContext instanceof ContextWrapper) {
if (context instanceof BaseActivity) { if (context instanceof BaseActivity)
return (BaseActivity) context; return (BaseActivity) context;
}
baseActivityContext = ((ContextWrapper)context).getBaseContext(); baseActivityContext = ((ContextWrapper)context).getBaseContext();
} }
return null; return null;

33
app/src/main/java/gr/thmmy/mthmmy/utils/ui/PhotoViewUtils.java

@ -3,12 +3,20 @@ package gr.thmmy.mthmmy.utils.ui;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.github.chrisbanes.photoview.PhotoView; import com.github.chrisbanes.photoview.PhotoView;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
public class PhotoViewUtils { public class PhotoViewUtils {
@ -24,21 +32,32 @@ public class PhotoViewUtils {
PhotoView photoView = new PhotoView(context); PhotoView photoView = new PhotoView(context);
photoView.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, screenHeight)); photoView.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, screenHeight));
Glide.with(context) Glide.with(context)
.load(imageURL) .load(imageURL)
.fitCenter() .fitCenter()
.into(photoView); .error(R.drawable.ic_file_not_found)
.listener(new RequestListener<Drawable>() {
builder.addContentView(photoView, new ViewGroup.LayoutParams( @Override
ViewGroup.LayoutParams.MATCH_PARENT, public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
ViewGroup.LayoutParams.MATCH_PARENT)); photoView.setZoomable(false);
builder.show(); return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
photoView.setOnLongClickListener(v -> { photoView.setOnLongClickListener(v -> {
ImageDownloadDialogBuilder imageDownloadDialogBuilder = new ImageDownloadDialogBuilder(context, imageURL); ImageDownloadDialogBuilder imageDownloadDialogBuilder = new ImageDownloadDialogBuilder(context, imageURL);
imageDownloadDialogBuilder.show(); imageDownloadDialogBuilder.show();
return false; return false;
}); });
return false;
}
})
.into(photoView);
builder.addContentView(photoView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.show();
} }
} }

5
app/src/main/res/drawable/ic_file_not_found.xml

@ -0,0 +1,5 @@
<vector android:height="96dp" android:tint="#D926A69A"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="96dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4c-1.48,0 -2.85,0.43 -4.01,1.17l1.46,1.46C10.21,6.23 11.08,6 12,6c3.04,0 5.5,2.46 5.5,5.5v0.5H19c1.66,0 3,1.34 3,3 0,1.13 -0.64,2.11 -1.56,2.62l1.45,1.45C23.16,18.16 24,16.68 24,15c0,-2.64 -2.05,-4.78 -4.65,-4.96zM3,5.27l2.75,2.74C2.56,8.15 0,10.77 0,14c0,3.31 2.69,6 6,6h11.73l2,2L21,20.73 4.27,4 3,5.27zM7.73,10l8,8H6c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4h1.73z"/>
</vector>
Loading…
Cancel
Save