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

@Nullable
public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding, @Nullable Options opts) 

Source Link

Document

Decode an input stream into a bitmap.

Usage

From source file:com.wisc.cs407project.ImageLoader.ImageLoader.java

public static Bitmap decodeFile(File f) {
    try {//  w  ww. j  a  v a2s .co  m
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        //Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_WIDTH && height_tmp / 2 < REQUIRED_HEIGHT)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.google.android.panoramio.BitmapUtilsTask.java

/**
 * Loads a bitmap from the specified url.
 * //from  w w  w . j a v a  2  s  . c  o m
 * @param url The location of the bitmap asset
 * @return The bitmap, or null if it could not be loaded
 */
public Bitmap loadThumbnail(String string) {
    Bitmap bitmap = null;

    InputStream is;
    try {
        is = (InputStream) new URL(string).getContent();
        BitmapFactory.Options optsDownSample = new BitmapFactory.Options();
        bitmap = BitmapFactory.decodeStream(is, null, optsDownSample);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:com.binroot.fatpita.BitmapManager2.java

/**
 * http://ballardhack.wordpress.com/2010/04/10/loading-images-over-http-on-a-separate-thread-on-android/
 * Convenience method to retrieve a bitmap image from
 * a URL over the network. The built-in methods do
 * not seem to work, as they return a FileNotFound
 * exception.//from w ww.  j  a  v  a2  s .c o m
 *
 * Note that this does not perform any threading --
 * it blocks the call while retrieving the data.
 *
 * @param url The URL to read the bitmap from.
 * @return A Bitmap image or null if an error occurs.
 */
public Bitmap readBitmapFromNetwork(String url, int sample) {
    InputStream is = null;
    BufferedInputStream bis = null;
    Bitmap bmp = null;
    try {
        is = fetch(url);
        bis = new BufferedInputStream(is);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = sample;
        bmp = BitmapFactory.decodeStream(bis, null, options);

    } catch (MalformedURLException e) {
        Log.e(TAG, "Bad ad URL", e);
    } catch (IOException e) {
        Log.e(TAG, "Could not get remote ad image", e);
    } finally {
        try {
            if (is != null)
                is.close();
            if (bis != null)
                bis.close();
        } catch (IOException e) {
            Log.w(TAG, "Error closing stream.");
        }
    }

    return bmp;
}

From source file:com.cooliris.media.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;/*from  ww  w .  jav a 2 s. c o  m*/
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }
    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        return bitmap;
    }
    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if (bitmap != null) {
        bitmap = Utils.resizeBitmap(bitmap, maxResolutionX);
    }
    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        writeToCache(crc64, bitmap, maxResolutionX);
    }
    return bitmap;
}

From source file:forplay.android.AndroidAssetManager.java

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

    try {/*w ww . j  a  va2  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;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inDither = true;
                options.inPreferredConfig = AndroidPlatform.instance.preferredBitmapConfig;
                options.inScaled = false;
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
                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();
        Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}

From source file:com.xlauncher.media.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;/* w  w  w . j av a  2s.  c  o m*/
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }
    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        return bitmap;
    }
    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if (bitmap != null) {
        bitmap = Utils.resizeBitmap(context, bitmap, maxResolutionX, false);
    }
    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        writeToCache(crc64, bitmap, maxResolutionX);
    }
    return bitmap;
}

From source file:com.timtory.wmgallery.media.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;//from   w  ww.  j  a  v  a 2  s.  c o m
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }
    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        return bitmap;
    }
    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if (bitmap != null) {
        bitmap = Utils.resizeBitmap(context, bitmap, maxResolutionX);
    }
    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        writeToCache(crc64, bitmap, maxResolutionX);
    }
    return bitmap;
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

@SuppressWarnings("deprecation")
public static Bitmap decodeBitmap(InputStream bitmap) {
    final Options options = new Options();
    options.inScaled = false;//from w  w w  .j a v  a 2 s .  co  m
    options.inDither = true;
    options.inPreferQualityOverSpeed = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    return BitmapFactory.decodeStream(bitmap, null, options);
}

From source file:org.exoplatform.utils.image.SocialImageLoader.java

private Bitmap decodeFile(File f) {
    try {//from  www .  j av a 2s.  c  om
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        return null;
    }

}

From source file:com.cooliris.mediayemaha.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;//www.ja v  a 2 s  .  co  m
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }
    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        return bitmap;
    }
    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        writeToCache(crc64, bitmap, maxResolutionX / options.inSampleSize);
    }
    return bitmap;
}