load Image from Zip File - Android Graphics

Android examples for Graphics:Image File

Description

load Image from Zip File

Demo Code

/*//www  . j av a 2s  . co m
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
  public static Bitmap loadPreviewFrame(Context context, InputStream is, String previewName) throws IOException {
    ZipInputStream zis = (is instanceof ZipInputStream) ? (ZipInputStream) is
        : new ZipInputStream(new BufferedInputStream(is));
    ZipEntry ze;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = am.isLowRamDevice() ? 4 : 2;
    opts.inPreferredConfig = Bitmap.Config.RGB_565;
    // First thing to do is iterate over all the entries and the zip and store them
    // for building the animations afterwards
    Bitmap preview = null;
    while ((ze = zis.getNextEntry()) != null && preview == null) {
      final String entryName = ze.getName();
      if (entryName.equals(previewName)) {
        preview = BitmapFactory.decodeStream(zis, null, opts);
      }
    }
    zis.close();

    return preview;
  }
}

Related Tutorials