com.codingrhemes.steamsalesmobile.HttpThumbnails.java Source code

Java tutorial

Introduction

Here is the source code for com.codingrhemes.steamsalesmobile.HttpThumbnails.java

Source

/*
Steam Sales Mobile - Android application to keep track of the steam sales.
    Copyright (C) 2013  Mathieu Rhaume <mathieu@codingrhemes.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

package com.codingrhemes.steamsalesmobile;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.InputStream;

// Class that downloads those pictures!!
public class HttpThumbnails {
    public static Bitmap readPictureFromTheWeb(String URL) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        Bitmap thePicture = null;

        try {

            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                thePicture = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("HttpThumbnails", e.getLocalizedMessage());
        }
        return thePicture;
    }
}