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:com.samsunghack.apps.android.utils.ImageDownloader.java

Bitmap downloadBitmap(String url) {
    final int IO_BUFFER_SIZE = 4 * 1024;

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = (mode != Mode.CORRECT) ? new DefaultHttpClient()
            : AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {/*from  ww w  .java  2  s  .c  om*/
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

From source file:com.blueverdi.rosietheriveter.PhotoViewActivity.java

private Bitmap getBitmapFromAssets(String fileName) {
    String fn = FilenameUtils.concat(imageSource, fileName);
    Bitmap bit = null;//from  www. java2s . co  m
    try {
        InputStream bitmap = getAssets().open(fn);
        bit = BitmapFactory.decodeStream(bitmap);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bit;
}

From source file:com.mimming.sugarglider.MapImageManager.java

private void fetchMap(final Location location, final int zoom, final ImageFoundCallback callback) {
    new AsyncTask<String, Void, Bitmap>() {
        @Override//  w  w w  .ja v a 2 s .com
        protected Bitmap doInBackground(String... urls) {
            try {
                HttpResponse response = new DefaultHttpClient().execute(new HttpGet(urls[0]));

                InputStream is = response.getEntity().getContent();

                return BitmapFactory.decodeStream(is);
            } catch (Exception e) {
                Log.v(TAG, "Failed to load image: " + e.getMessage());
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                mapCache.put(toCacheKey(location, zoom), bitmap);
                if (callback != null) {
                    callback.callback(bitmap);
                }
            }
        }
    }.execute(toStaticMapsApiUrl(location, zoom));
}

From source file:com.frostwire.android.gui.views.ImageLoader.java

private Bitmap getBitmap(Context context, byte fileType, long id) {
    Bitmap bmp = null;//from  w  w w.  j  a va2  s  .  c om

    try {
        ContentResolver cr = context.getContentResolver();

        if (fileType == Constants.FILE_TYPE_PICTURES) {
            bmp = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MICRO_KIND, null);
        } else if (fileType == Constants.FILE_TYPE_VIDEOS) {
            bmp = Video.Thumbnails.getThumbnail(cr, id, Video.Thumbnails.MICRO_KIND, null);
        } else if (fileType == Constants.FILE_TYPE_AUDIO) {
            bmp = MusicUtils.getArtwork(context, id, -1, 2);
        } else if (fileType == Constants.FILE_TYPE_APPLICATIONS) {
            InputStream is = cr.openInputStream(
                    Uri.withAppendedPath(Applications.Media.CONTENT_URI_ITEM, String.valueOf(id)));
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        }
    } catch (Throwable e) {
        bmp = null;
        // ignore
    }

    return bmp;
}

From source file:util.ImageDownloader.java

Bitmap downloadBitmap(String url) {
    final int IO_BUFFER_SIZE = 4 * 1024;

    // AndroidHttpClient is not allowed to be used from the main thread
    //        final HttpClient client = (mode == Mode.NO_ASYNC_TASK) ? new DefaultHttpClient() :
    //            AndroidHttpClient.newInstance("Android");
    //        final HttpGet getRequest = new HttpGet(url);

    try {/*www .j av  a2s. c  o m*/
        return BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    //        try {
    //            HttpResponse response = client.execute(getRequest);
    //            final int statusCode = response.getStatusLine().getStatusCode();
    //            if (statusCode != HttpStatus.SC_OK) {
    //                Log.w("ImageDownloader", "Error " + statusCode +
    //                        " while retrieving bitmap from " + url);
    //                return null;
    //            }
    //            final HttpEntity entity = response.getEntity();
    //            if (entity != null) {
    //                InputStream inputStream = null;
    //                try {
    //                    inputStream = entity.getContent();
    // return BitmapFactory.decodeStream(inputStream);
    // Bug on slow connections, fixed in future release.
    //                    return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
    //                } finally {
    //                    if (inputStream != null) {
    //                        inputStream.close();
    //                    }
    //                    entity.consumeContent();
    //                }
    //            }
    //        } catch (IOException e) {
    //            getRequest.abort();
    //            Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    //        } catch (IllegalStateException e) {
    //            getRequest.abort();
    //            Log.w(LOG_TAG, "Incorrect URL: " + url);
    //        } catch (Exception e) {
    //            getRequest.abort();
    //            Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    //        } finally {
    //            if ((client instanceof AndroidHttpClient)) {
    //                ((AndroidHttpClient) client).close();
    //            }
    //        }
    return null;
}

From source file:com.sina.stock.SinaStockClient.java

private Bitmap getBitmapFromUrl(String url) throws HttpException, IOException {

    HttpMethod method = new GetMethod(url);
    int statusCode = mHttpClient.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        method.releaseConnection();/*from  w ww .  ja v  a  2s  . c om*/
        return null;
    }

    InputStream in = method.getResponseBodyAsStream();
    BufferedInputStream bis = new BufferedInputStream(in);

    Bitmap bm = BitmapFactory.decodeStream(bis);

    bis.close();
    method.releaseConnection();

    return bm;
}

From source file:com.softwaresemantics.diyglsllwp.ImageDownloader.java

Bitmap downloadBitmap(String url) {

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = AndroidHttpClient.newInstance("Android-Diyglsllwp");
    final HttpGet getRequest = new HttpGet(url);

    try {//  w w  w . j  a v a2  s .  co m
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                // return BitmapFactory.decodeStream(inputStream);
                // Bug on slow connections, fixed in future release.
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

From source file:net.sinoace.library.utils.ImageDownloader.java

Bitmap downloadBitmap(String url) {

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {/* ww w  . j  a va  2  s  .  c  o  m*/
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        Log.i(url, "");

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                // return BitmapFactory.decodeStream(inputStream);
                // Bug on slow connections, fixed in future release.
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

From source file:cn.org.eshow.framwork.util.AbFileUtil.java

/**
 * ???src?.//from   www .  j  a  v  a  2  s . c o m
 *
 * @param src srcimage/arrow.png?
 * @return Bitmap 
 */
public static Bitmap getBitmapFromSrc(String src) {
    Bitmap bit = null;
    try {
        bit = BitmapFactory.decodeStream(AbFileUtil.class.getResourceAsStream(src));
    } catch (Exception e) {
        AbLogUtil.d(AbFileUtil.class, "?" + e.getMessage());
    }
    return bit;
}

From source file:com.pbadun.fragment.adapter.ImageDownloader.java

Bitmap downloadBitmap(String url) {
    //final int IO_BUFFER_SIZE = 4 * 1024;

    //  ...  url//  w ww .  j  a va  2  s . c o  m
    String fName = Uri.encode(url);

    //  
    try {
        FileInputStream fi = context.openFileInput(fName);
        Log.d("log", "File is set!");

        //   -      
        Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(fi));
        return bitmap;
    } catch (FileNotFoundException e1) {
        //Log.d("bitmap",e1.toString());
        Log.d("log", "File not isset!");
    }

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = (mode == Mode.NO_ASYNC_TASK) ? new DefaultHttpClient()
            : AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();

                Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));

                //    ...
                FileOutputStream fo = null;
                try {
                    fo = context.openFileOutput(fName, Context.MODE_PRIVATE);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fo);
                    fo.flush();
                } catch (Exception e) {
                    //     ... :(
                    Log.d("log", "Error create file.");
                    //    ...
                    context.deleteFile(fName);
                } finally {
                    if (fo != null) {
                        //   
                        fo.close();
                    }
                }

                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}