【发布时间】:2014-04-06 20:59:53
【问题描述】:
我有一个应用程序,它使用 URL 将图像加载到位图中,最近收到了一封来自 Android 2.2.1 用户的电子邮件,该用户遇到了一些图像无法加载的问题。我试过环顾四周,似乎无法找到未加载的图像之间的任何联系。此外,有时无法为她加载的图像会重新开始工作,就像没有任何问题一样。我从未听说过任何更高版本的 Android 存在此问题,因此我认为这是 2.2.1 特有的问题。如果有人可以向我解释出了什么问题以及(如果可能的话)如何解决它,我将不胜感激。
以下是相关代码:
public class htmlGrabber extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try {
return getHtml();
}
catch (IOException e) {
problem();
return null;
}
}//end of doInBackground
@Override
protected void onPostExecute(String result){
Loading.fa.finish();
shared_preferences=getSharedPreferences("shared_preferences_test", MODE_PRIVATE);
shared_preferences_editor = shared_preferences.edit();
shared_preferences_editor.putString("url_key", current);
shared_preferences_editor.commit();
TouchImageView img = (TouchImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}
}
public String getHtml() throws IllegalStateException, IOException{
//gets html from the url stored in current, then parses through it and extracts the image url, then converts
//it into a bitmap image
String html = "";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(current);
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
/* Edited out the code that parses through the HTML looking for the image URL. The image URL is stored in the string "link"
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(link).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return html;
}//end of getHtml
}//end of htmlGrabber
如果您需要任何澄清,请告诉我!
【问题讨论】:
标签: android bitmap android-asynctask android-imageview