Skip to content

Commit 56051de

Browse files
committed
Add AsyncUtils.readBitmap
1 parent 6de7f73 commit 56051de

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@
55

66
import android.graphics.Bitmap;
77
import android.graphics.BitmapFactory;
8+
import android.os.AsyncTask;
9+
10+
import net.servicestack.client.AsyncResult;
811

912
import java.io.IOException;
1013
import java.io.InputStream;
1114
import java.net.HttpURLConnection;
15+
import java.net.URL;
1216

1317
public class AndroidUtils {
18+
19+
public static Bitmap readBitmap(String url){
20+
try {
21+
URL heartbeatUrl = new URL(url);
22+
HttpURLConnection conn = (HttpURLConnection)heartbeatUrl.openConnection();
23+
return readBitmap(conn.getInputStream());
24+
} catch (IOException e) {
25+
throw new RuntimeException(e);
26+
}
27+
}
28+
1429
public static Bitmap readBitmap(HttpURLConnection response){
1530
try {
1631
return readBitmap(response.getInputStream());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package net.servicestack.android;
2+
3+
import android.graphics.Bitmap;
4+
import android.os.AsyncTask;
5+
6+
import net.servicestack.client.AsyncResult;
7+
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
11+
/**
12+
* Created by mythz on 2/15/2017.
13+
*/
14+
15+
public class AsyncUtils {
16+
17+
public static void readBitmap(final String url, final AsyncResult<Bitmap> asyncResult){
18+
new AsyncTask<String, Void, Bitmap>(){
19+
20+
@Override
21+
protected Bitmap doInBackground(String... url) {
22+
HttpURLConnection conn = null;
23+
try {
24+
conn = (HttpURLConnection)new URL(url[0]).openConnection();
25+
return AndroidUtils.readBitmap(conn.getInputStream());
26+
} catch (Exception e) {
27+
asyncResult.setError(e);
28+
return null;
29+
}
30+
}
31+
32+
@Override
33+
protected void onPostExecute(Bitmap bitmap) {
34+
asyncResult.setResult(bitmap);
35+
}
36+
};
37+
}
38+
}

src/AndroidClient/android/src/main/java/net/servicestack/client/Utils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ public static byte[] toUtf8Bytes(String string) {
499499

500500
public static String readToEnd(String url) {
501501
try {
502-
URL heartbeatUrl = new URL(url);
503-
HttpURLConnection conn = (HttpURLConnection)heartbeatUrl.openConnection();
502+
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
504503
return readToEnd(conn);
505504
} catch (Exception e) {
506505
throw new RuntimeException(e);
@@ -529,6 +528,16 @@ public static String readToEnd(InputStream stream, final String charsetName) thr
529528
return text;
530529
}
531530

531+
public static byte[] readBytesToEnd(String url) {
532+
try {
533+
URL heartbeatUrl = new URL(url);
534+
HttpURLConnection conn = (HttpURLConnection)heartbeatUrl.openConnection();
535+
return readBytesToEnd(conn);
536+
} catch (Exception e) {
537+
throw new RuntimeException(e);
538+
}
539+
}
540+
532541
public static byte[] readBytesToEnd(HttpURLConnection response){
533542
try {
534543
return readBytesToEnd(response.getInputStream());

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ public static byte[] toUtf8Bytes(String string) {
499499

500500
public static String readToEnd(String url) {
501501
try {
502-
URL heartbeatUrl = new URL(url);
503-
HttpURLConnection conn = (HttpURLConnection)heartbeatUrl.openConnection();
502+
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
504503
return readToEnd(conn);
505504
} catch (Exception e) {
506505
throw new RuntimeException(e);
@@ -529,6 +528,16 @@ public static String readToEnd(InputStream stream, final String charsetName) thr
529528
return text;
530529
}
531530

531+
public static byte[] readBytesToEnd(String url) {
532+
try {
533+
URL heartbeatUrl = new URL(url);
534+
HttpURLConnection conn = (HttpURLConnection)heartbeatUrl.openConnection();
535+
return readBytesToEnd(conn);
536+
} catch (Exception e) {
537+
throw new RuntimeException(e);
538+
}
539+
}
540+
532541
public static byte[] readBytesToEnd(HttpURLConnection response){
533542
try {
534543
return readBytesToEnd(response.getInputStream());

0 commit comments

Comments
 (0)