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 getImageFromAssetsFile(Context ct, String fileName) {
    Bitmap image = null;/*ww  w.j  a  v  a2s . c o  m*/
    AssetManager am = ct.getAssets();
    try {
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;

}

From source file:Main.java

public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
    Bitmap image = null;// w  ww  .j  a  v  a 2  s  .c om
    AssetManager am = context.getAssets();
    InputStream is = null;
    try {
        is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        return image;
    } catch (IOException e) {
        e.printStackTrace();
        return image;
    } finally {
        if (is != null) {
            try {
                is.close();
                is = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 *
 * @param path//w w w  . ja  va 2s.c  om
 * @return
 */
public static Bitmap returnBmpByPath(String path) {
    Bitmap bitmap = null;
    File file = new File(path);
    try {
        if (file.exists()) {
            FileInputStream is = new FileInputStream(file);
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Log.v(tag, bitmap.toString());

    return bitmap;

}

From source file:Main.java

public static Bitmap getPlusBitmap(Context context, String s) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w w w .  ja v  a  2  s.c om
    Bitmap bitmap = BitmapFactory.decodeStream(getCommunityPicInputStream(s));
    Bitmap bitmap1;
    if (bitmap != null) {
        Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), 0x7f02003f);
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
        canvas.drawBitmap(bitmap2, bitmap.getWidth() - bitmap2.getWidth() / 2,
                bitmap.getHeight() - bitmap2.getHeight() / 2, paint);
        bitmap2.recycle();
    } else {
        bitmap1 = null;
    }
    return bitmap1;
}

From source file:Main.java

private static Bitmap getBitmapFromAsset(Context context, String filePath) {
    AssetManager assetManager = context.getAssets();

    InputStream istream;//from   w w  w.j  a  va2  s  .  com
    Bitmap bitmap = null;
    try {
        istream = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(istream);
    } catch (IOException e) {
        // handle exception
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap getbmFromAssetsFile(Resources res, String fileName) {
    if (res == null)
        return null;
    Bitmap bm = null;/*w  w w . j a  va2 s  .c  o  m*/
    AssetManager am = res.getAssets();
    try {
        InputStream is = am.open(fileName);
        bm = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bm;

}

From source file:Main.java

public static Drawable changeLocalAvatar(Context context, Uri uri) {
    ContentResolver cr = context.getContentResolver();
    Bitmap bitmap = null;//from  ww w  . j  a v a2  s .  c  o  m
    try {
        bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return new BitmapDrawable(bitmap);
}

From source file:Main.java

public static Bitmap readBitmap(InputStream stream) {
    return BitmapFactory.decodeStream(stream);
}

From source file:Main.java

/**
 * Converts an inputstream to a byte array (Mostly useful for sending images via JSON)
 * @param is Input stream, if using a URI, open it by calling:
 *     InputStream iStream =   context.getContentResolver().openInputStream(uri);
 * @return Byte Array//from ww  w  .j  av  a  2  s  .com
 */
public static Bitmap convertISToBitmap(InputStream is) {
    try {
        return BitmapFactory.decodeStream(is);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Drawable GetUrlDrawable(String url) {
    try {//  w  w w  .java 2  s  .c om
        URL aryURI = new URL(url);
        URLConnection conn = aryURI.openConnection();
        InputStream is = conn.getInputStream();
        Bitmap bmp = BitmapFactory.decodeStream(is);
        return new BitmapDrawable(bmp);
    } catch (Exception e) {
        Log.e("ERROR", "urlImage2Drawable failed with image url at " + url, e);
        return null;
    }
}