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:net.idlesoft.android.apps.github.utils.GravatarCache.java

private static Bitmap downloadGravatar(final String id) throws IOException {
    final URL aURL = new URL("http://www.gravatar.com/avatar/" + URLEncoder.encode(id) + "?size=100&d=mm");
    final HttpURLConnection conn = (HttpURLConnection) aURL.openConnection();
    conn.setDoInput(true);/*from   www.j a  va2 s.c  o m*/
    conn.connect();
    final InputStream is = conn.getInputStream();
    final Bitmap bm = BitmapFactory.decodeStream(is);
    is.close();
    return bm;
}

From source file:Main.java

/**
 * @param url//from   w ww .java 2 s  .co m
 * @return
 */
public static Bitmap getBitMapFromURL(URL url) {
    Bitmap mBitmap = null;

    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        conn.getContentLength();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        mBitmap = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
    } catch (Exception e) {
        Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage());
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:Main.java

public static int initTexture(Context context, int drawableId)// textureId
{
    int[] textures = new int[1];
    glGenTextures(1, textures, 0);// w w  w  .  j av  a 2s. c  o  m
    int textureId = textures[0];
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    InputStream is = context.getResources().openRawResource(drawableId);
    Bitmap bitmapTmp;
    try {
        bitmapTmp = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmapTmp, 0);
    bitmapTmp.recycle();

    return textureId;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromInputStream(InputStream is) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Bitmap.Config.RGB_565;
    opt.inJustDecodeBounds = false;//ww  w  .ja v  a  2s. c  o m
    opt.inSampleSize = 2;
    return BitmapFactory.decodeStream(is);
}

From source file:mc.lib.assets.AssetsHelper.java

public static Bitmap readImageToBitmap(Context context, String path) {
    InputStream is = null;/* w ww.jav  a2s  . c  o m*/
    try {
        is = context.getAssets().open(path);
        return BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        Log.e(LOGTAG, "Can not read asset " + path, e);
    } finally {
        StreamHelper.close(is);
    }
    return null;
}

From source file:com.codingrhemes.steamsalesmobile.HttpThumbnails.java

public static Bitmap readPictureFromTheWeb(String URL) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    Bitmap thePicture = null;//w ww. jav a2s .  c  o  m

    try {

        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            thePicture = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } else {
            Log.d("JSON", "Failed to download file");
        }
    } catch (Exception e) {
        Log.d("HttpThumbnails", e.getLocalizedMessage());
    }
    return thePicture;
}

From source file:Main.java

/**
 * retrieve the contact photo given a contact id
 *
 * @param context//  ww  w .ja va2 s  .c  o  m
 *            a context object used to get a content resolver
 * @param id
 *            the id number of contact
 *
 * @return the bitmap of the photo or null
 */
public static Bitmap loadContactPhoto(Context context, long id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);

    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
            uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

From source file:com.apps.android.viish.encyclolpedia.tools.ImageManager.java

public static Bitmap getImage(Context context, String fileName) throws IOException {
    FileInputStream fis = getFileInputStream(context, fileName);
    Bitmap bm = BitmapFactory.decodeStream(fis);
    fis.close();/*w  w  w.j ava 2 s.  c o  m*/

    return bm;
}

From source file:Main.java

public static Bitmap decodeStream(InputStream inputStream) {
    try {/*from   w  w w  .  j a v a2s  . co  m*/
        return BitmapFactory.decodeStream(inputStream);
    } catch (OutOfMemoryError e) {
        Log.e(LOGTAG, "decodeStream() OOM!", e);
        return null;
    }
}

From source file:com.airbop.library.simple.AirBopImageDownloader.java

static public Bitmap downloadBitmap(String url, Context context) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {//from  www .  j a  va2 s .  c om
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            displayMessage(context, "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // Could provide a more explicit error message for IOException or IllegalStateException
        getRequest.abort();
        displayMessage(context, "Error while retrieving bitmap from " + url + e.toString());
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}