23
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HTMLをパースして画像をダウンロード&表示する方法

Last updated at Posted at 2014-06-26

HTMLをパースして画像をダウンロード&表示する方法

HTMLをパースするいい感じのライブラリを見つけたので、
ついでに別のライブラリを使ってパースした画像パスを元に画像をダウンロード&表示させるということをしてみました。

今回はAsyncTaskLoaderを使ってWikipediaのHTMLを取得し、
imgタグの画像をImageView(実体はWebView)に表示させる。

今回使用したライブラリ

JSOUP(HTMLパーサ)
http://jsoup.org/

Android Query(jQueryのように)
https://code.google.com/p/android-query/

実装

public class MyActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Document> {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        MyAsyncTaskLoader appLoader = new MyAsyncTaskLoader(getApplication());

        // loaderの開始
        appLoader.forceLoad();
        return appLoader;
    }

    @Override
    public void onLoadFinished(Loader loader, Document doc) {
        if (doc != null) {
        	// 取得成功
            Element content = doc.getElementById("mp-tfa");
            Elements images = content.getElementsByTag("img");
            Element image = images.get(0);
            String imagePath = "http:" + image.attr("src");

            AQuery imageView = new AQuery(this);
            imageView.id(R.id.imageView).visible().webImage(imagePath, true, false, 0);
            Log.d("imagePath", imagePath);
        }
    }

    @Override
    public void onLoaderReset(Loader loader) {

    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_my, container, false);
            getLoaderManager().initLoader(0, null, MyActivity.this);


            return rootView;
        }
    }

    public static class MyAsyncTaskLoader extends AsyncTaskLoader<Document> {

        public MyAsyncTaskLoader(Context context) {
            super(context);
        }

        @Override
        public Document loadInBackground() {
            Document doc = null;
            try {
            	// HTML取得
                doc = Jsoup.connect("http://en.wikipedia.org/wiki/Main_Page").get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return doc;
        }
    }
}
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity$PlaceholderFragment">

    <WebView
        android:id="@+id/imageView"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

23
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?