2012-06-14 3 views
0

일부 데이터가 html 형식입니다. 나는 Html.fromHtml (String)을 사용하여 안드로이드의 textview에 데이터를 설정할 때 사용하고있다. 또한 html 데이터에는 2 ~ 3 개의 이미지가 포함됩니다. html 데이터에서 이미지의 이름을 가져 와서 배열에 저장하고 싶습니다. 배열의 이미지 수에 따라 이미지 뷰로 설정합니다. 이미지 태그의 src에서 이미지 이름을 얻으려면 어떻게해야합니까?android의 전체 문자열에서 특정 단어를 검색 하시겠습니까?

정규 표현식 또는 하위 문자열을 사용하면 어느 것이 더 나은 옵션이 될 수 있습니까?

몇 가지 예를 들어 설명해주십시오.

내 코드 :

public View getView(int position, View convertView, ViewGroup parent) 
{ 
--- 
--- 
    desc = (TextView) view.findViewById(R.id.description); 
    URLImageParser p = new URLImageParser(desc, this); 
    Spanned htmlSpan = Html.fromHtml(listItem.getdesc(), p, null); 
    desc.setText(htmlSpan); 
---- 
---- 
---- 
---- 
} 


public class URLDrawable extends BitmapDrawable { 
     // the drawable that you need to set, you could set the initial drawing 
     // with the loading image if you need to 
     protected Drawable drawable; 

     @Override 
     public void draw(Canvas canvas) { 
      // override the draw to facilitate refresh function later 
      if(drawable != null) { 
       drawable.draw(canvas); 
      } 
     } 
    } 



    public class URLImageParser implements ImageGetter { 
     ListAdapter c; 
     View container; 

     /*** 
     * Construct the URLImageParser which will execute AsyncTask and refresh the container 
     * @param t 
     * @param listAdapter 
     */ 
     public URLImageParser(View t, ListAdapter listAdapter) { 
      this.c = listAdapter; 
      this.container = t; 
     } 

     public Drawable getDrawable(String source) { 
      URLDrawable urlDrawable = new URLDrawable(); 

      // get the actual source 
      ImageGetterAsyncTask asyncTask = 
       new ImageGetterAsyncTask(urlDrawable); 

      asyncTask.execute(source); 

      // return reference to URLDrawable where I will change with actual image from 
      // the src tag 
      return urlDrawable; 
     } 

     public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> { 
      URLDrawable urlDrawable; 

      public ImageGetterAsyncTask(URLDrawable d) { 
       this.urlDrawable = d; 
      } 

      @Override 
      protected Drawable doInBackground(String... params) { 
       String source = params[0]; 
       return fetchDrawable(source); 
      } 

      @Override 
      protected void onPostExecute(Drawable result) { 
       // set the correct bound according to the result from HTTP call 
       urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 
         + result.getIntrinsicHeight()); 

       // change the reference of the current drawable to the result 
       // from the HTTP call 
       urlDrawable.drawable = result; 

       // redraw the image by invalidating the container 
       URLImageParser.this.container.invalidate(); 
      } 

      /*** 
      * Get the Drawable from URL 
      * @param urlString 
      * @return 
      */ 
      public Drawable fetchDrawable(String urlString) { 
       try { 
        InputStream is = fetch(urlString); 
        Drawable drawable = Drawable.createFromStream(is, "src"); 
        drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 
          + drawable.getIntrinsicHeight()); 
        return drawable; 
       } catch (Exception e) { 
        return null; 
       } 
      } 

      private InputStream fetch(String urlString) throws MalformedURLException, IOException { 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpGet request = new HttpGet(urlString); 
       HttpResponse response = httpClient.execute(request); 
       return response.getEntity().getContent(); 
      } 
     } 
    } 

답변

0

Html.ImageGetter() 더 도움이 될 것입니다, 그것을 발견 할 것이다 태그 <img> http://developer.android.com/reference/android/text/Html.ImageGetter.html

+0

안녕 덕분에 니펫을 ... I Html.imagegetter 예제를 살펴 보았습니다. 모두 내가 그릴에서 이미지를 얻는 것을 발견했습니다. 사실 내 모든 이미지는 서버에 저장되며 큰 크기입니다. 그래서 지금 그 이미지를 서버에서 비동기 적으로 가져와 imagegetter 메서드를 사용하여 텍스트 뷰로 설정해야합니다. 이 작업을 어떻게 수행 할 수 있습니까? – Amrutha

+0

당신은'doInBackground'에 서버 접두사 파일명'xxxx.imagefile.png' 이미지를 추가함으로써 그것을 할 수 있습니다. –

+0

@ user1455252 http://stackoverflow.com/questions/7424512/android-html-imagegetter-as-asynctask을 참조하십시오. –

0

내가

만약 이미지의 이름을 얻으려면 jsoup html 파서를 사용하면 인생이 훨씬 쉬워집니다.

이미지 이름이 HTML 데이터에서 어떻게 나타나는지 알지 못합니다.

내가 게시 한 예를 abc.png

그러나

<img src='image_url/abc.png'> 

File input = new File("/tmp/input.html"); 
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/"); 

Elements pngs = doc.select("img[src$=.png]").first(); 

String imageurl= pngs.attr("src"); which will return 

이미지 URL을 다음과 같이 HTML 이미지를 가지고 가정이/응답에 대한 here

관련 문제