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 getBitmapFromAsset(Context context, String strName) {
    AssetManager assetManager = context.getAssets();

    InputStream istr;/*from   www.j a  v  a 2s .  c  o m*/
    Bitmap bitmap = null;
    try {
        istr = assetManager.open(strName);
        bitmap = BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
        return null;
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap fetchImage(String fileUrl) {
    try {/*from   w  w w  .  ja  va  2s  .  com*/
        URL myFileUrl = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        Bitmap image = BitmapFactory.decodeStream(is);
        is.close();
        return image;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap readBitmapFile(String aFileName) {
    Bitmap bitmap = null;/*from w w w  .  ja  va2s  .c  o m*/
    File file = new File(aFileName);
    try {
        FileInputStream fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

/**
 * get bitmap from assert//from   w  ww  . ja  v  a 2s  .c  o  m
 * 
 * @param context
 * @param fileName
 * @return
 */
public static Bitmap getImageFromAssetFile(Context context, String fileName) {
    Bitmap image = null;
    try {
        AssetManager am = context.getAssets();
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {

    }
    return image;
}

From source file:Main.java

public static Bitmap loadImageFromNet(String url) {
    URL m;//  w  w w . j  a va  2 s .co  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

public static Drawable getAsssetDrawableByName(Context context, String name) {
    try {/*from  ww  w  .  j  a v  a2 s .  co m*/
        InputStream is = context.getAssets().open(name);
        return new BitmapDrawable(context.getResources(), BitmapFactory.decodeStream(is));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ColorDrawable(Color.TRANSPARENT);
}

From source file:Main.java

public static Bitmap getBitmapFromUrl(String urlSource) {
    try {//from  www.  j  a  v a 2s . c o  m
        URL url = new URL(urlSource);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap GetFromFile(String fileName, String path) {
    String fString = fileName.replaceAll("/", ".");
    File file = new File("/sdcard/OneBus/user/" + path + "/" + fString + ".jpg");
    if (!file.exists()) {
        return null;
    }//from   www . jav  a  2 s  . c  om
    try {

        FileInputStream fis = new FileInputStream(file);
        return BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String strURL) {
    try {/*from ww w .  ja  v  a 2s .c om*/
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        return BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * @param filePath example("pictures/picture_1.jpg")
 *//*ww w .  j  a va  2s . co  m*/
public static Bitmap getAssetsPicture(Context context, String filePath) {
    Bitmap bitmap = null;
    AssetManager assetManager = context.getAssets();
    try {
        InputStream inputStream = assetManager.open(filePath);
        bitmap = BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}