2016-09-09 2 views
1

Je veux définir des images d'Url dans un imageView. Pour tester j'ai pris quelques photos dans le dossier drawable.setImageDrawable ne fonctionne pas avec l'URL

//creat a list of images and put images inside 
    final int[] imageList = new int[]{R.drawable.ic_test_img_1, R.drawable.ic_test_img_2, R.drawable.ic_test_img_3, 
      R.drawable.ic_test_img_4, R.drawable.ic_test_img_5, R.drawable.ic_test_img_6}; 

    //for each picture into the list... 
    for (int i = 0; i < imageList.length; i++) { 

     //set RelativeLayout 
     final RelativeLayout relView = new RelativeLayout(this); 
     //set Params for the RelativeLayout 
     relView.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth - 80), windowHeight - 80)); 
     relView.setX(40); 
     relView.setY(40); 
     relView.setTag(i); 
     //set BackgroundColor RelativeLayout 
     relView.setBackgroundColor(Color.WHITE); 

     //set ImageView 
     ImageView img = new ImageView(this); 
     //set the image from the list into the imageView 
     img.setImageResource(imageList[i]); 
     //set params to RelativLayout 
     img.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth), windowHeight)); 
     //show image 
     relView.addView(img); 

Cela fonctionne très bien. Maintenant, je veux définir des images à partir d'une ArrayList d'URL. J'ai écrit ce code:

//for each picture into the list... 
    for (int i = 0; i < imageUrl.size(); i++) { 

     //set RelativeLayout 
     final RelativeLayout relView = new RelativeLayout(this); 
     //set Params for the RelativeLayout 
     relView.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth - 80), windowHeight - 80)); 
     relView.setX(40); 
     relView.setY(40); 
     relView.setTag(i); 
     //set BackgroundColor RelativeLayout 
     relView.setBackgroundColor(Color.WHITE); 

     //set ImageView 
     final ImageView img = new ImageView(this); 
     //set the image from the list into the imageView 

     final String url = imageUrl.get(i); 

     new Thread() { 

      public void run() { 
       URL myUrl = null; 
       try { 
        myUrl = new URL(url); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
       InputStream inputStream = null; 
       try { 
        inputStream = (InputStream)myUrl.getContent(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       Drawable drawable = Drawable.createFromStream(inputStream, null); 
       img.setImageDrawable(drawable); 
      } 
     }.start(); 

     //set params to RelativLayout 
     img.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth), windowHeight)); 
     //show image 
     relView.addView(img); 

Ce code n'a pas fonctionné. Il n'y a pas de photos dans l'imageView. Quel est le problème avec le code?

+0

Est-ce que vous pourriez avoir des exceptions, pouvez-vous publier les journaux? –

Répondre

0

Vous devez utiliser Glide ou toute autre bibliothèque de chargement d'image et vous épargner tout ce que les maux de tête

et il est aussi simple que cela

Glide.with(this).load("http://googl.com/gEgYUd").into(imageView); 
+0

devrais-je utiliser Glide dans un nouveau fil? – dudi

+0

no Glide crée son propre fil et met l'image en cache pour vous. Glide est génial – humazed

+0

Merci pour l'indice. J'ai utilisé final ImageView img = new ImageView (this); final Chaîne url = imageUrl.get (i); Glide.with (this) .load (url) .into (img); mais cela ne fonctionne toujours pas. – dudi

0

Vous devez utiliser la méthode runOnUiThread dans cette discussion, il mettra à jour imageview

this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Drawable drawable = Drawable.createFromStream(inputStream, null); 
       img.setImageDrawable(drawable); 
      } 
     });