Example usage for android.graphics BitmapFactory decodeStream

List of usage examples for android.graphics BitmapFactory decodeStream

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeStream.

Prototype

public static Bitmap decodeStream(InputStream is) 

Source Link

Document

Decode an input stream into a bitmap.

Usage

From source file:Main.java

public static Bitmap getBitmapFromRemote(String address) {
    Bitmap bitmap = null;/*from   w w  w.j a  v  a  2  s .  co m*/
    try {
        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap loadImageFromUrl(String url) {
    URL m;/*from   ww  w . j a  v a 2s . c o m*/
    InputStream i = null;
    try {
        m = new URL(url);
        i = (InputStream) m.getContent();
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return BitmapFactory.decodeStream(i);
}

From source file:Main.java

private static byte[] getBytesFromDriveImageUri(Context context, Uri uri) {
    InputStream inputStream = null;
    try {// www . j a v  a 2 s .  c o  m
        inputStream = context.getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

    return stream.toByteArray();
}

From source file:Main.java

public static Bitmap decodeResourcesBitmap(Context context, int res) {
    boolean bool = BITMAP_CACHE.containsKey(Integer.valueOf(res));
    Bitmap localBitmap = null;//w w w .j a va  2  s  .c  o m
    if (bool) {
        localBitmap = (Bitmap) BITMAP_CACHE.get(Integer.valueOf(res));
    }
    if ((localBitmap == null) || (localBitmap.isRecycled())) {
        localBitmap = BitmapFactory.decodeStream(context.getResources().openRawResource(res));
        BITMAP_CACHE.put(Integer.valueOf(res), localBitmap);
    }
    return localBitmap;
}

From source file:Main.java

public static Bitmap loadImageFromStorage(Context context) {
    Bitmap bitmap = null;//  w  ww  .  j  a va  2s  . co  m
    try {
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        File f = new File(directory, "gravatar.jpg");
        bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return bitmap;

}

From source file:Main.java

public static Bitmap getEmotion(Context context, String key) {
    String fileName = "default_emotions_package/" + MAP.get(key);
    try {//from w ww.  ja  v  a  2  s. c o  m
        AssetManager am = context.getAssets();
        return BitmapFactory.decodeStream(am.open(fileName));
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    return null;
}

From source file:Main.java

public static Bitmap getBitmapFromAsset(FragmentActivity fragmentActivity, String strName) {
    try {//from  w ww.ja v  a  2 s.co m
        AssetManager assetManager = fragmentActivity.getAssets();

        InputStream inStream = null;
        try {
            inStream = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Bitmap bitmap = BitmapFactory.decodeStream(inStream);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Bitmap decodeResourcesBitmap(Context paramContext, int paramInt) {
    boolean bool = RESOURCES_BITMAP_CACHE.containsKey(Integer.valueOf(paramInt));
    Bitmap localBitmap = null;/*from  w  w  w.  j  ava2  s  . c  om*/
    if (bool) {
        localBitmap = (Bitmap) RESOURCES_BITMAP_CACHE.get(Integer.valueOf(paramInt));
    }
    if ((localBitmap == null) || (localBitmap.isRecycled())) {
        localBitmap = BitmapFactory.decodeStream(paramContext.getResources().openRawResource(paramInt));
        RESOURCES_BITMAP_CACHE.put(Integer.valueOf(paramInt), localBitmap);
    }
    return localBitmap;
}

From source file:Main.java

public static Bitmap loadBitmapFromAssets(Context context, String filePath) {
    InputStream inputStream = null;
    try {/*  w  w w . j a  v  a2 s  . c  o m*/
        inputStream = context.getResources().getAssets().open(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (inputStream == null)
        return null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    return bitmap;
}

From source file:Main.java

public static void compressWithQuality(Bitmap image, String outPath, int maxSize) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    image.compress(Bitmap.CompressFormat.JPEG, 20, os);

    while (os.toByteArray().length / 1024 > 1024) {
        os.reset();/*from   ww w. j a v  a2  s .  c om*/
        image.compress(Bitmap.CompressFormat.JPEG, 20, os);
    }

    ByteArrayInputStream bi = new ByteArrayInputStream(os.toByteArray());
    BitmapFactory.decodeStream(bi);
    FileOutputStream fi = new FileOutputStream(outPath);
    fi.write(os.toByteArray());
    fi.flush();
    fi.close();

}