簡體   English   中英

從URL獲取位圖圖像無濟於事(... X文件...)

[英]Getting Bitmap image from URL does nothing… (…X file…)

異步圖像下載並存儲在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時,它返回空圖像:

- 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...

解決方法如下:

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 {
            ...
        }
    }

}

在任何類中(例如實用程序...):

/** 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM