2013-08-23 2 views
0

Мне нужно получить изображения из архивов CBZ и отобразить их в моем PageViewer. На данный момент я разархивирую архив CBZ и размещаю изображения на SD-карте, которая так медленна ... Для разархивирования одного изображения требуется 8 секунд. Есть ли другой способ сделать это? Мне не нужно разархивировать архив CBZ, но просто получите изображения, чтобы отобразить их. Любые предложения были бы хороши, чтобы ускорить это.Получение изображений из архива 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(); 
    } 
} 
} 

Edit: Еще немного кода после ответа:

Я создаю ZipFile для уменьшения масштабов его, потому что изображения являются слишком большими. Я получаю исключение OutOfMemory в первой строке decodeFromStream.

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

Вы можете использовать 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)); 

} 
+0

Потому что у меня нет идеи, как это использовать. Вы можете помочь? – Marc

+0

Я смутил ZipInputStream и ZipFile, я сменил ответ – bwt

+0

Звук прошу прощения, сейчас попробую. – Marc

Смежные вопросы