Fix loading chapter images in local feeds (#7016)
This commit is contained in:
parent
69f0daa2e8
commit
376c83d200
|
@ -49,6 +49,6 @@ public class ApGlideModule extends AppGlideModule {
|
|||
registry.append(String.class, InputStream.class, new ApOkHttpUrlLoader.Factory());
|
||||
registry.append(String.class, InputStream.class, new NoHttpStringLoader.StreamFactory());
|
||||
|
||||
registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory());
|
||||
registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory(context));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package de.danoeh.antennapod.ui.glide;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import androidx.annotation.NonNull;
|
||||
import com.bumptech.glide.Priority;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
|
@ -22,12 +25,17 @@ import okhttp3.Response;
|
|||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapterImage, ByteBuffer> {
|
||||
|
||||
public static class Factory implements ModelLoaderFactory<EmbeddedChapterImage, ByteBuffer> {
|
||||
private final Context context;
|
||||
|
||||
public Factory(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ModelLoader<EmbeddedChapterImage, ByteBuffer> build(@NonNull MultiModelLoaderFactory unused) {
|
||||
return new ChapterImageModelLoader();
|
||||
return new ChapterImageModelLoader(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,12 +44,16 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
|
|||
}
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
|
||||
public ChapterImageModelLoader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoadData<ByteBuffer> buildLoadData(@NonNull EmbeddedChapterImage model,
|
||||
int width,
|
||||
int height,
|
||||
@NonNull Options options) {
|
||||
return new LoadData<>(new ObjectKey(model), new EmbeddedImageFetcher(model));
|
||||
public LoadData<ByteBuffer> buildLoadData(@NonNull EmbeddedChapterImage model, int width,
|
||||
int height, @NonNull Options options) {
|
||||
return new LoadData<>(new ObjectKey(model), new EmbeddedImageFetcher(model, context));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -51,9 +63,11 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
|
|||
|
||||
static class EmbeddedImageFetcher implements DataFetcher<ByteBuffer> {
|
||||
private final EmbeddedChapterImage image;
|
||||
private final Context context;
|
||||
|
||||
public EmbeddedImageFetcher(EmbeddedChapterImage image) {
|
||||
public EmbeddedImageFetcher(EmbeddedChapterImage image, Context context) {
|
||||
this.image = image;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -61,9 +75,15 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
|
|||
|
||||
BufferedInputStream stream = null;
|
||||
try {
|
||||
if (image.getMedia().localFileAvailable()) {
|
||||
File localFile = new File(image.getMedia().getLocalMediaUrl());
|
||||
stream = new BufferedInputStream(new FileInputStream(localFile));
|
||||
boolean isLocalFeed = image.getMedia().getStreamUrl().startsWith(ContentResolver.SCHEME_CONTENT);
|
||||
if (isLocalFeed || image.getMedia().localFileAvailable()) {
|
||||
if (isLocalFeed) {
|
||||
Uri uri = Uri.parse(image.getMedia().getStreamUrl());
|
||||
stream = new BufferedInputStream(context.getContentResolver().openInputStream(uri));
|
||||
} else {
|
||||
File localFile = new File(image.getMedia().getLocalMediaUrl());
|
||||
stream = new BufferedInputStream(new FileInputStream(localFile));
|
||||
}
|
||||
IOUtils.skip(stream, image.getPosition());
|
||||
byte[] imageContent = new byte[image.getLength()];
|
||||
IOUtils.read(stream, imageContent, 0, image.getLength());
|
||||
|
|
Loading…
Reference in New Issue