将HTML.fromHTML()写入android中的图像

问题描述 投票:0回答:1

我已经将我的Edittext数据以HTML格式存储在DB中。我正在使用HTML.fromHTML()

在HTML文本视图中显示相同的数据。

现在,我想将此HTML字符串作为图像共享。我正在尝试以下方法。

 // storing below value in DB

String Notes= Html.toHtml(notes_content.getText());

//Then through select query i am fetching data as a string.

String notescontent;
Cursor data = mDatabaseHelper.getData();
            listData = new ArrayList<>();
            while (data.moveToNext()) {
               notescontent= data.getString(1);

            }


String content=Html.fromHtml(notescontent).toString();
            System.out.println(">>>>>>>>>>>>>>>>>> content >>>>>>>>>>> "+content);
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setTextSize(20f);
                paint.setColor(Color.WHITE);
                paint.setTextAlign(Paint.Align.LEFT);
                float baseline = -paint.ascent(); // ascent() is negative
                int width = (int) (paint.measureText(content) + 0.5f); // round
                int height = (int) (baseline + paint.descent() + 0.5f);

             Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //use ARGB_8888 for better quality
             Canvas canvas = new Canvas(bmp);
            canvas.drawText(content, 0, baseline, paint);

            File cachePath = new File(this.getCacheDir(), "images");
            cachePath.mkdirs(); // don't forget to make the directory
            FileOutputStream stream = new FileOutputStream(cachePath + "/Notes.png"); // overwrites this image every time
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            stream.close();


            File imagePath = new File(this.getCacheDir(), "images");
            File newFile = new File(imagePath, "Smart_Notes.png");
            Uri contentUri = FileProvider.getUriForFile(this, "com.note.fileprovider", newFile);

            if (contentUri != null) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
                shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
                shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
                startActivity(Intent.createChooser(shareIntent, "Choose an app"));

图像已成功存储在缓存目录中,我也能够成功共享。

问题:

我的文本为HTML格式,所以我将其转换为字符串String content = Html.fromHtml(notescontent).toString();并共享为图像,但文本并未按原样显示。

HTML格式(粗体,斜体)不再出现​​。此外,文本格式不正确,文本在图像中为单行。


在从html转换之前,请在notescontent(我从数据库中获取的字符串值)下面找到

I/System.out: >>>>>>>>>>>>>>>>>> content >>>>>>>>>>> <p dir="ltr">testing date and time ? tested by <b>ajinkya kadam</b>.<br></p>
    <p dir="ltr">this is a test data for <b><i>testing purpose</i></b><br>
    </p>

请从HTML转换后,在notescontent下方找到String content = Html.fromHtml(notescontent).toString();] >>

I/System.out: >>>>>>>>>>>>>>>>>> content >>>>>>>>>>> testing date and time ? tested by ajinkya kadam.
    this is a test data for testing purpose

有人可以在上面帮助我吗?我希望文本应该以带有样式和换行符的图像形式出现。

非常感谢。

我已经将我的Edittext数据以HTML格式存储在DB中。我正在使用HTML.fromHTML()以HTML格式在textview中显示相同的数据。现在,我想将此HTML字符串作为图像共享。我正在尝试以下...

android html android-canvas android-bitmap
1个回答
0
投票

如下使用TextPaintStaticLayout来获得所需的格式:

© www.soinside.com 2019 - 2024. All rights reserved.