import android.graphics.Canvas;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;publicclassURLDrawableextendsBitmapDrawable{//the drawable that you need to set, you could set the initial drawing//with the loading image if you need toprotected Drawable drawable;@Overridepublicvoiddraw(Canvas canvas){//override the draw to facilitate refresh function laterif(drawable != null){
drawable.draw(canvas);}}}//
import java.io.InputStream;import java.net.URL;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.AsyncTask;import android.text.Html.ImageGetter;import android.util.Base64;import android.view.View;publicclassURLImageParserimplementsImageGetter{
Context context;
View container;publicURLImageParser(View container, Context context){this.context = context;this.container = container;}public Drawable getDrawable(String source){if(source.matches("data:image.*base64.*")){
String base_64_source = source.replaceAll("data:image.*base64","");byte[] data = Base64.decode(base_64_source, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0, data.length);
Drawable image =newBitmapDrawable(context.getResources(), bitmap);
image.setBounds(0,0,0+ image.getIntrinsicWidth(),0+ image.getIntrinsicHeight());return image;}else{
URLDrawable urlDrawable =newURLDrawable();
ImageGetterAsyncTask asyncTask =newImageGetterAsyncTask(urlDrawable);
asyncTask.execute(source);return urlDrawable;//return reference to URLDrawable where We will change with actual image from the src tag}}publicclassImageGetterAsyncTaskextendsAsyncTask<String, Void, Drawable>{
URLDrawable urlDrawable;publicImageGetterAsyncTask(URLDrawable d){this.urlDrawable = d;}@Overrideprotected Drawable doInBackground(String... params){
String source = params[0];returnfetchDrawable(source);}@OverrideprotectedvoidonPostExecute(Drawable result){
urlDrawable.setBounds(0,0,0+ result.getIntrinsicWidth(),0+ result.getIntrinsicHeight());//set the correct bound according to the result from HTTP call
urlDrawable.drawable = result;//change the reference of the current drawable to the result from the HTTP call
URLImageParser.this.container.invalidate();//redraw the image by invalidating the container}public Drawable fetchDrawable(String urlString){try{
InputStream is =(InputStream)newURL(urlString).getContent();
Drawable drawable = Drawable.createFromStream(is,"src");
drawable.setBounds(0,0,0+ drawable.getIntrinsicWidth(),0+ drawable.getIntrinsicHeight());return drawable;}catch(Exception e){return null;}}}}//