Замените весь разделенный URL-адрес (декодированный) на кодированный URL в заданной строке html. Позже используйте строку html, чтобы получить изображение из HTML ImageGetter - PullRequest
0 голосов
/ 19 июня 2019

Я использую Html.ImageGetter, но URL-адрес в строке html разделен пробелом, из-за чего он не принимает полный URL-адрес.Поэтому я хочу закодировать URL-адрес, чтобы проанализировать его и извлечь из него изображение.В настоящее время я удаляю (\ ") escape-символ для разбора URL. Я ищу лучший подход.

@Override
protected void onCreate(Bundle savedInstanceState) {
    TextView tempDesc;    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity);
    tempDesc= findViewById(R.id.description);
    String html = "<p>This is a space separated image url<p><img alt src=\"https://s3.us-east-2.amazonaws.com/mtp-uploads1/openpicker/fkOYciaj0wJVSJvA-1556271210514-Screenshot from 2019-04-26 15-03-20.png\"</p>"; tempDesc.setText(Html.fromHtml(html,this,null);}
@Override
public Drawable getDrawable(String source) {
    LevelListDrawable d = new LevelListDrawable();
    Drawable empty = getResources().getDrawable(R.drawable.tick_icon);
    d.addLevel(0, 0, empty);
    d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
    new LoadImage().execute(source, d);
    return d;
}

@SuppressLint("StaticFieldLeak")
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
    private LevelListDrawable mDrawable;
    @Override
    protected Bitmap doInBackground(Object... params) {
        String source = (String) params[0];
        //This is done to remove the \" from the image url
        //Looking for better solution.
        String result = source.substring(2, source.length() - 2);
        mDrawable = (LevelListDrawable) params[1];
        try {
            InputStream is = new java.net.URL(result).openStream();
            Bitmap abc = BitmapFactory.decodeStream(is);
            return abc;
        } catch (FileNotFoundException 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.setLevel(1);
            CharSequence t = quesDescription.getText();
            quesDescription.refreshDrawableState();
            quesDescription.setText(t);
        }
    }

}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}
...