2012-05-17 24 views
2

我試圖表現出的Base64與Html.fromHtmlHtml.imageGetter不爲Base64顯示IMG的Android

一個IMG我有一個形象的Base64編碼字符串的標籤。

這是我的代碼:

public class GlossaryAdapter extends BaseAdapter{ 

    ... 
    private Resources res; 

    public GlossaryAdapter(Context context, ...) { 
     this.res = context.getResources(); 
     ... 
    } 

    public View getView(int position, View convertView, ViewGroup arg2) { 
     ... 
     holder.tvContent.setText(Html.fromHtml(glossary.getContent(), new Html.ImageGetter() { 
     @Override 
     public Drawable getDrawable(String source) {    
       try { 
        byte[] data; 
        data = Base64.decode(source,Base64.DECODE); 
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
        return new BitmapDrawable(res, bitmap); 
       } catch (IOException e) { 
        e.printStackTrace(); 
        return null; 
       } 
      } 
     }, null)); 

glossary.getContent()包含:

<img src="AAAY5671NF..." /> 

我在一個HTML頁面和工程測試此字符串。顯示圖像。

我正在使用Android 1.6。 這Base64編碼類:http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

我沒有錯誤。但沒有任何表現。 如果我將返回更改爲'null',我得到一個小的灰色方塊。

有什麼想法?

+0

人?我做'bitmap.getWidth()'和'bitmap.getHeight()',並得到正確的圖像寬度和高度。但是當我使用位圖獲得了NPE。 – Munir

回答

3

後很長一段時間... 得到分貝imageBytes(BLOB)。 使用Base64並傳遞給src。 解碼並設置位圖邊界。 工程!

@Override 
    public Drawable getDrawable(String source) {    
      try { 
       byte[] data; 
       data = Base64.decode(source,Base64.DECODE); 
       Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
       Drawable d = new BitmapDrawable(res, bitmap); 
       d.setBounds(0,0,72,72) // <----- 
       return d; 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     }