1

I am using the method HTML.fromHtml in android to show some about text in my app. Is it possible to add an image into my about text and if so how?

Here is my activity where the aboutText will be started

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );

    setContentView(R.layout.activity_information);

    int versionNumber = 0;
    String versionName = "";

    try{
        PackageInfo pInfo = getPackageManager().getPackageInfo( getPackageName(), 0 );
        versionNumber = pInfo.versionCode;
        versionName = pInfo.versionName;
    } catch (NameNotFoundException e){
        Log.e( TAG, "error while extracting version name and version number" + e.getMessage() );
    }

    TextView about = (TextView)findViewById( R.id.about_text_view );
    Spanned aboutText = Html.fromHtml( "<h1> PDiX Attach, Version " + versionName + "</h1>"
                                         + getString( R.string.about_text ));

    about.setText( aboutText );


}

And the resours R.id.about_text looks like this:

        <b>Requirements:</b>
        <br/>The application supports Android Devices with Version 2.2 or higher.
        <br/>HERE I WANT TO ADD AN IMAGE<br/>

        </body>


        ]]>

Thanks

4

2 回答 2

0

如果要添加图像,则应使用 WebView。

于 2013-01-21T15:15:31.920 回答
0

我将在这里猜测一下,并假设现在您知道应该使用 WebView 来显示带有本地图像的 HTML。我猜您需要知道的是如何将图像滑入 HTML 字符串。我是这里的新手,所以我不知道我的解决方案是否是最好的,但对于它的价值,这就是我所做的。简而言之,我将位图图像转换为 Base64 字符串。

import android.graphics.Bitmap;
import java.io.ByteArrayOutputStream;
import android.util.Base64;

...

    Bitmap image = [insert appropriate code here];
    String encodedImage = null;
    if ( image != null ) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        image.compress(Bitmap.CompressFormat.PNG, 100, baos); // PNG preserves transparancy 
        byte[] byteArrayImage = baos.toByteArray();
        encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
        String imageHTML = "<img src='data:image/jpg;base64," + 
            encodedImage + "'/>";
    }

我希望这有帮助。(作为附言,我感谢所有在网上论坛上回答此类问题的人。你们的耐心和善意帮助我学到了很多东西。)

于 2013-01-21T16:09:02.133 回答