Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting images from CBZ archive
    primarykey
    data
    text
    <p>I need to get images from CBZ archives and display them in my PageViewer. At the moment I'm unzipping the CBZ archive and placing the images on the SD-Card which is so slow... It takes 8 seconds to unzip one image. Is there any other way to do this? I don't need to unzip the CBZ archive, but just get the images to display them. Any suggestions would be great to speed this up.</p> <p>Code to unzip the archive:</p> <pre><code>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(); } } } </code></pre> <p>Edit: Some more code after a answer:</p> <p>I'm creating the zipFile to downscale it, because the images are way too big. I'm getting a OutOfMemory exception at the first decodeFromStream line.</p> <pre><code>ZipFile zipFile = null; try { zipFile = new ZipFile("/sdcard/file.cbz"); } catch (IOException e) { e.printStackTrace(); } for (Enumeration&lt;? extends ZipEntry&gt; 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 &gt; reqHeight || width &gt; 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 &lt; widthRatio ? heightRatio : widthRatio; } return inSampleSize; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload