Android Glide 4.X:DataFetcher-尝试从ZIP文件加载图片

Android Glide 4.X:DataFetcher-尝试从ZIP文件加载图片,android,zip,android-glide,zipinputstream,appglidemodule,Android,Zip,Android Glide,Zipinputstream,Appglidemodule,我必须通过Glide对图像进行解密并将其加载到ImageView,并限制不要将解密的图像保存在磁盘上。据我所知,Glide有机会使用DataFetcher实现这一点。作为一个使用DataFetcher的例子,我想先从zip文件加载一个图像 A已阅读并基于我创建的,试图重写解压缩DataFetcher.loadData方法中的单个图片zip文件的代码,并将其加载到ImageView public class FileDataFetcher implements DataFetcher<Inp

我必须通过
Glide
对图像进行解密并将其加载到ImageView,并限制不要将解密的图像保存在磁盘上。据我所知,
Glide
有机会使用
DataFetcher
实现这一点。作为一个使用
DataFetcher
的例子,我想先从
zip
文件加载一个图像

A已阅读并基于我创建的,试图重写解压缩
DataFetcher.loadData
方法中的单个图片
zip
文件的代码,并将其加载到
ImageView

public class FileDataFetcher implements DataFetcher<InputStream> {
    private final FileEnveloper fileEnveloper;

    FileDataFetcher(FileEnveloper fileEnveloper) {
        this.fileEnveloper = fileEnveloper;
    }

    @Override
    public void loadData(@NonNull Priority priority, DataCallback<? super InputStream> callback) {
        try {
            unzip(); //Works fine!

            FileInputStream fin = new FileInputStream(fileEnveloper.getFile());
            ZipInputStream zin = new ZipInputStream(fin);
            callback.onDataReady(zin);
        } catch (IOException e) {
            e.printStackTrace();
            callback.onLoadFailed(e);
        }
    }

    @Override
    public void cleanup() {
        // Intentionally empty only because we're not opening an InputStream or another I/O resource!
    }

    @Override
    public void cancel() {
        // Intentionally empty.
    }

    @NonNull
    @Override
    public Class<InputStream> getDataClass() {
        return InputStream.class;
    }

    @NonNull
    @Override
    public DataSource getDataSource() {
        return DataSource.LOCAL;
    }

    private void unzip() throws IOException {
        String location = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator ;
        FileInputStream fin = new FileInputStream(fileEnveloper.getFile());
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                File dir = new File(location + ze.getName());
                if (!dir.exists() || !dir.isDirectory())
                    dir.mkdirs();
            } else {
                FileOutputStream fout = new FileOutputStream(location + ze.getName());
                byte[] buffer = new byte[1024];
                int read = 0;
                while ((read = zin.read(buffer)) != -1) {
                    fout.write(buffer, 0, read);
                }
                zin.closeEntry();
                fout.close();
            }
        }
        zin.close();
    }
}
我想原因不在于
Glide
,而在于
ZipInputStream
Glide
的错误用法。有人知道应该怎么做吗?

要让它工作(对于
zip
-archive中的单个文件),我们必须使用
zin.getnextery()
ZipInputStream
设置在初始位置,如下所示:

@Override
public void loadData(@NonNull Priority priority, DataCallback<? super InputStream> callback) {
    try {
        unzip(); //Works fine!

        FileInputStream fin = new FileInputStream(fileEnveloper.getFile());
        ZipInputStream zin = new ZipInputStream(fin);
        zin.getNextEntry();//Essential!!!
        callback.onDataReady(zin);
    } catch (IOException e) {
        e.printStackTrace();
        callback.onLoadFailed(e);
    }
}
@覆盖
public void loadData(@NonNull Priority Priority,DataCallback
@Override
public void loadData(@NonNull Priority priority, DataCallback<? super InputStream> callback) {
    try {
        unzip(); //Works fine!

        FileInputStream fin = new FileInputStream(fileEnveloper.getFile());
        ZipInputStream zin = new ZipInputStream(fin);
        zin.getNextEntry();//Essential!!!
        callback.onDataReady(zin);
    } catch (IOException e) {
        e.printStackTrace();
        callback.onLoadFailed(e);
    }
}