简体   繁体   English

从URL获取位图图像无济于事(... X文件...)

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

Async Image Download and storing in an array of ImageView 异步图像下载并存储在ImageView数组中

I have a problem loading bitmap images from an url. 我从网址加载位图图像时遇到问题。 The point is that the code works, because I call 2 requests from the server to retrieve some information, during the first request, everything works fine, but during the second one, it seems to be that connection.connect(); 关键是代码有效,因为我从服务器调用了2个请求以检索一些信息,在第一个请求期间,一切正常,但是在第二个请求期间,似乎是connection.connect();。 doesn't do anything... 什么也没做...

Here is the code: 这是代码:

FIRST RETRIEVE 首次检索

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

}

SECOND RETRIEVE 第二次检索

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

}

I don't know why, but the second time I call loadImageFromNetwork, it returns null image: 我不知道为什么,但是第二次我调用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...

Here is the solution: 解决方法如下:

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

}

and

in whatever class (Utils for example...): 在任何类中(例如实用程序...):

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