异步图像下载并存储在ImageView数组
中我在从网址加载位图图片时遇到问题。关键是代码有效,因为我从服务器调用2个请求来检索一些信息,在第一个请求期间,一切正常,但在第二个请求期间,似乎是connection.connect(); 什么都不做......
以下是代码:
第一次检索
private class RequestQuickEventService extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... gameId) {
quickEvent = MyRestClient.getInstance().retrieveQuickEvent();
...
if (quickEvent == null) {
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
prepareQuestion();
}
}
}
private void prepareQuestion() {
new DownloadImageService().execute(quickEvent.getImage());
}
private class DownloadImageService extends AsyncTask<String, Void, Bitmap> {
private Bitmap loadImageFromNetwork(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
questionImage.setImageBitmap(result);
...
}
}
第二次检索
private class SendQuickQuestionService extends AsyncTask<Integer, Void, Void> {
protected void onPreExecute() {
...
taskResponse = new TaskResponse(ti, te, 1);
...
}
protected Void doInBackground(Integer... params) {
...
quickEvent = MyRestClient.getInstance().sendQuickEvent(...);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (quickEvent != null) {
prepareQuestion(); //same as before
}
}
}
我不知道为什么,但第二次调用loadImageFromNetwork时,它返回null image:
- First url: https://.../e4774cdda0793f86414e8b9140bb6db42012-02-20_161209.586668.jpeg-->OK
- Second url: https://.../e4774cdda0793f86414e8b9140bb6db42012-02-20_143228.782917.png-->FAIL
connection.connect(); //THE SECOND TIME DOES NOTHING...
答案 0 :(得分:0)
以下是解决方案:
enter/** Classes **/
private class GetProfilePicAsyncTask extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
return Utility.getBitmap(User.getInstance().getAvatar());
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
avatar.setImageBitmap(result);
}
else {
...
}
}
}
和
在任何类别(例如Utils)中:
/** Variables **/
public static AndroidHttpClient httpclient = null;
/** Public Functions **/
public static Bitmap getBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
bis.close();
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (httpclient != null) {
httpclient.close();
}
}
return bm;
}
/** Classes **/
private static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}