我还有一个小问题, 我有一个标准格式的图片链接,但我需要在最后使用身份验证令牌来访问图像,因此每当我在我的webbrowser中输入正确的身份验证令牌图像时,都会自动下载。 这是我的问题,我如何在android中将这样的图像下载到位图? 因为使用
final URL bitmapUrl = new URL(imageUrl);
bitmap = BitmapFactory.decodeStream(bitmapUrl.openConnection().getInputStream());
不起作用......
答案 0 :(得分:1)
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(70000);
conn.setReadTimeout(70000);
conn.setRequestProperty("Auth_keyName", "Value");// if authentication required
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
答案 1 :(得分:0)
尝试此方法下载位图
public Bitmap downloadBitmap(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bmp= BitmapFactory.decodeStream(input);
return bmp;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}