我在Android中创建了一个asynctask,该任务从链接中获取图像作为位图,并在imageview中显示
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.io.InputStream;
public class ImageViewLoader extends AsyncTask<String, Void, Bitmap>
{
ImageView bmImage;
public ImageViewLoader(ImageView bmImage)
{
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls)
{
String urldisplay = urls[0];
Bitmap bitmap = null;
try
{
InputStream in = new java.net.URL(urldisplay).openStream();
bitmap = BitmapFactory.decodeStream(in);
}
catch (Exception e)
{
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap result)
{
bmImage.setImageBitmap(result);
}
}
例如,如果我运行此
ImageViewLoader loader=new ImageViewLoader(imageView);
loader.execute("http://localhost:8000/media/12");
我在imageview中看到了这个
该图像已成功显示,但是如果该图像不存在或出现连接错误,我将得到一个空的imageview,我想知道如何检查从doInBackground返回的位图是否为有效图像这样我就可以将imageview的可见性设置为GONE