0

我需要从 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;
    }
4

1 回答 1

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

}
于 2013-08-23T09:35:52.080 回答