Mithrand1r
Mithrand1r

Reputation: 2353

Android SDK Obtain image from HTTP get

I have a problem downloading and putting in ImageView image that is downloaded from ULR, normally it would be easy using

imgView.setImageDrawable(grabImageFromUrl(url));

But in my case i need to use HttpClient written by me to check certificate. I guess i have to use HTTPGEt, i have used it before taking in response Json strings, but i just can't find a way to grab a picture.

Do anyone have expirience in such operation?

Upvotes: 2

Views: 4045

Answers (3)

Mithrand1r
Mithrand1r

Reputation: 2353

Actually i have found out solution for my problem, I will post it here if anyone of users ever came across similiar problem:

Bitmap bmp =null;
        DefaultHttpClient client = new MyHttpClient(ShowNotification.this);
        HttpGet get = new HttpGet(url);
        HttpResponse getResponse = null;
        try {
            getResponse = client.execute(get);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String entityContents="";
        HttpEntity responseEntity = getResponse.getEntity();
        BufferedHttpEntity httpEntity = null;
        try {
            httpEntity = new BufferedHttpEntity(responseEntity);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        InputStream imageStream = null;
        try {
            imageStream = httpEntity.getContent();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bmp = BitmapFactory.decodeStream(imageStream);

        ImageView imgView =(ImageView)findViewById(R.id.imgView);
        imgView.setImageBitmap(bmp);

Upvotes: 3

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

You just need to pass the image URL to your method: grabImageFromUrl(url). may be there is something wrong in it. Can you please show us its content?

This simple tutorial can help you solving your issue http://www.vogella.com/articles/AndroidNetworking/article.html

Upvotes: 0

shkschneider
shkschneider

Reputation: 18243

I recommend you this excellent tiny library project:

Android-Universal-Image-Loader

It allows async loading.

Upvotes: 0

Related Questions