Android Bitmap Create getBitmap(Context context, InputStream inputStream)

Here you can find the source of getBitmap(Context context, InputStream inputStream)

Description

Gets the bitmap.

Parameter

Parameter Description
context the context
inputStream the input stream

Return

the bitmap

Declaration

public static Bitmap getBitmap(Context context, InputStream inputStream) 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

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

public class Main {
    /**/*w w  w .  jav a 2s  .c  om*/
     * Gets the bitmap.
     *
     * @param context the context
     * @param inputStream the input stream
     * @return the bitmap
     */
    public static Bitmap getBitmap(Context context, InputStream inputStream) {

        File cacheDir;

        // Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED))
            cacheDir = new File(
                    android.os.Environment.getExternalStorageDirectory(),
                    "Zoolook");
        else
            cacheDir = context.getCacheDir();
        if (!cacheDir.exists())
            cacheDir.mkdirs();

        File f = new File(cacheDir, "banner");
        try {
            Bitmap bitmap = null;
            OutputStream os = new FileOutputStream(f);
            CopyStream(inputStream, os);
            os.close();
            bitmap = decodeF(f);
            return bitmap;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * Copy stream.
     *
     * @param is the is
     * @param os the os
     */
    public static void CopyStream(InputStream is, OutputStream os) {
        final int buffer_size = 1024;
        try {
            byte[] bytes = new byte[buffer_size];
            for (;;) {
                int count = is.read(bytes, 0, buffer_size);
                if (count == -1)
                    break;
                os.write(bytes, 0, count);
            }
        } catch (Exception ex) {
        }
    }

    /**
     * Decode f.
     *
     * @param f the f
     * @return the bitmap
     */
    private static Bitmap decodeF(File f) {
        Bitmap b = null;
        try {

            BitmapFactory.Options bfo = new BitmapFactory.Options();
            FileInputStream fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, bfo);
            fis.close();

        } catch (Exception e) {

            e.printStackTrace();

        }
        return b;

    }
}

Related

  1. decodeFromResource(Resources res, int resId)
  2. decodeFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  3. decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight)
  4. decodeSampledBitmapFromResource(Resources res, int id, int reqWidth, int reqHeight)
  5. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  6. getImage(String absPath)
  7. createCompatibleImage(int width, int height)
  8. createCompatibleImage(BufferedImage image, int width, int height)
  9. getBitMapFromStream(String filename)