使用 AsyncTask 下载图像,如果没有网络连接,我需要帮助
我是编程新手,所以请耐心等待,这对我来说是一次重要的学习经历,而且到目前为止很有趣。
我想做的是将图像加载到我的应用程序中,我定期更改并上传到我的服务器。现在,它会加载放置图像,直到正确的图像在后台下载完成,然后显示下载的新图像。
我的问题是,如果没有网络连接,它最终什么也不会显示。我假设我可以在 PostExecute 中做一些事情,如果无法从网络加载图像,则可以在应用程序内显示可绘制的图像,我只是不知道该怎么做!
任何帮助将不胜感激!
编辑:我是否走在显示备用图像的正确轨道上?
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
private Resources res;
public ImageDownloader(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
url = params[0];
try {
return BitmapFactory.decodeStream(new URL(url).openConnection()
.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
return BitmapFactory.decodeResource(res, R.drawable.displayThis);
}
}
@Override
protected void onPostExecute(Bitmap result) {
if (isCancelled()) {
result = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
}
}
}
@Override
protected void onPreExecute() {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageResource(R.drawable.uboxback);
}
}
}
}
I'm new to programming so bear with me, this is a big learning experience for me, and it's been a lot of fun so far.
What I'm trying to do is to load an image into my app that I change and upload to my server on a regular basis. Right now, it loads a placement image until the proper image is finished downloading in the background, then shows the new image that it's downloaded.
My problem is that if there is no network connection, it ends up showing nothing at all. I'm assuming that there's something I can do in PostExecute that would show a drawable from within the app if it fails to load the image from the net, I'm just not sure how to do it!
Any help would be greatly appreciated!
EDIT: Am I on the right track to displaying an alternate image?
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
private Resources res;
public ImageDownloader(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
url = params[0];
try {
return BitmapFactory.decodeStream(new URL(url).openConnection()
.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
return BitmapFactory.decodeResource(res, R.drawable.displayThis);
}
}
@Override
protected void onPostExecute(Bitmap result) {
if (isCancelled()) {
result = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
}
}
}
@Override
protected void onPreExecute() {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageResource(R.drawable.uboxback);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你从 doInBackground 函数得到 null 作为返回值,你不能只显示另一个图像吗?
此外,您始终可以在尝试执行任何操作之前检查是否有可用的网络连接,然后在此时执行某些操作?
您可以通过执行类似于以下操作的操作来检查网络连接是否可用:
这有帮助吗?
贝克斯
If you get null as a return value from you doInBackground function can you not just display another image?
Also you could always check if there is a network connection available before trying to do any of it and do something at that point?
You can check if a network connection is available by doing something similar to the following:
Does this help at all?
Bex
如果在 doInBackground() 中从互联网获取数据期间互联网失败,则 catch 块中将出现异常。在这里你可以做到这一点。
取一个全局变量。如果互联网失败,则将“失败”值分配给 catch 块中的该变量。然后在 onPostExecute() 中,您可以借助 if 和 else 取决于此变量来检查 do 。
希望它有帮助......
In case of internet failed during data getting from internet in doInBackground() , Exception would come in the catch block. Here u can do the trick.
Take one global variable. If internet is failed then assign the "Failed" value to that variable in the catch block. Then in the onPostExecute() u can check do with the help of if and else depends on this variable.
Hope it helps....