有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何基于Html标记拆分Html字符串

我有Html格式的字符串。我的Html可以包含任何标签,如图像,视频。。。 我现在可以正确处理图像和文本,如下所示: 我的xml中有一个文本视图:

TextView textView = new TextView(DetailActivity.this);

    textView.setText(Html.fromHtml(content, new Html.ImageGetter() {


        @Override
        public Drawable getDrawable(String source) {

            try {
                URI uri = new URI(source);
                URL videoUrl = uri.toURL();
                File tempFile = new File(videoUrl.getFile());
                filename = tempFile.getName();
            } catch (Exception e) {

            }

            Drawable drawable = null;
            ContextWrapper cw1 = new ContextWrapper(DetailActivity.this);
            File directory1 = cw1.getDir("multiImage", Context.MODE_PRIVATE);
            final File myImageFile1 = new File(directory1, filename);
            File f = new File(myImageFile1.getAbsolutePath());

            Log.i("multiImage", filename);
            final ImageView imageView = new ImageView(DetailActivity.this);

            if (f.exists()) {

                drawable = Drawable.createFromPath(myImageFile1.getAbsolutePath());

                //drawable.setBounds(0, 0, drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth());
                int imgH = drawable.getIntrinsicHeight();
                int imgW = drawable.getIntrinsicWidth();
                int padding = 20;
                int realWidth = ScreenW - (2 * padding);
                int realHeight = imgH * realWidth / imgW;
                drawable.setBounds(padding, 0, realWidth, realHeight);


            } else {

                Picasso.with(DetailActivity.this)
                        .load(source)
                        .into(imageView, new Callback() {
                            @Override
                            public void onSuccess() {


                                BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
                                Bitmap bitmap = draw.getBitmap();

                                FileOutputStream outStream = null;
                                File outFile = new File(myImageFile1.getAbsolutePath());
                                try {
                                    outStream = new FileOutputStream(outFile);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                                try {
                                    outStream.flush();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                try {
                                    outStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }


                            @Override
                            public void onError() {

                            }
                        });

                URL sourceURL;
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                try {
                    sourceURL = new URL(source);
                    URLConnection urlConnection = sourceURL.openConnection();
                    urlConnection.connect();
                    InputStream inputStream = urlConnection.getInputStream();
                    BufferedInputStream bufferedInputStream =
                            new BufferedInputStream(inputStream);
                    Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream);

                    // convert Bitmap to Drawable
                    drawable = new BitmapDrawable(getResources(), bm);

                    int imgH = drawable.getIntrinsicHeight();
                    int imgW = drawable.getIntrinsicWidth();
                    int padding = 20;
                    int realWidth = ScreenW - (2 * padding);
                    int realHeight = imgH * realWidth / imgW;
                    drawable.setBounds(padding, 0, realWidth, realHeight);


                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }


            return drawable;
        }

    }, new UlTagHandler()));

但我不能显示视频使用上述代码。 我想拆分字符串-->;从第一个到视频标签,再到视频标签末尾的视频标签和字符串的其余部分


共 (0) 个答案