Android 从CBZ存档获取图像

Android 从CBZ存档获取图像,android,unzip,zipfile,Android,Unzip,Zipfile,我需要从CBZ档案中获取图像,并在我的PageViewer中显示它们。目前我正在解压CBZ存档文件并将图像放在SD卡上,速度太慢了。。。解压一个图像需要8秒钟。 还有别的办法吗?我不需要解压CBZ存档,只需要让图像显示它们。任何建议都可以加快速度 要解压缩存档的代码: package nl.MarcVale.ComicViewer; /** * Created by Marc on 23-8-13. */ import android.util.Log; import java.io.Fi

我需要从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;
    }
ZipFile ZipFile=null;
试一试{
zipFile=新的zipFile(“/sdcard/file.cbz”);
}捕获(IOE异常){
e、 printStackTrace();
}
对于(枚举您可以使用。它为每个条目提供
InputStream
,您可以使用解码

ZipFile-ZipFile=new-ZipFile();

对于(枚举),因为我不知道如何使用它。你能帮我吗?我混淆了ZipInputStream和ZipFile,我更改了答案Sound Promming,现在要尝试一下。我尝试过这样做,但似乎做不到。是否有可能获得直接位置(字符串位置)到zip中的文件?因为我需要缩小图像的比例。
decodeStream
方法之一采用
BitmapFactory.Options
。您可以
getInputStream
并对流解码两次(一次使用
inJustDecodeBounds
,第二次使用
inSampleSize
设置)。
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));

}