【发布时间】:2019-11-02 03:54:55
【问题描述】:
我有什么:我正在从 URL 下载图像并将其转换为位图。
发生了什么:有时服务器返回一个图像,有时只是一个小的占位符。
我要做什么:如何找出位图是否包含图像(与很小的占位符相比,图像的大小相当适中)。
用于从 URL 获取图像的代码:
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
【问题讨论】:
-
您确实提到占位符小于典型图像。测量位图的宽/高是否足以区分两者?