2

How can I do this using AsyncTask? This shows some html content in a TextView. I would like to make this with AsyncTask to display a "Loading" message because this way took too much time... :

@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;
        }
    }
}

I have used the Master/Detail Flow template from Android.

Cosmin Rusu
  • 37
  • 1
  • 8

4 Answers4

1

in your onCreate, you need to set the set to "loading.." or something, and start the task:

DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.myUrl.com" });

And then:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      //Download your pictures and prepare the text
      return response;
    }

    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
      //Better:
      ImageView.setbackground(....)
    }
  }

} 

Extra note

I would personally use an ImageView for the picture! textView is not optimal for what you need and will not scale easily.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • I proposed to use `Future`, but your approach is more related to android. Looks good! – mike Aug 14 '13 at 14:22
  • I don't want to download the pictures. Basically I used Html.ImageGetter to handle all the tags, so I implemented the getDrawable function. I would like to have that in an AsyncTask class. But I don't know how to return that drawable... :( I mean, I have lots of pictures in mItem.description + mItem.photo(the first image of the item( some news) ). – Cosmin Rusu Aug 14 '13 at 14:31
  • and the ImageGetter download the pictures ;-) – Waza_Be Aug 14 '13 at 14:32
  • How to return a Drawable in your method? – Cosmin Rusu Aug 14 '13 at 14:33
  • You cand change the type returned by doInBackgroubnd to wathever you want. replace the String with Drawable. – Waza_Be Aug 14 '13 at 14:33
  • " I mean, I have lots of pictures in mItem.description + mItem.photo(the first image of the item( some news) )." – Cosmin Rusu Aug 14 '13 at 14:35
  • Bast practice would be to use a progressDialog in the ActionBar and a layout with multiple ImageView – Waza_Be Aug 14 '13 at 14:38
  • Sure, I'm just a nearby, this seems to be the easiest way for me. So you say there is no way I can make just the MyImageGetter class a AsyncTask? – Cosmin Rusu Aug 14 '13 at 14:53
0

This answered question could help you: AsyncTask Android example And this is a good AsyncTask example: http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html

Community
  • 1
  • 1
user1337
  • 92
  • 2
  • 7
0

Is there a reason you're using a TextView instead of a WebView?

Based on what you're doing, I would say a webview is more appropriate, but if you're set on doing it this way, you should check out this answer.

Community
  • 1
  • 1
lifeson106
  • 503
  • 4
  • 11
  • WebView is slow, and use a lot of memory. Would be better to use ImageView in layout in fact... – Waza_Be Aug 14 '13 at 14:18
0

Use Future

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

Further information can be found here http://developer.android.com/reference/java/util/concurrent/Future.html

mike
  • 4,929
  • 4
  • 40
  • 80