我正在传递 url 以从服务器获取图像,例如
getBitmapFromURL("http://abc.xyz.in/logo.jpg");
但它没有返回任何东西。说图像是私人的。所以我的问题是无论如何都要将 cookie 传递给 getBitmapFromURL() 方法。这样我就可以得到图像。或者还有其他选择吗?非常感谢。
我正在传递 url 以从服务器获取图像,例如
getBitmapFromURL("http://abc.xyz.in/logo.jpg");
但它没有返回任何东西。说图像是私人的。所以我的问题是无论如何都要将 cookie 传递给 getBitmapFromURL() 方法。这样我就可以得到图像。或者还有其他选择吗?非常感谢。
2014,如果答案对你来说可行,请提出答案和积分
这应该可以解决问题:
public static Bitmap getBitmapFromURL(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;
}
}
不要忘记在清单中添加 Internet 权限和任何其他所需的权限。
我得到了我的答案。我可以通过使用以下行来做到这一点:
connection.addRequestProperty("key", value);