可从带有蜂窝的 url 中绘制

发布于 2024-11-18 08:51:18 字数 2089 浏览 2 评论 0原文

从一开始我就使用这种方法:

 public Drawable createPortrait(String url){
    try {
        InputStream is = (InputStream)new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "Image");
        return d;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

但是 honeycomb 不允许我再这样做,我在日志中看到的是: android.os.networkonmainthreadexception 。 问题是我的 url 已经取自 json data :

 private class GrabURL extends AsyncTask<String, Void, Void> {
    private final HttpClient Client = new DefaultHttpClient();
    private String Content;
    private String Error = null;
    private ProgressDialog Dialog = new ProgressDialog(Main.this);

    protected void onPreExecute() {
        Dialog.setMessage("Downloading source..");
        Dialog.show();
    }

    protected Void doInBackground(String... urls) {
        try {
            HttpGet httpget = new HttpGet(urls[0]);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            Content = Client.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            Error = e.getMessage();
            cancel(true);
        } catch (IOException e) {
            Error = e.getMessage();
            cancel(true);
        }

        return null;
    }
    protected void onPostExecute(Void unused) {
        Dialog.dismiss();
        if (Error != null) {
            Toast.makeText(Main.this, Error, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(Main.this, "Source: " + Content, Toast.LENGTH_LONG).show();
        }
        Object o = new Gson().fromJson(Content, Info.class);
        Info i = (Info)o;
        String d = i.getData().get(0).getLg_portrait();
        portrait.setBackgroundDrawable(createPortrait(d));
    }

}

并且 Portrait 是一个 ImageView 。我不知道该怎么办。

from the beginning i used this method :

 public Drawable createPortrait(String url){
    try {
        InputStream is = (InputStream)new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "Image");
        return d;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

but honeycomb doesn't allow me to do it anymore, what i see in my log is : android.os.networkonmainthreadexception .
the thing is that my url is already taken from json data :

 private class GrabURL extends AsyncTask<String, Void, Void> {
    private final HttpClient Client = new DefaultHttpClient();
    private String Content;
    private String Error = null;
    private ProgressDialog Dialog = new ProgressDialog(Main.this);

    protected void onPreExecute() {
        Dialog.setMessage("Downloading source..");
        Dialog.show();
    }

    protected Void doInBackground(String... urls) {
        try {
            HttpGet httpget = new HttpGet(urls[0]);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            Content = Client.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            Error = e.getMessage();
            cancel(true);
        } catch (IOException e) {
            Error = e.getMessage();
            cancel(true);
        }

        return null;
    }
    protected void onPostExecute(Void unused) {
        Dialog.dismiss();
        if (Error != null) {
            Toast.makeText(Main.this, Error, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(Main.this, "Source: " + Content, Toast.LENGTH_LONG).show();
        }
        Object o = new Gson().fromJson(Content, Info.class);
        Info i = (Info)o;
        String d = i.getData().get(0).getLg_portrait();
        portrait.setBackgroundDrawable(createPortrait(d));
    }

}

and portrait is an ImageView . i don't know what to do .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会发光的星星闪亮亮i 2024-11-25 08:51:18

您还需要在异步任务中下载图像。 Honeycomb 根本不允许您运行阻塞 UI 线程的冗长 HTTP 操作(从流读取会进行 HTTP 调用并等待下载图像)。您应该立即返回一些占位符,触发 AsyncTask 并在下载后替换图像。

提示“执行后”在 UI 线程中运行......

You need to download the image in Async task as well. Honeycomb simply does not let you run lengthy HTTP operation blocking the UI thread (reading from stream makes HTTP call and waits for the image to be downloaded). You should return some placeholder immediately, trigger AsyncTask and replace the image after it is already downloaded.

Hint "post execute" is run in UI thread....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文