2013-04-22 4 views
0

활동 내에서 TextView 이미지가 포함 된 html 텍스트를 표시하고 싶습니다. 이미지를 표시 할 수 없습니다. ImageGetter을 사용하고 있습니다. 로컬 Drawable을 표시하려고 시도하지만 URL에서 이미지를 가져 오려고하면 내 모든 것을 표시하지 않습니다. 다음은 내 코드입니다 :이미지 게터가 작동하지 않습니다.

package com.example.imagegetter; 

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.text.Html; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    String htmlTextWithImage = "<h1>Image Getter Example</h1>" 
      + "<img src=\"http://allisonnazarian.com/wp-content/uploads/2012/02/random.jpg\"/>"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     TextView tv = (TextView) findViewById(R.id.tv); 
     tv.setText(Html.fromHtml(htmlTextWithImage,new MyImageGetter(),null)); 
    } 

    private class MyImageGetter implements Html.ImageGetter{ 

     @Override 
     public Drawable getDrawable(String arg0) { 
      Bitmap bitmap; 
      try { 
       bitmap = BitmapFactory.decodeStream(new URL(arg0).openStream()); 
       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; 
      } 
     } 
    } 
} 

답변

관련 문제