【问题标题】:cookies with getBitmapFromURL()带有 getBitmapFromURL() 的 cookie
【发布时间】:2014-01-03 08:29:46
【问题描述】:

我正在传递 url 以从服务器获取图像,例如

getBitmapFromURL("http://abc.xyz.in/logo.jpg");

但它没有返回任何东西。说图像是私人的。所以我的问题是无论如何都要将 cookie 传递给 getBitmapFromURL() 方法。这样我就可以得到图像。或者还有其他选择吗?非常感谢。

【问题讨论】:

  • 我想通过 url 传递 cookie,然后只有我可以获取数据,我该怎么办?
  • 你不能直接这样做,尝试使用http连接并获取字节并将其转换为位图
  • @GirishNair 请解释一下作为答案。
  • 您的问题是is there anyway to pass the cookie to getBitmapFromURL() 答案是否定的,所以您必须使用cookie 发出HTTP 发布请求才能访问它,我没有找到任何其他方式或尝试联系github.com/nostra13/Android-Universal-Image-Loader 的nostra 来为你做这个

标签: android android-image android-bitmap


【解决方案1】:

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;
    }
} 

不要忘记在清单中添加互联网权限和任何其他需要的权限。

【讨论】:

  • 我在做同样的事情仍然没有得到任何东西,因为我的文件路径是私有的。所以我需要传递 cookie。
  • 嘿,我认为您可以使用 volley 库,请查看给定的链接。希望对你有帮助github.com/smanikandan14/Volley-demo
【解决方案2】:

我得到了答案。我可以使用以下行来做到这一点:

connection.addRequestProperty("key", value);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-06
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多