我需要在很多活动中从扩展文件中访问很多图像。
执行:
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(context, 8, -1);
fileStream = expansionFile.getInputStream("drawables/drawable-hdpi/" + image);
Drawable drawable = Drawable.createFromStream(fileStream, null);
每个活动都很慢,在某些活动中我需要同时加载4个以上的图像。
那么,如果我创建一个在每个活动中抽象出这个代码的新类,你会怎么想?
答案 0 :(得分:0)
所以我创建了一个ExpansionFileRetriever类,它检索扩展文件:
公共类ExpansionFileRetriever {
private static ExpansionFileRetriever instance;
public static ExpansionFileRetriever get() {
if(instance == null) instance = getSync();
return instance;
}
private static synchronized ExpansionFileRetriever getSync() {
if(instance == null) instance = new ExpansionFileRetriever();
return instance;
}
public ExpansionFileRetriever(){
// here you can directly access the Application context calling
Context c = App.get();
try {
expansionFile = APKExpansionSupport.getAPKExpansionZipFile(c, 20, -1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ZipResourceFile expansionFile;
public Drawable getDrawable(String path, String resource) {
InputStream fileStream = null;
String file = path + resource;
try {
fileStream = expansionFile.getInputStream(file);
Drawable drawable = Drawable.createFromStream(fileStream, null);
return drawable;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
每当我需要访问扩展文件的文件时
我只是用
ExpansionFileRetriever expFile = ExpansionFileRetriever.get();
并且得到,说Drawable
我所做的就是:
expFile.getDrawable(path, name)