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.photocitygame.android.ImageActivity.java

/** Called when the activity is first created. */
@Override//ww w . j  av a 2  s.c  om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image);
    imageView = (ImageView) findViewById(R.id.imageview);
    ImageButton capture = (ImageButton) findViewById(R.id.capture);
    capture.setImageResource(android.R.drawable.ic_menu_camera);
    capture.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            doCapture();
        }
    });
    String url;
    flag = (ParcelableFlag) getIntent().getExtras().getParcelable(PhotoCity.FLAG);
    model = (Model) getIntent().getExtras().getParcelable(PhotoCity.MODEL);
    zone = (Zone) getIntent().getExtras().getParcelable(PhotoCity.ZONE);
    HttpClient client = new DefaultHttpClient();

    TableLayout scoreTable = (TableLayout) findViewById(R.id.score_table);

    if (flag != null) {
        if (zone != null) {
            model = zone.getModelById(flag.getModelId());
        }
        url = PhotoCity.api().getFlagImageUrl(flag);
        for (Entry<String, Integer> entry : flag.getScores()) {
            addScoreRow(scoreTable, entry.getKey(), entry.getValue());
        }
    } else {
        url = PhotoCity.api().getModelImageUrl(model);
    }
    if (model != null) {
        setTitle(model.getName());
    } else {
        setTitle("Details");
    }
    HttpGet get = new HttpGet(url);
    try {
        HttpResponse response = client.execute(get);
        Bitmap bm = BitmapFactory.decodeStream(response.getEntity().getContent());
        imageView.setImageBitmap(bm);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:libraryjava.parseJSON.java

/**
 * method Bitmap deprecated/*  www  .  ja va2s.co m*/
 * @param urll
 * @return
 */
public Bitmap doInBackgroundDeprecated(String urll) {
    StrictMode();

    try {
        URL url = new URL(urll);

        HttpGet httpRequest = new HttpGet(url.toURI());
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
        InputStream input = bufferedEntity.getContent();

        return BitmapFactory.decodeStream(input);

    } catch (Exception ex) {
        return null;
    }
}

From source file:es.pentalo.apps.RBPiCameraControl.API.RBPiCamera.java

synchronized public Bitmap shotPhoto(List<Command> commands) {
    HttpEntity entity = getEntityPost(mBaseUrl + "api/photo/shot/", commands);

    if (entity != null) {
        InputStream inputStream;/*from   ww  w.j  av a  2s  . c o m*/
        try {
            inputStream = entity.getContent();
            return BitmapFactory.decodeStream(inputStream);

        } catch (IllegalStateException e) {
            Log.e(TAG, e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
    }

    return null;
}

From source file:com.dii.ids.application.utils.io.SimpleDiskCache.java

public BitmapEntry getBitmap(String key) throws IOException {
    DiskLruCache.Snapshot snapshot = diskLruCache.get(toInternalKey(key));
    if (snapshot == null)
        return null;

    try {/*www .ja v a2 s.c  o m*/
        Bitmap bitmap = BitmapFactory.decodeStream(snapshot.getInputStream(VALUE_IDX));
        return new BitmapEntry(bitmap, readMetadata(snapshot));
    } finally {
        snapshot.close();
    }
}

From source file:cn.xiongyihui.wificar.MjpegStream.java

public Bitmap readFrame(DataInputStream in) throws IOException {
    int mContentLength = -1;

    in.mark(FRAME_MAX_LENGTH);//from www . j  a  v  a2s.  c om
    int headerLen = getStartOfSequence(in, SOI_MARKER);
    in.reset();
    byte[] header = new byte[headerLen];
    in.readFully(header);
    try {
        mContentLength = parseContentLength(header);
    } catch (NumberFormatException nfe) {
        mContentLength = getEndOfSeqeunce(in, EOF_MARKER);
    }
    in.reset();
    byte[] frameData = new byte[mContentLength];
    in.skipBytes(headerLen);
    in.readFully(frameData);
    return BitmapFactory.decodeStream(new ByteArrayInputStream(frameData));
}

From source file:ch.dbrgn.android.simplerepost.services.FileDownloadService.java

/**
 * Function that downloads the url and returns a Bitmap instance.
 *
 * If an error occurs, a DownloadErrorEvent is posted into the
 * bus and null is returned./*from   w w w  .j a  va 2s. c  o m*/
 *
 * You should probably run this code in a background thread!
 */
private ImageBitmap downloadBitmap(String url) {
    final DefaultHttpClient client = new DefaultHttpClient();
    Bitmap bitmap = null;

    // Parse filename out of URL
    final String[] parts = url.split("/");
    final String filename = parts[parts.length - 1];

    final HttpGet getRequest = new HttpGet(url);
    try {
        // Do the request
        HttpResponse response = client.execute(getRequest);

        // Check status code
        final int statusCode = response.getStatusLine().getStatusCode();

        // Handle error case
        if (statusCode != HttpStatus.SC_OK) {
            final String message = "Error " + statusCode + " while retrieving bitmap from " + url;
            Log.w(LOG_TAG, message);
            mBus.post(new DownloadErrorEvent(message));
            return null;
        }

        // Get the data
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                // Get contents from the stream
                inputStream = entity.getContent();

                // Decode stream data back into image Bitmap that android understands
                bitmap = BitmapFactory.decodeStream(inputStream);
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // You Could provide a more explicit error message for IOException
        getRequest.abort();
        final String message = "Something went wrong while retrieving bitmap from " + url + e.toString();
        Log.e(LOG_TAG, message);
        mBus.post(new DownloadErrorEvent(message));
    }

    return new ImageBitmap(bitmap, filename);
}

From source file:com.gh4a.utils.ImageDownloader.java

/**
 * Download bitmap.//from  w  w  w .  j  av  a  2  s  .  com
 * 
 * @param urlStr the url str
 * @return the bitmap
 */
static Bitmap downloadBitmap(String urlStr) {
    InputStream is = null;
    try {
        URL url = new URL(urlStr);
        URLConnection conn = url.openConnection();
        conn.connect();
        is = conn.getInputStream();
        // bis = new BufferedInputStream(is);
        final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(is));
        return bitmap;
    } catch (Exception e) {
        Log.e(Constants.LOG_TAG, e.getMessage(), e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(Constants.LOG_TAG, e.getMessage(), e);
            }
        }
    }
    return null;
}

From source file:es.curso.android.GooglePlaces.Api.java

public Bitmap doGetImagePetition(String url) {

    try {/*w  ww . ja v a2 s.  c  o  m*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = null;

        httpGet = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpGet);

        InputStream inputStream;

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            inputStream = response.getEntity().getContent();
            return BitmapFactory.decodeStream(inputStream);
        }

        return null;

    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        return null;
    }
}

From source file:com.rabross.android.minecraftskinwidget.ImageDownloader.java

Bitmap downloadBitmap(String name, String aUrl) {

    String url = aUrl + name;/*  ww w.ja  v  a 2s  .co  m*/

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = 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();
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(TAG, "I/O error while retrieving bitmap", e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(TAG, "Incorrect URL");
    } catch (Exception e) {
        getRequest.abort();
        Log.w(TAG, "Error while retrieving bitmap", e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

From source file:es.warp.killthedj.server.ServerRequest.java

public Bitmap getCurrentCover() throws ServerRequestException {
    try {/*  w  w  w .j a  v  a 2  s.  c o m*/
        URL coverUrl = new URL(RemoteServers.currentTrackCoverURL(partyId));
        return BitmapFactory.decodeStream(coverUrl.openConnection().getInputStream());
    } catch (IOException e) {
        throw new ServerRequestException(e.getMessage(), e);
    }
}