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:nya.miku.wishmaster.http.recaptcha.Recaptcha.java

/**
 *   /* w w w.  j av  a  2s .  co m*/
 * @param publicKey  
 * @param task ,   
 * @param httpClient , ? ? ? ?
 * @param scheme  (http  https), ? null,   "http"
 * @return  
 */
public static Recaptcha obtain(String publicKey, CancellableTask task, HttpClient httpClient, String scheme)
        throws RecaptchaException {
    Exception lastException = null;
    if (scheme == null)
        scheme = "http";
    Recaptcha recaptcha = new Recaptcha();
    for (ChallengeGetter getter : GETTERS) {
        try {
            recaptcha.challenge = getter.get(publicKey, task, httpClient, scheme);
            HttpResponseModel responseModel = null;
            try {
                responseModel = HttpStreamer.getInstance().getFromUrl(
                        scheme + RECAPTCHA_IMAGE_URL + recaptcha.challenge,
                        HttpRequestModel.builder().setGET().build(), httpClient, null, task);
                InputStream imageStream = responseModel.stream;
                recaptcha.bitmap = BitmapFactory.decodeStream(imageStream);
            } finally {
                if (responseModel != null)
                    responseModel.release();
            }
            if (recaptcha.bitmap != null)
                return recaptcha;
        } catch (Exception e) {
            lastException = e;
        }
    }
    if (lastException != null) {
        if (lastException instanceof RecaptchaException) {
            throw (RecaptchaException) lastException;
        } else {
            throw new RecaptchaException(lastException);
        }
    } else {
        throw new RecaptchaException("Can't get recaptcha");
    }
}

From source file:com.example.placephotos.PhotoService.java

/**
 *
 * @param r Request object which contain the query parameters/
 * @return A Bitmap as returned by the Places Photos API.
 *//* w w w.ja  v a2 s.c om*/
public Bitmap getPhoto(Request r) {
    HttpResponse response = executeRequest(r);
    InputStream content;
    try {
        content = response.getEntity().getContent();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return BitmapFactory.decodeStream(content);
}

From source file:com.intel.xdk.base.Base.java

private Bitmap getBitmapFromAsset(String strName) throws IOException {
    AssetManager assetManager = cordova.getActivity().getAssets();
    InputStream istr = assetManager.open(strName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}

From source file:com.kku.apps.pricesearch.util.HttpConnection.java

public static Bitmap getBitmapFromUrl(String url) {

    HttpGet method = new HttpGet(url);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {/* ww w.jav a2 s . com*/
        BufferedHttpEntity entity = httpClient.execute(method, new ResponseHandler<BufferedHttpEntity>() {

            @Override
            public BufferedHttpEntity handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {

                // Xe?[^XR?[h
                int status = response.getStatusLine().getStatusCode();

                if (HttpStatus.SC_OK != status) {
                    throw new RuntimeException("?MG?[?");
                }
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufferEntity = new BufferedHttpEntity(entity);
                return bufferEntity;
            }
        });
        InputStream is = entity.getContent();
        return BitmapFactory.decodeStream(is);
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.thomaskuenneth.openweathermapweather.MainActivity.java

private Bitmap getImage(WeatherData w) throws IOException {
    URL req = new URL("http://openweathermap.org/img/w/" + w.icon + ".png");
    return BitmapFactory.decodeStream(req.openConnection().getInputStream());
}

From source file:mc.lib.network.NetworkHelper.java

public static Bitmap getBitmap(String url) {
    Bitmap res = null;/*w ww.j  a va2  s .  co  m*/
    InputStream is = null;
    try {
        URLConnection c = new URL(url).openConnection();
        c.connect();
        is = c.getInputStream();
        res = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        Log.e(LOGTAG, "Wrong url format", e);
    } catch (Exception e) {
        Log.e(LOGTAG, "Cannot get bitmap", e);
    } finally {
        StreamHelper.close(is);
    }
    return res;
}

From source file:com.fanfou.app.opensource.http.SimpleClient.java

public final Bitmap getBitmap(final String url) throws IOException {
    final HttpResponse response = get(url);
    final int statusCode = response.getStatusLine().getStatusCode();
    if (AppContext.DEBUG) {
        Log.d(SimpleClient.TAG, "getBitmap() statusCode=" + statusCode + " [" + url + "]");
    }//w w  w  .ja v  a  2s .  co m
    if (statusCode == 200) {
        return BitmapFactory.decodeStream(response.getEntity().getContent());
    }
    return null;
}

From source file:nya.miku.wishmaster.http.recaptcha.Recaptcha2.java

/**
 *   //from  www  .ja  v  a 2s  .  co m
 * @param publicKey  
 * @param task ,   
 * @param httpClient , ? ? ? ?
 * @param scheme  (http  https), ? null,   "http"
 * @return  
 */
public static Recaptcha2 obtain(String publicKey, CancellableTask task, HttpClient httpClient, String scheme)
        throws RecaptchaException {
    try {
        if (scheme == null)
            scheme = "http";
        Recaptcha2 recaptcha = new Recaptcha2();
        recaptcha.httpClient = httpClient;
        recaptcha.scheme = scheme;
        recaptcha.publicKey = publicKey;
        String response = HttpStreamer.getInstance().getStringFromUrl(
                scheme + RECAPTCHA_FALLBACK_URL + publicKey, HttpRequestModel.builder().setGET().build(),
                httpClient, null, task, false);
        Matcher matcher = Pattern.compile("name=\"c\" value=\"([\\w-]+)").matcher(response);
        if (matcher.find()) {
            recaptcha.challenge = matcher.group(1);
            HttpResponseModel responseModel = HttpStreamer.getInstance().getFromUrl(
                    scheme + RECAPTCHA_IMAGE_URL + recaptcha.challenge + "&k=" + publicKey,
                    HttpRequestModel.builder().setGET().build(), httpClient, null, task);
            try {
                InputStream imageStream = responseModel.stream;
                recaptcha.bitmap = BitmapFactory.decodeStream(imageStream);
            } finally {
                responseModel.release();
            }
            return recaptcha;
        } else
            throw new RecaptchaException("can't parse recaptcha challenge answer");
    } catch (Exception e) {
        if (e instanceof RecaptchaException) {
            throw (RecaptchaException) e;
        } else {
            throw new RecaptchaException(e);
        }
    }
}

From source file:es.eucm.eadandroid.homeapp.repository.resourceHandler.RepoResourceHandler.java

/**
 * Downloads an image as a Bitmap/*from w  ww.j  av  a 2s . c  om*/
 */
public static Bitmap DownloadImage(String url_from, ProgressNotifier pn) {

    int last = url_from.lastIndexOf("/");
    String fileName = url_from.substring(last + 1);

    pn.notifyIndeterminate("Downloading " + fileName);
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = getInputStreamFromUrl(url_from);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        pn.notifyError("Repository connection error");
    }

    return bitmap;
}

From source file:org.imaginationforpeople.android2.model.Picture.java

public Bitmap getImageBitmap() {
    if (imageBitmap == null) {
        String path;// ww  w  . j  av  a 2s.  c o  m
        try {
            path = new URL(imageUrl).getPath();
            FileInputStream file = DataHelper.openFileInput(
                    DataHelper.FILE_PREFIX_PROJECT_IMAGE + path.substring(path.lastIndexOf('/') + 1));
            imageBitmap = BitmapFactory.decodeStream(file);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }
    return imageBitmap;
}