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?
Est-ce que vous pourriez avoir des exceptions, pouvez-vous publier les journaux? –