在textview(JSON字符串)中加载Html.fromHtml中的图像

时间:2016-02-17 17:25:44

标签: android json image-load

我遇到使用JSON字符串解析将图像从网站加载到TextView的问题。我试图在Html.ImageGetter和BitmapDrawable的帮助下加载图像。我看到了这些参考文献Load images in Html.fromHtml in textview (http url images, Base64 url images)Android HTML ImageGetter as AsyncTask

我不明白为什么我有Null Pointer Exception。 我正在尝试加载内容"来自http://techmasi.com/wp-json/posts此JSON链接的字符串。

这是我的代码

public class URLDrawable extends BitmapDrawable {
// the drawable that you need to set, you could set the initial drawing
// with the loading image if you need to
protected Drawable drawable;

@Override
public void draw(Canvas canvas) {
    // override the draw to facilitate refresh function later
    if(drawable != null) {
        drawable.draw(canvas);
    }
}}

这是另一个班级:

public class URLImageParser implements Html.ImageGetter {
Context context;
TextView container;

public URLImageParser(TextView t, Context c) {
    this.context = c;
    this.container = t;
}

public Drawable getDrawable(String source) {
    URLDrawable urlDrawable = new URLDrawable();

    // get the actual source
    ImageGetterAsyncTask asyncTask =
            new ImageGetterAsyncTask( urlDrawable);

    asyncTask.execute(source);

    // return reference to URLDrawable where I will change with actual image from
    // the src tag
    return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
    URLDrawable urlDrawable;

    public ImageGetterAsyncTask(URLDrawable d) {
        this.urlDrawable = d;
    }

    @Override
    protected Drawable doInBackground(String... params) {
        String source = params[0];
        return fetchDrawable(source);
    }

    @Override
    protected void onPostExecute(Drawable result) {
        // set the correct bound according to the result from HTTP call
        Log.d("height", "" + result.getIntrinsicHeight());
        Log.d("width",""+result.getIntrinsicWidth());
        urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight());
        // change the reference of the current drawable to the result
        // from the HTTP call
        urlDrawable.drawable = result;
        // redraw the image by invalidating the container
        URLImageParser.this.container.invalidate();
        // For ICS
        URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
                + result.getIntrinsicHeight()));
        // Pre ICS
        URLImageParser.this.container.setEllipsize(null);
    }
}


public Drawable fetchDrawable(String urlString) {
    try {
        InputStream is = fetch(urlString);
        Drawable drawable = Drawable.createFromStream(is, "src");
        drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                + drawable.getIntrinsicHeight());
        return drawable;
    } catch (Exception e) {
        return null;
    }
}

private InputStream fetch(String urlString) throws MalformedURLException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);
    return response.getEntity().getContent();
}}

我在以下行中收到NullPointer Exception:

urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight());

Logcat输出:

java.lang.NullPointerException
        at com.droiddigger.techmasi.URLImageParser$ImageGetterAsyncTask.onPostExecute(URLImageParser.java:64)
        at com.droiddigger.techmasi.URLImageParser$ImageGetterAsyncTask.onPostExecute(URLImageParser.java:48)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

请帮助。 提前致谢。

1 个答案:

答案 0 :(得分:1)

我建议不要使用Drawable.createFromStream()。它基本上没有记录和doesn't do what you need。我打赌它会返回null。

如果您需要从包含PNG,JPG,GIF或其他Android支持的图像类型的InputStream解码位图,请尝试BitmapFactory.decodeStream(),然后从该位图创建一个Drawable。