1

I'm having trouble with android studio when I try to load an image from an url that requires login to view particular picture.

This is the code that gets the Image URL and it do returns an absolute URL of that image.

Document document = Jsoup.connect(url + "home").timeout(0).header("Cookie", cookie).get();
String imageUrl = document.select("MY CSS SELECTOR").attr("abs:data-src");

and I am using this code to convert that url to Bitmap Image

URLConnection connection = new java.net.URL(imageUrl).openConnection();
connection.setRequestProperty("Cookie", cookie);
connection.connect();
Bitmap temp = BitmapFactory.decodeStream(connection.getInputStream());

So I'm guessing that my second code isn't working as intended and I want to know how I can load image only shows when I am logged in.

Thanks

1 Answers1

0

First, it would probably be helpful to know, what the server actually responds with for both of your requests. Trying to find an issue without that is more like a witch hunt than actual error analysis.

Anyway, apart from cookie based authentication there are a couple more commonly used authentication schemes (http based authentication for example). So if I were you I would first make sure that the page you're trying to access actually does use cookie based authentication. Then make sure the URL you extract from your first request is correct. If it is not, try logging the actual server response as it might say something about what you're doing wrong. If the server responds with some kind of "unauthorized/access denied" response, make sure your requests are being sent the way you think they're being sent. (I've seen libraries setting missing http headers automatically which may cause the server to not respond the way you expect it to) Last, it might just be android's image decoder failing you. See this question for example BitmapFactory works perfectly on .png image but returns null when i load jpeg image from url