Comment puis-je faire cela en utilisant AsyncTask? Cela montre du contenu html dans un TextView. Je voudrais faire cela avec AsyncTask pour afficher un message « Chargement » parce que cette façon prenait trop de temps ...:AsyncTask ImageGetter
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_stire_detail,
container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
TextView noteView = ((TextView) rootView
.findViewById(R.id.stire_detail));
String EntireStire = "<b>" + mItem.title + " </b> <br> <img src='"
+ mItem.photo + "' > " + " <br><small>" + mItem.description
+ " <br> " + mItem.content + "</small>";
noteView.setText(Html.fromHtml(EntireStire, new MyImageGetter(),
null));
noteView.setMovementMethod(LinkMovementMethod.getInstance());
}
return rootView;
}
private class MyImageGetter implements Html.ImageGetter {
@Override
public Drawable getDrawable(String arg0) {
Bitmap bitmap;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
(InputStream) new URL(arg0).getContent(), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 200;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth/scale/2 >= REQUIRED_SIZE
&& o.outHeight/scale/2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
// HKEY_CURRENT_USER\Software\Google\Chrome\Metro
bitmap = BitmapFactory.decodeStream(
(InputStream) new URL(arg0).getContent(), null, o2);
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
}
}
J'ai utilisé le modèle de flux maître/détail d'Android.
J'ai proposé d'utiliser 'Future', mais votre approche est plus liée à Android. Cela semble bon! – mike
Je ne veux pas télécharger les images. Fondamentalement, j'ai utilisé Html.ImageGetter pour gérer tous les tags , donc j'ai implémenté la fonction getDrawable. Je voudrais l'avoir dans une classe AsyncTask. Mais je ne sais pas comment retourner ce dessinable ... :( Je veux dire, j'ai beaucoup de photos dans mItem.description + mItem.photo (la première image de l'article (quelques nouvelles)). –
et le ImageGetter téléchargez les images ;-) –