我需要从CBZ档案中获取图像并在我的PageViewer中显示它们。目前我正在解压缩CBZ存档并将图像放在SD卡上,这很慢......解压缩一张图像需要8秒钟。 有没有其他方法可以做到这一点?我不需要解压缩CBZ存档,只是让图像显示它们。任何建议都可以加快速度。
解压缩档案的代码:
package nl.MarcVale.ComicViewer;
/**
* Created by Marc on 23-8-13.
*/
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class DecompressZip {
private String _zipFile;
private String _location;
public DecompressZip(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
编辑:答案后还需要更多代码:
我正在创建zipFile来缩小它,因为图像太大了。我在第一个decodeFromStream行收到OutOfMemory异常。
ZipFile zipFile = null;
try {
zipFile = new ZipFile("/sdcard/file.cbz");
} catch (IOException e) {
e.printStackTrace();
}
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
ZipEntry ze = e.nextElement();
try {
//Bitmap bm = BitmapFactory.decodeStream(zipFile.getInputStream(ze));
Bitmap bm = decodeScaledBitmapFromSdCard(zipFile.getInputStream(ze),width,height);
pages.add(bm);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static Bitmap decodeScaledBitmapFromSdCard(InputStream filePath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//BitmapFactory.decodeFile(filePath, options);
BitmapFactory.decodeStream(filePath, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(filePath, null, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
答案 0 :(得分:0)
您可以使用ZipFile。它会为每个条目提供InputStream
,您可以使用BitmapFactory.decodeStream解码。
ZipFile zipFile = new ZipFile(<filename>);
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
ZipEntry ze = e.nextElement();
Bitmap bm = BitmapFactory.decodeStream(zipFile.getInputStream(ze));
}