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);
}
}