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 getBitmap(Context context, String fileName) {
    FileInputStream fis = null;//from w w w.j  ava2s  . c om
    Bitmap bitmap = null;
    try {
        fis = context.openFileInput(fileName);
        bitmap = BitmapFactory.decodeStream(fis);
    } catch (FileNotFoundException e) {
    } catch (OutOfMemoryError e) {
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}

From source file:Main.java

public static boolean setAssetImage(ImageView imageView, String path, String filename) {
    if (filename == null || "".equals(filename) || "NULL".equalsIgnoreCase(filename)) {
        return false;
    }//from ww w. ja  v a  2  s .c o m
    String _path = (path == null) ? "" : path;

    InputStream is = null;
    try {
        is = imageView.getContext().getAssets().open(_path + filename);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    imageView.setImageBitmap(bitmap);

    return true;
}

From source file:Main.java

public static Bitmap getGoogleMapThumbnail(double latitude, double longitude) {
    String URL = "http://maps.google.com/maps/api/staticmap?center=" + latitude + "," + longitude
            + "&zoom=15&size=600x600&sensor=false";

    Bitmap bmp = null;/*w w w .jav  a  2s . c  o m*/
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);

    InputStream in = null;
    try {
        in = httpclient.execute(request).getEntity().getContent();
        bmp = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bmp;
}

From source file:Main.java

public static Bitmap load_bmp_image(String fichero) {
    Bitmap bmp = null;/*from  w w w  .java  2 s  .  c om*/
    try {
        File f = new File(fichero);
        bmp = BitmapFactory.decodeStream(new FileInputStream(f));

    } catch (Exception e) {

    }
    return bmp;
}

From source file:Main.java

public static Bitmap getImage(String urlpath) throws Exception {
    Bitmap bitmap = null;/*w w  w.  j a  v  a  2  s  .c  om*/
    URL url = new URL(urlpath);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5 * 1000);
    if (conn.getResponseCode() == 200) {
        InputStream inputStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap downLoadBitmap(String httpUrl) {
    InputStream inputStream = null;
    try {/*w w  w. ja  va2  s.  c  om*/
        URL url = new URL(httpUrl);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = conn.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            return bitmap;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

From source file:Main.java

private static boolean loadAssetImage(ImageView view, String filename) {

    InputStream input = null;//ww w.  j av a 2 s.c  om
    try {
        input = view.getContext().getAssets().open(filename);
        Bitmap icon = BitmapFactory.decodeStream(input);
        view.setImageBitmap(icon);
        input.close();
        return true;
    } catch (Exception error) {
    } finally {
        silentClose(input);
    }
    return false;
}

From source file:Main.java

public static Bitmap decodeUriAsBitmap(Context mContext, Uri uri) {
    Bitmap bitmap;//from   w w w. j  av  a 2  s .c  o  m
    try {
        bitmap = BitmapFactory.decodeStream(mContext.getContentResolver().openInputStream(uri));
    } catch (FileNotFoundException e) {
        return null;
    }
    return bitmap;
}

From source file:Main.java

static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w,
        int h) {/*  w w  w  .  ja v a2s  . com*/
    if (str_url == null || str_url.length() <= 0)
        return null;

    BitmapDrawable thumb = null;
    try {
        URL url = new URL(str_url);
        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream stream = connection.getInputStream();
        BufferedInputStream data = new BufferedInputStream(stream);

        Bitmap bitmap = BitmapFactory.decodeStream(data);
        if (bitmap != null) {
            if (scale)
                thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true));
            else
                thumb = new BitmapDrawable(resources, bitmap);
        }

    } catch (MalformedURLException e) {
        //e.printStackTrace();
    } catch (IOException e) {
        //e.printStackTrace();
    }

    return thumb;
}

From source file:Main.java

public static Uri getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;// w  w  w.j  a  v  a  2s .  c o m

    try {

        if (uri.getAuthority() == null) {
            return null;
        } else if (uri.getAuthority() != null) {
            try {
                is = context.getContentResolver().openInputStream(uri);
                Bitmap bmp = BitmapFactory.decodeStream(is);

                bitmapResult = bmp;
                return writeToTempImageAndGetPathUri(context, bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}