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:eu.trentorise.smartcampus.portfolio.utils.NetUtility.java

public static Bitmap loadBitmapfromUrl(String imageUrl) {
    Bitmap resultImage = null;/*www  . j  av a 2 s. c o m*/
    HttpGet getRequest = new HttpGet();
    try {
        URI imageURI = new URI(imageUrl);
        getRequest.setURI(imageURI);
        HttpClient httpClient = HttpClientFactory.INSTANCE.getThreadSafeHttpClient();
        HttpResponse response = httpClient.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w(NetUtility.class.getName(), "Error: " + statusCode + " image url: " + imageUrl);
        } else {
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = new FlushedInputStream(entity.getContent());
                    resultImage = BitmapFactory.decodeStream(inputStream);
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        }
    } catch (Exception e) {
        getRequest.abort();
        e.printStackTrace();
        Log.w(NetUtility.class.getName(), "Error image url: " + imageUrl);
    }
    return resultImage;
}

From source file:jp.co.ipublishing.esnavi.dependence.shelter.ShelterImageFactory.java

@NonNull
@Override//from   w w w.ja  va2 s.c  o  m
public Bitmap getPhoto(@NonNull Context context, int shelterId) {
    try {
        final InputStream is = context.getResources().getAssets()
                .open(String.format("shelter_images/%d.jpg", shelterId));
        final Bitmap bmp = BitmapFactory.decodeStream(is);
        is.close();
        return bmp;
    } catch (IOException e) {
        Log.e(TAG, ExceptionUtils.getStackTrace(e));
    }

    return getNoImageBitmap(context);
}

From source file:de.incoherent.suseconferenceclient.app.HTTPWrapper.java

public static Bitmap getImage(String url) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    Log.d("SUSEConferences", "Get: " + url);
    HttpResponse response = client.execute(get);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode >= 200 && statusCode <= 299) {
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }//w ww  .  ja v a  2s  .co m
                entity.consumeContent();
            }
        } else {
            throw new HttpResponseException(statusCode, statusLine.getReasonPhrase());
        }
    } else {
        throw new HttpResponseException(statusCode, statusLine.getReasonPhrase());
    }
}

From source file:org.quizpoll.net.ImageDownloadHelper.java

@Override
public void parseResponse(HttpResponse response) {
    Bitmap bitmap;//from w  w w .j av a2s  .  c  om
    try {
        bitmap = BitmapFactory.decodeStream(response.getEntity().getContent());
        success(bitmap);
    } catch (IllegalStateException e) {
        // Ignore
    } catch (IOException e) {
        // Ignore
    }
}

From source file:Main.java

/**
 * Read bytes./*  ww w  .  j av  a 2 s  .  c  o m*/
 *
 * @param uri      the uri
 * @param resolver the resolver
 * @return the byte[]
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static byte[] readBytes(Uri uri, ContentResolver resolver, boolean thumbnail) throws IOException {
    // this dynamically extends to take the bytes you read
    InputStream inputStream = resolver.openInputStream(uri);
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

    if (!thumbnail) {
        // this is storage overwritten on each iteration with bytes
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the
        // byteBuffer
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
    } else {
        Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
        int thumb_width = imageBitmap.getWidth() / 2;
        int thumb_height = imageBitmap.getHeight() / 2;
        if (thumb_width > THUMBNAIL_SIZE) {
            thumb_width = THUMBNAIL_SIZE;
        }
        if (thumb_width == THUMBNAIL_SIZE) {
            thumb_height = ((imageBitmap.getHeight() / 2) * THUMBNAIL_SIZE) / (imageBitmap.getWidth() / 2);
        }
        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, thumb_width, thumb_height, false);
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteBuffer);
    }
    // and then we can return your byte array.
    return byteBuffer.toByteArray();
}

From source file:com.huguesjohnson.retroleague.util.HttpFetch.java

public static Bitmap fetchBitmap(String imageAddress, int timeout) throws MalformedURLException, IOException {
    return (BitmapFactory.decodeStream(fetch(imageAddress, timeout)));
}

From source file:com.richtodd.android.repository.FileRepositoryObjectProvider.java

@Override
public Bitmap getThumbnail() throws RepositoryException {

    try {/*w w w .  ja v  a2 s  . c o m*/
        if (m_thumbnail != null) {
            return m_thumbnail;
        }

        if (m_fileThumbnail != null) {
            Bitmap bitmap;

            FileInputStream in = new FileInputStream(m_fileThumbnail);
            try {
                bitmap = BitmapFactory.decodeStream(in);
            } finally {
                in.close();
            }

            return bitmap;
        }

        if (m_thumbnailProvider != null) {
            m_thumbnail = m_thumbnailProvider.getThumbnail(getObject());
            return m_thumbnail;
        }

        return null;
    } catch (FileNotFoundException ex) {
        throw new RepositoryException(ex);
    } catch (IOException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:com.intellisol.plugin.Wallpaper.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {/*from   w  w  w  . j  av a2s  .c om*/
        // get path from argument
        String path = args.getString(0);

        // get context (Android)
        Context ctxt = cordova.getActivity().getBaseContext();
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctxt);

        // get image file
        InputStream bitmapIn = cordova.getActivity().getAssets().open(path);
        Bitmap bitmap = BitmapFactory.decodeStream(bitmapIn);

        // set wallpaper
        wallpaperManager.setBitmap(bitmap);

    } catch (JSONException e) {
        // log error
        Log.d("Wallpaper", e.toString());
        return false;
    } catch (Exception e) {
        // log error
        Log.d("Wallpaper", e.toString());
        return false;
    }
    return true;
}

From source file:com.liferay.social.task.PortraitAsyncTask.java

protected Bitmap doInBackground(Void... params) {
    InputStream is = null;//from w  w  w.j  a  v a 2s. c om
    Bitmap bitmap = null;

    try {
        HttpClient client = HttpUtil.getClient(_session);

        String portraitURL = PortraitUtil.getPortraitURL(_session, true, _user.getPortraitId(),
                _user.getUuid());

        HttpGet get = new HttpGet(portraitURL);

        HttpResponse response = client.execute(get);
        is = response.getEntity().getContent();

        bitmap = BitmapFactory.decodeStream(is);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    return bitmap;
}

From source file:ch.pec0ra.mobilityratecalculator.DistanceCalculator.java

public Itinerary calculate() {
    if (!checkConnection()) {
        return new Itinerary(context.getString(R.string.connexion_error));
    }//from   w w  w  .  j a  v  a  2s. c o  m

    try {
        URL url = new URL(buildImageUrl());
        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        String ret = downloadUrl(buildDistancesUrl());
        return new Itinerary(new JSONObject(ret), isTwoWay, bmp);
    } catch (Exception e) {
        e.printStackTrace();
        return new Itinerary(context.getString(R.string.connexion_error));
    }
}