Friday, 16 August 2013

Getting the redirected address after URL.openConnection();

Getting the redirected address after URL.openConnection();

First of all I would like to apologise if this question has been asked,
I've been Googling but not found any solution.
URL A: http://graph.facebook.com/1787989776/picture
URL B:
http://profile.ak.fbcdn.net/hprofile-ak-ash3/49968_1787989776_350012849_q.jpg
In Short: I want to cache my photo using file name as URL redirected after
visiting URL A, instead of URL A.
Detailed description:
We always cache images downloaded. In my case I would like to cache
facebook profile's photo. Eg: An image downloaded from URL A.
By clicking the link, you can see, we are being redirected to URL B.
Caching my photo using URL A isn't safe because the image may have changed
the next time I call it (user change his profile photo, but Facebook api
ease our job by maintaining the graph Url), while my image loader would
not be fetching any new image, because the condition "Image is found in
cache" gives me true, sure it does.
So, it would be good if I can get the URL B and use that to cache my photo.
I believe the description is enough but if you would like to know what I
have done here it is:
URL url = new URL(imgUrl); // imgUrl = URL A
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(downloadedFile); //
downloadedFile = File named with encoded URL A
byte data[] = new byte[10240]; // 10kb
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();

No comments:

Post a Comment