0

I would like to use HTTP post and get methods to retrieve images for a gallery view. How would i go about doing this?

YogoTi
  • 91
  • 1
  • 6

1 Answers1

1

Do a search here for how to use HttpGet - there are lots of examples. But once you have a valid response you will need to use BitmapFactory.decodeStream to get a bitmap (something like the example code below) which you can draw into the ImageView.

HttpResponse response = HttpClient.execute(request);
HttpEntity entity     = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statusCode        = statusLine.getStatusCode();

if (statusCode == HttpStatus.SC_OK)
{
    InputStream inputStream = entity.getContent();
    Bitmap rawBitmap = null;

    BitmapFactory.Options bitmapOpt = new BitmapFactory.Options();
    bitmapOpt.inDither          = true;
    bitmapOpt.inPurgeable       = true;
    bitmapOpt.inInputShareable  = true;
    bitmapOpt.inTempStorage     = null;
    rawBitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOpt);
}
Torid
  • 4,176
  • 1
  • 28
  • 29
  • Thanks! What im trying to do is upload fresh images to a application every week. do you think there may be a better way of doing this? – YogoTi Jul 19 '11 at 20:51
  • There's also a method using BufferedInputStream and BitmapFactory.decodeByteArray described at http://stackoverflow.com/questions/2183808/android-bitmapfactory-decodebytearray-gives-pixelated-bitmap. But I'm not sure what you mean by "upload fresh images to a application every week"? Which way are the images flowing? – Torid Jul 19 '11 at 21:21
  • So say this week there is a picture of.... a tree for example... Next week i want this image to change to a flower. I just want to be able to change these images weekly. Without upgrading the WHOLE application for URL changes of images.\ – YogoTi Jul 19 '11 at 21:24
  • So yes... The best way to do that would be to check a web site periodically (weekly) and then use one of these approaches. – Torid Jul 19 '11 at 21:41
  • So your saying i would have to upgrade the application weekly? – YogoTi Jul 19 '11 at 21:51
  • No... The app doesn't change (need re-installation). Just have your app connect to the website weekly and download the new image... – Torid Jul 19 '11 at 22:03
  • How would i go about doing this??? Do you know of a article that will help me out? Or maybe you could. Thanks – YogoTi Jul 19 '11 at 22:04