depuis le début i utilisé cette méthode:drawable d'une url avec nid d'abeille
public Drawable createPortrait(String url){
try {
InputStream is = (InputStream)new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "Image");
return d;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
mais nid d'abeille ne me permet pas de le faire plus, ce que je vois dans mon journal est: android.os.networkonmainthreadexception. la chose est que mon URL est déjà pris à partir des données JSON:
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(Main.this);
protected void onPreExecute() {
Dialog.setMessage("Downloading source..");
Dialog.show();
}
protected Void doInBackground(String... urls) {
try {
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
if (Error != null) {
Toast.makeText(Main.this, Error, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Main.this, "Source: " + Content, Toast.LENGTH_LONG).show();
}
Object o = new Gson().fromJson(Content, Info.class);
Info i = (Info)o;
String d = i.getData().get(0).getLg_portrait();
portrait.setBackgroundDrawable(createPortrait(d));
}
}
et portrait est un ImageView. je ne sais pas quoi faire.
dans un code quel serait-il? Je ne comprends pas ce que vous entendez par "espace réservé". – Tsunaze
placeholder image (sinon vous affichez l'espace vide) - il peut s'agir d'un indicateur de progression animé. Vous devez déplacer createPortrait (d) de onPostExecute() (thread UI) à doInBackground() (thread d'arrière-plan). –
ok ça marche mais j'ai du créer une autre classe privée comme graburl; – Tsunaze