Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show image from HTML tag in text view?

Xml Layout

<org.sufficientlysecure.htmltextview.HtmlTextView
            android:id="@+id/txtDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:lineSpacingExtra="5dp"
            android:padding="5dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/black" />

Java

txtDescription.setHtml(object.getString("content"), new HtmlHttpImageGetter(txtDescription));

Response from server

{"success":"true","Content":{"id":115,"title":"vvipprogram","content":"<img src=\"https://example.com//media//images//topvvip.jpg" alt=\"topvvip\" width=\"43%\"\/>\r\n"}}

Result Screenshot

enter image description here

I Want to show this image in full page ? How to do this ? plz help me .

like image 653
Sachin Yadav Avatar asked Oct 24 '17 14:10

Sachin Yadav


1 Answers

You can use this code for set html image and text both from textview:

This is xml code:

      <TextView
        android:id="@+id/txtDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="5dp"
        android:padding="5dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:textColor="@color/black" />

This is Java code:

public class ConditionOfUseActivity extends Activity implements Html.ImageGetter {
     private TextView txtDescription;
     private Drawable empty;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_condition_use);
        txtDescription = (TextView) findViewById(R.id.txtDescription);
        String source = "this is a test of <b>ImageGetter</b> it contains " +
            "two images: <br/>" +
            "<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
            "<img src=\"http://developer.android.com/assets/images/icon_search.png\">";           
        Spanned spanned = Html.fromHtml(object.getString("content"), ConditionOfUseActivity.this, null);
        txtDescription.setText(spanned); 
     }

     @Override
     public Drawable getDrawable(String s) {
       LevelListDrawable d = new LevelListDrawable();
       empty = getResources().getDrawable(R.drawable.splash1);
       d.addLevel(0, 0, empty);
       d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
       new LoadImage().execute(s, d);
       return d;
    }

    class LoadImage extends AsyncTask<Object, Void, Bitmap> {
       private LevelListDrawable mDrawable;

       @Override
       protected Bitmap doInBackground(Object... params) {
          String source = (String) params[0];
          mDrawable = (LevelListDrawable) params[1];
          Log.d(TAG, "doInBackground " + source);
          try {
            InputStream is = new URL(source).openStream();
            return BitmapFactory.decodeStream(is);
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (MalformedURLException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            BitmapDrawable d = new BitmapDrawable(bitmap);
            mDrawable.addLevel(1, 1, d);
            //mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            mDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
            mDrawable.setLevel(1);
            CharSequence t = txtDescription.getText();
            txtDescription.setText(t);
        }
    }
}
like image 135
Ramesh Bhati Avatar answered Oct 22 '22 03:10

Ramesh Bhati