2012-12-06 54 views
0

嗨,大家好,我有問題,我需要在EditText使用ImageGetter顯示圖像。 這項工作Android的 - 如何顯示圖像從SD卡android使用ImageGetter

String html = "<img src=\"ic_launcher\">"; 
    CharSequence text = Html.fromHtml(html, new Html.ImageGetter(){ 
     public Drawable getDrawable(String source){ 
      int id = getResources().getIdentifier(source, "drawable", getPackageName()); 
      Drawable d = getResources().getDrawable(id); 
      int w = d.getIntrinsicWidth(); 
      int h = d.getIntrinsicHeight(); 
      d.setBounds(0, 0, w, h); 

      return d; 
     } 
    }, null); 

    mContentEditText.setText(text); 

但我仍需要在SD卡上的圖像,而不是「R.drawable.IMAGE_NAME」,感謝

回答

0
String html2 = "<img src=\"a.jpg\">"; 
    CharSequence text2 = Html.fromHtml(html2, new Html.ImageGetter(){ 
     public Drawable getDrawable(String source){ 
      String path = "/sdcard/a/" + source; 
      File f = new File(path); 
      Drawable bmp = Drawable.createFromPath(f.getAbsolutePath()); 
      bmp.setBounds(0, 0, bmp.getIntrinsicWidth(), bmp.getIntrinsicHeight()); 
      return bmp; 
     } 
    }, null); 
    display.setText(text2); 

適合我! 100%thx全部:)

+0

如何獲得多個圖像? ,如果html2有兩個圖像src? – vish

0

您必須申請清單中的適當的權限,這樣就可以從外部存儲器中讀取。然後創建一段代碼,將搜索您的SD卡的圖像,你想要的。

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 

鏡像文件在哪裏是你的鏡像文件,例如:File imageFile = new File("/sdcard/gallery_photo_4.jpg");

+0

如何把這裏?... String html =「」; – bilhip

+0

我不明白你的意思。 – Pavlos

+0

如何從SD卡中的html圖像中獲取多張圖像? – vish

0
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    mPath ="Your image Path here"; 
    l1=(LinearLayout) findViewById(R.id.layout1); 
    l1.setBackgroundColor(Color.WHITE); 
    System.out.println(mPath); 

     Bitmap b=Bitmap.createScaledBitmap(BitmapFactory.decodeFile(mPath),l1.getWidth(),l1.getHeight(),false); 
if(b!=null){ 

     ImageView iv=new ImageView(this); 
      iv.setImageBitmap(b); 
    l2.addView(iv); 
    } 


} 

這裏是一個適用於me.hope它會幫助你的代碼。

+0

這是不是我想...畢竟感謝 – bilhip

+0

我已經把我自己的code.you可以使用Bitmap.decodeScaledBitmap(BitmapFoctory.decodeFile(mPath),width,height,filter);從你的SD卡上獲取圖像。這是你問的問題。 –

+0

你可以使用 環境.getExternalStorageDirectory()。toString()+「yourDirectory/yourImageName.png」 –