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 void setFBImage(final String fbid, final Context context, final ImageView v) {
    new AsyncTask<String, Void, Bitmap>() {

        @Override//from w ww . j a va 2  s  .  c  om
        protected Bitmap doInBackground(String... strings) {
            File img = new File(context.getFilesDir() + "/profile.jpg");
            Bitmap bmp = null;
            if (img.exists()) {
                try {
                    bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
                } catch (Exception E) {
                    E.printStackTrace();
                }
            } else {
                try {
                    URL img_url = new URL("https://graph.facebook.com/" + String.valueOf(fbid)
                            + "/picture?type=large&redirect=true&width=400&height=400");
                    bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
                    FileOutputStream fOut = new FileOutputStream(img);
                    bmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
            return bmp;
        }

        @Override
        protected void onPostExecute(Bitmap img) {
            if (img != null) {
                v.setImageBitmap(img);
            }
        }
    }.execute();
}

From source file:Main.java

public static Bitmap load_bitmap_from_uri_string(Context context, String uri, int sizeX, int sizeY) {
    Bitmap small_bitmap = null;/*www. j a  va 2 s . c  om*/

    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            small_bitmap = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
            bitmap.recycle();

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

    return small_bitmap;
}

From source file:Main.java

/**
 * Decodes an InputStream to Bitmap without resizing the image.<br>
 * Be careful, if the image is big this can become a problem because it will use to much memory.
 * @param inputStream an input stream to an image
 * @return the Bitmap/* w  ww. j a v  a  2s.c om*/
 */
public static Bitmap decodeBitmapFromInputStream(InputStream inputStream) {
    return BitmapFactory.decodeStream(inputStream);
}

From source file:Main.java

/**
 * Retrieves the Yahoo! avatar of the specified user.
 * // ww  w .  ja  v a 2  s. com
 * @param userId
 *            The ID of the user to get the avatar of.
 * @return A bitmap containing the avatar which is received.
 */
public static Bitmap getYahooAvatar(final String userId) {
    try {
        return BitmapFactory
                .decodeStream(new URL("http://img.msg.yahoo.com/avatar.php?yids=" + userId).openStream());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static Drawable load_drawable_from_uri_string(Context context, String uri, int sizeX, int sizeY) {
    Drawable drawable = null;/*from   ww  w.j a  va2s.  c om*/
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

private static Bitmap readScaledBitmapFromUri(Uri photoUri, Context context, int width, int height)
        throws FileNotFoundException, IOException {
    Log.i("Photo Editor", "Read Scaled Bitmap: " + width + " " + height);
    InputStream is;/*  w w  w  . j av a2s .c om*/
    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (width > MAX_IMAGE_DIMENSION || height > MAX_IMAGE_DIMENSION) {
        float ratio = calculateScaleRatio(width, height);
        Log.i("Photo Editor", "Scaled Bitmap: " + ratio);
        srcBitmap = readRoughScaledBitmap(is, ratio);
        ratio = calculateScaleRatio(srcBitmap.getWidth(), srcBitmap.getHeight());
        srcBitmap = scaleBitmap(srcBitmap, ratio);
    } else {
        Log.i("Photo Editor", "NOT Scaled Bitmap ");
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();
    return srcBitmap;
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlStr) {
    Bitmap bitmap = null;/*from w  ww.ja  v a2s  .com*/
    try {
        URL url = new URL(urlStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        Log.e(LOG_TAG, "cannot get the image from link :" + urlStr);
    }
    return bitmap;
}

From source file:Main.java

public static Drawable get_scaled_drawable_from_uri_string_for_square_container(Context context, String uri,
        int max_size) {
    Drawable drawable = null;//from   www. java  2s  . c  o m
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            int sx = bitmap.getWidth();
            int sy = bitmap.getHeight();

            int fsx = max_size;
            int fsy = max_size;

            if (sy > sx)
                fsx = (int) ((float) max_size * ((float) sx / (float) sy));
            else if (sx > sy)
                fsy = (int) ((float) max_size * ((float) sy / (float) sx));

            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, fsx, fsy, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

static Bitmap createBitmap(String src) throws IOException {
    if (src == null || src.length() == 0) {
        // no image source defined
        return null;
    }/*from  w  ww  .  ja  va2s. co  m*/

    InputStream inputStream = createInputStream(src);
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    inputStream.close();
    return bitmap;
}

From source file:Main.java

/**
 * Method to get the current user image saved
 *
 * @param path the saved path for the image
 * @return the bitmap that will be displayed
 *///ww w  .ja v a 2  s .c o m
public static Bitmap loadUserImageFromStorage(String path) {
    Bitmap bitmap;
    try {
        if (path != null) {
            File file = new File(path);
            bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
            return bitmap;
        }
    } catch (FileNotFoundException e) {
    }
    return null;
}