Skip to content

Commit c5d1f88

Browse files
committed
Fix AsyncUtils.readBitmap
1 parent 0dc4d6d commit c5d1f88

File tree

1 file changed

+24
-6
lines changed
  • src/AndroidClient/android/src/main/java/net/servicestack/android

1 file changed

+24
-6
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/android/AsyncUtils.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.servicestack.client.AsyncResultVoid;
99
import net.servicestack.client.AsyncSuccess;
1010
import net.servicestack.client.AsyncSuccessVoid;
11+
import net.servicestack.client.Utils;
1112

1213
import java.net.HttpURLConnection;
1314
import java.net.URL;
@@ -54,15 +55,32 @@ public static void readBitmap(final String url, final AsyncSuccess<Bitmap> succe
5455
readBitmap(url, createAsyncResult(success, error));
5556
}
5657

57-
private static void readBitmap(final String url, final AsyncResult<Bitmap> asyncResult){
58+
private static void readBitmap(final String imgUrl, final AsyncResult<Bitmap> asyncResult){
5859
new AsyncTask<String, Void, Bitmap>(){
5960

6061
@Override
61-
protected Bitmap doInBackground(String... url) {
62+
protected Bitmap doInBackground(String... urls) {
6263
HttpURLConnection conn = null;
64+
String url = urls[0];
6365
try {
64-
conn = (HttpURLConnection)new URL(url[0]).openConnection();
65-
return AndroidUtils.readBitmap(conn.getInputStream());
66+
while (!Utils.isNullOrEmpty(url)){
67+
conn = (HttpURLConnection)new URL(url).openConnection();
68+
conn.setInstanceFollowRedirects(true);
69+
70+
switch (conn.getResponseCode()){
71+
case HttpURLConnection.HTTP_MOVED_PERM:
72+
case HttpURLConnection.HTTP_MOVED_TEMP:
73+
url = conn.getHeaderField("Location");
74+
if (!Utils.isNullOrEmpty(url))
75+
continue;
76+
}
77+
78+
Bitmap ret = AndroidUtils.readBitmap(conn.getInputStream());
79+
return ret;
80+
}
81+
82+
asyncResult.setError(new RuntimeException("Could not download Bitmap from: " + urls[0]));
83+
return null;
6684
} catch (Exception e) {
6785
asyncResult.setError(e);
6886
return null;
@@ -71,8 +89,8 @@ protected Bitmap doInBackground(String... url) {
7189

7290
@Override
7391
protected void onPostExecute(Bitmap bitmap) {
74-
asyncResult.setResult(bitmap);
92+
asyncResult.completeResult(bitmap);
7593
}
76-
};
94+
}.execute(imgUrl);
7795
}
7896
}

0 commit comments

Comments
 (0)