1

My code is :

public class MainActivity extends ActionBarActivity  {

    String URL1 = "http://cs619925.vk.me/v619925510/1b82b/0nTm-Pj0ABM.jpg";
    String URL2 = "http://8tracks.imgix.net/i/000/955/740/87318.original-8382.jpg?rect=128,0,768,768&q=65&sharp=15&vib=10&fm=jpg&fit=max&w=200&h=200";
    String URL3 = "http://a400.idata.over-blog.com/300x225/1/89/70/64/Autres-images/smiley-face-on-beach.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView myFirstImage = (ImageView) findViewById(R.id.iv1);
        ImageView mySecondImage = (ImageView) findViewById(R.id.iv2);
        ImageView myThirdImage = (ImageView) findViewById(R.id.iv3);        


        myFirstImage.setTag(URL1);
        mySecondImage.setTag(URL2);
        myThirdImage.setTag(URL3);


        new DownloadImagesTask().execute(myFirstImage);
        new DownloadImagesTask().execute(mySecondImage);
        new DownloadImagesTask().execute(myThirdImage);

    }

        public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

            ImageView imageView = null;

            protected Bitmap doInBackground(ImageView... imageViews) {
                this.imageView = imageViews[0];
                return download_Image((String)imageView.getTag());
            }

            protected void onPostExecute(Bitmap result) {
                imageView.setImageBitmap(result);
            }

            private Bitmap download_Image(String url) {

                Bitmap bmp =null;
                try{
                    URL ulrn = new URL(url);
                    HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
                    InputStream is = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(is);
                    if (null != bmp)
                        return bmp;

                    }catch(Exception e){}
                return bmp;
            }
        } 



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

On running it on the emulator, I don't get images displayed. Help me with this one.And I don't even know what the eroor is? if there is any.

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
Garry
  • 29
  • 1
  • 8
  • Try using breakpoints and debug your code to get a better understanding of what went wrong. – shyam Mar 02 '15 at 06:56
  • I checked the log. It says "called unimplemented opengl es api" – Garry Mar 02 '15 at 07:00
  • @Garry Emulator not support opengl check here ... http://stackoverflow.com/questions/5926316/android-gles20-called-unimplemented-opengl-es-api – Sabeer Mar 02 '15 at 07:18
  • Can you please put logs for exception that you handled in aSyncTask.. There you may get something interesting to resolve your issue. – Rushabh Patel Mar 02 '15 at 07:47
  • I tried your code, it works fine for me on Emulator, images are taking time to download , may be because of internet connectivity. U can try using loader until image loads. – Swats Mar 02 '15 at 09:30
  • @Swats now it is working for me too :) I don't know why it wasn't working earlier. – Garry Mar 02 '15 at 09:49

2 Answers2

0

if you want download file or if you want load image into imageview, then use picasso:

Picasso.with(context).load(URL1).into(myFirstImage);

public class MainActivity extends ActionBarActivity  {

String URL1 = "http://cs619925.vk.me/v619925510/1b82b/0nTm-Pj0ABM.jpg";
String URL2 = "http://8tracks.imgix.net/i/000/955/740/87318.original-8382.jpg?rect=128,0,768,768&q=65&sharp=15&vib=10&fm=jpg&fit=max&w=200&h=200";
String URL3 = "http://a400.idata.over-blog.com/300x225/1/89/70/64/Autres-images/smiley-face-on-beach.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView myFirstImage = (ImageView) findViewById(R.id.iv1);
    ImageView mySecondImage = (ImageView) findViewById(R.id.iv2);
    ImageView myThirdImage = (ImageView) findViewById(R.id.iv3);        

     Picasso.with(this).load(URL1).into(myFirstImage);
     Picasso.with(this).load(URL2).into(mySecondImage);
     Picasso.with(this).load(URL3).into(myThirdImage);

   }
 }   
Community
  • 1
  • 1
gadfil
  • 417
  • 4
  • 13
0

You can easily download image using Picasso and Glide library.

shweta c
  • 107
  • 2
  • 12