Here you can find the source of getBitmap(Context context, InputStream inputStream)
Parameter | Description |
---|---|
context | the context |
inputStream | the input stream |
public static Bitmap getBitmap(Context context, InputStream inputStream)
//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; } }