|
4 | 4 | import androidx.appcompat.app.AppCompatActivity;
|
5 | 5 |
|
6 | 6 | import android.os.Bundle;
|
| 7 | +import android.os.Environment; |
7 | 8 | import android.util.Log;
|
8 | 9 | import android.view.View;
|
9 | 10 |
|
10 | 11 | import com.codepath.asynchttpclient.AsyncHttpClient;
|
11 | 12 | import com.codepath.asynchttpclient.RequestHeaders;
|
12 | 13 | import com.codepath.asynchttpclient.RequestParams;
|
| 14 | +import com.codepath.asynchttpclient.callback.BinaryHttpResponseHandler; |
13 | 15 | import com.codepath.asynchttpclient.callback.JsonHttpResponseHandler;
|
14 | 16 | import com.codepath.asynchttpclient.callback.TextHttpResponseHandler;
|
15 | 17 |
|
| 18 | +import java.io.BufferedInputStream; |
| 19 | +import java.io.BufferedOutputStream; |
| 20 | +import java.io.ByteArrayOutputStream; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileOutputStream; |
16 | 23 | import java.io.IOException;
|
| 24 | +import java.io.InputStream; |
| 25 | + |
17 | 26 | import okhttp3.Headers;
|
18 | 27 | import okhttp3.MediaType;
|
19 | 28 | import okhttp3.MultipartBody;
|
20 | 29 | import okhttp3.RequestBody;
|
| 30 | +import okhttp3.Response; |
21 | 31 | import okio.BufferedSource;
|
22 | 32 | import okio.ByteString;
|
23 | 33 | import okio.Okio;
|
@@ -92,11 +102,31 @@ public void onFailure(int statusCode, Headers headers, String response, Throwabl
|
92 | 102 |
|
93 | 103 | }
|
94 | 104 |
|
95 |
| - client.post("https://api.thecatapi.com/v1/images/search", "test", new JsonHttpResponseHandler() { |
| 105 | + client.get("https://cdn2.thecatapi.com/images/6eg.jpg", new BinaryHttpResponseHandler() { |
96 | 106 | @Override
|
97 |
| - public void onSuccess(int statusCode, Headers headers, JSON json) { |
98 |
| - Log.d("DEBUG", json.toString()); |
99 |
| - |
| 107 | + public void onSuccess(int statusCode, Headers headers, Response response) { |
| 108 | + try { |
| 109 | + // ~/Library/Android/sdk/platform-tools/adb pull /sdcard/Android/data/com.codepath.cpasynchttpclient/files/Pictures/TEST/test.jpg |
| 110 | + File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "TEST"); |
| 111 | + |
| 112 | + // Create the storage directory if it does not exist |
| 113 | + if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){ |
| 114 | + Log.d("DEBUG", "failed to create directory"); |
| 115 | + } |
| 116 | + |
| 117 | + // Return the file target for the photo based on filename |
| 118 | + InputStream data = response.body().byteStream(); |
| 119 | + File file = new File(mediaStorageDir.getPath() + File.separator + "test.jpg"); |
| 120 | + FileOutputStream outputStream = new FileOutputStream(file); |
| 121 | + byte[] buffer = new byte[2048]; |
| 122 | + int len; |
| 123 | + while ( (len = data.read(buffer)) != -1) { |
| 124 | + outputStream.write(buffer, 0, len); |
| 125 | + } |
| 126 | + Log.e("DEBUG", "done!"); |
| 127 | + } catch (IOException e) { |
| 128 | + Log.e("DEBUG", e.toString()); |
| 129 | + } |
100 | 130 | }
|
101 | 131 |
|
102 | 132 | @Override
|
|
0 commit comments