AndroidプラットフォームBitmapはファイルとしてキャッシュされます


gifなどのピクチャフォーマットを解析中に復号して得られたBitmapをピクチャに変換するにはどうすればよいのでしょうか.Bitmap.JAvaでは、Bitmapをファイルに変換することができるcompressメソッドが用意されています.JAvaのdecodeStreamメソッドに対応します.次の2つのメソッドの関数のプロトタイプを示します.
public static Bitmap decodeStream(InputStream is);
public boolean compress(CompressFormat format, int quality, OutputStream stream);
の下はFileUtilsです.JAvaのコードの1つです.まずdecodeStreamでシステムresの画像をBitmapに復号し、compressでファイルに転送します.
        final File mf = new File(context.getFilesDir(), filename);
        if (!mf.exists()) {
            Bitmap bitmap = null;
            FileOutputStream fos = null;
            InputStream is = null;
            try {
                is = context.getResources().openRawResource(maskRawResourceId);
                bitmap = BitmapFactory.decodeStream(is);
                if (bitmap == null) {
                    throw new IllegalStateException("Cannot decode raw resource mask");
                }

                fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
                if (!bitmap.compress(CompressFormat.JPEG, 100, fos)) {
                    throw new IllegalStateException("Cannot compress bitmap");
                }
            } finally {
                if (is != null) {
                    is.close();
                }

                if (bitmap != null) {
                    bitmap.recycle();
                }

                if (fos != null) {
                    fos.flush();
                    fos.close();
                }
            }
        }
では本題に戻り、gifアニメーション解析中のフレームごとの画像をどのようにキャッシュしますか?方法は次のとおりです.
        private void bitmap2ImageFile(Bitmap bitmap, int index, int totalCount) {
            if ((index == totalCount - 1) || mSaveOnce) {
                mSaveOnce = true;
                return;
            }

            if (bitmap == null) {
                return;
            }

            String bitmapName = Environment.getExternalStorageDirectory().toString() + "/gif/" + index + ".png";
            File f = new File(bitmapName);
            FileOutputStream fos = null;
            try {
                f.createNewFile();
                fos = new FileOutputStream(f);
                
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);                
            } finally {
                if (fos != null) {
                    fos.flush();
                    fos.close();                    
                }
            }                       
        }
ここで、パラメータbitmapは現在のピクチャの現在のフレームが復号されたBitmapであり、indexは現在のフレームインデックス番号(0から)であり、totalCountはgifアニメーションのフレーム数である.gifアニメーションのすべてのフレームピクチャの重複保存を避けるために、変数boolean mSaveOnceを追加し、すべてのフレームを保存した後、フラグをtrueとし、後で保存しません.
さらに、compressの最初のパラメータは、以下のフォーマットをサポートすることができます.
    public enum CompressFormat {
        JPEG    (0),
        PNG     (1),
        WEBP    (2);

        CompressFormat(int nativeInt) {
            this.nativeInt = nativeInt;
        }
        final int nativeInt;
    }