从 URL 获取位图图像不会执行任何操作...(...X 文件...)
异步图像下载并存储在 ImageView 数组中
我在从 url 加载位图图像时遇到问题。关键是代码有效,因为我从服务器调用了 2 个请求来检索一些信息,在第一个请求期间,一切正常,但在第二个请求期间,似乎是 connection.connect(); 不执行任何操作...
以下是代码:
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
}
}
}
我不知道为什么,但第二次调用 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...
Async Image Download and storing in an array of 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();
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:
- 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是解决方案:
在
任何类中(例如 Utils...):
Here is the solution:
and
in whatever class (Utils for example...):