Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

retrieve image using android

How to show the first image on a webpage ?

1 Answer

Hey Tim!

You can go ahead and use this code I put together for you to get an image from a website:

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {

                try {
                    URL url = new URL("https://instagramimages-a.akamaihd.net/profiles/profile_625618423_75sq_1382557512.jpg");
                    InputStream inputStream = url.openStream();
                    mImage = BitmapFactory.decodeStream(inputStream);
                }
                catch(Exception e) {
                    Looper.prepare();
                    Log.e("MainActivity", e.getMessage());
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void Avoid) {
                if (mImage != null) {
                    ImageView imageView = (ImageView) findViewById(R.id.imageView);
                    imageView.setImageBitmap(mImage);
                }
                else {
                    Toast.makeText(MainActivity.this, "Invalid image", Toast.LENGTH_LONG).show();
                }
            }
        }.execute();

Here, we're getting the Treehouse logo from their Instagram page. Update the url variable to your own image URL for your image and you're good to go!

If you have no idea what's going on here, a few of the courses use similar things to this, such as the newer Build a Weather App course (Where we get JSON data from the web) or the older Implementing Designs for Android Course (Where we get images from Gravatar and display them in our app) which should explain what is going on here.


If you want me to go through something here, give me a shout and I'll try my best to help :)

Um no i had one other question posted if you could help me out it was that how to find out just city name and country with location services i didnt find Ben's article too helpfull if you cou;d make that simpler

Hey Tim!

I haven't had any personal experience of using GPS systems in Android as of yet but highly recommend you take a look at this StackOverflow page for details on how you can translate GPS coordinates into an address :)