gpt4 book ai didi

java - 可从带有蜂窝的 url 中绘制

转载 作者:行者123 更新时间:2023-12-01 05:43:22 25 4
gpt4 key购买 nike

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

 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 数据中获取:

 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));
}

}

肖像是一个 ImageView 。我不知道该怎么办。

最佳答案

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

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

关于java - 可从带有蜂窝的 url 中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6542227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com