//爲的AsyncTask
的子類
GetXMLTask task = new GetXMLTask();
//執行任務
task.execute(new String[] { "ImageURL" });
//然後在Asyntask類創建一個對象將圖像分配給圖像視圖以避免android.os.NetworkOnMainThreadException
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
我希望,你得到Skia的解碼器返回false,請確保您的收益率這個消息還是不行,請檢查您的logcat,此消息???? – 2010-12-22 14:10:30
@Sankar:雅,我檢查了logcat,我是__NOT__「得到Skia解碼器返回false」的消息。 – Sen 2010-12-22 14:31:40
然後告訴我你的Logcat中有什麼信息? – 2010-12-22 14:32:30