Skip to content

Commit 313447f

Browse files
committed
加入getAsBitmap
1 parent e225c95 commit 313447f

File tree

11 files changed

+187
-10
lines changed

11 files changed

+187
-10
lines changed

EasyHttp/src/main/java/com/jimmyworks/easyhttp/EasyHttp.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import com.jimmyworks.easyhttp.database.repository.HttpCookiesRepository
1111
import com.jimmyworks.easyhttp.service.DoRequestService
1212
import com.jimmyworks.easyhttp.type.HttpMethod
1313

14-
14+
/**
15+
* EasyHttp
16+
*
17+
* @author Jimmy Kang
18+
*/
1519
class EasyHttp {
1620

1721
companion object {

EasyHttp/src/main/java/com/jimmyworks/easyhttp/EasyHttpConfig.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ object EasyHttpConfig {
1111

1212
const val CACHE_DIR_NAME: String = "cache_easy_http"
1313

14+
const val IMAGE_CACHE_DIR_NAME: String = "image_cache_easy_http"
15+
1416
const val REQUEST_DIR_NAME: String = "easy_http_request_record"
1517

1618
const val RESPONSE_DIR_NAME: String = "easy_http_response_record"

EasyHttp/src/main/java/com/jimmyworks/easyhttp/exception/HttpException.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package com.jimmyworks.easyhttp.exception
22

33
/**
4-
* 類別無對應錯誤
4+
* http錯誤
55
*
66
* @author Jimmy Kang
77
*/
88
class HttpException : Exception {
99

10+
override val message: String
11+
1012
val url: String
1113

1214
val httpCode: Int
1315

1416
val responseBody: String?
1517

1618
constructor(url: String, errorMessage: String) : super("url: $url, $errorMessage") {
19+
this.message = "url: $url, $errorMessage"
1720
this.url = url
1821
this.httpCode = 0
1922
this.responseBody = null
2023
}
2124

2225
constructor(url: String, e: Exception) : super("url: $url, ${e.message}") {
26+
this.message = "url: $url, ${e.message}"
2327
this.url = url
2428
this.httpCode = 0
2529
this.responseBody = null
@@ -30,6 +34,7 @@ class HttpException : Exception {
3034
httpCode: Int,
3135
responseBody: String
3236
) : super("url: $url, http code: $httpCode, responseBody: $responseBody") {
37+
this.message = "url: $url, http code: $httpCode, responseBody: $responseBody"
3338
this.url = url
3439
this.httpCode = httpCode
3540
this.responseBody = responseBody
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jimmyworks.easyhttp.listener
2+
3+
import android.graphics.Bitmap
4+
import com.jimmyworks.easyhttp.exception.HttpException
5+
import okhttp3.Headers
6+
7+
/**
8+
* 字串回傳監聽器
9+
*
10+
* @author Jimmy Kang
11+
*/
12+
interface BitmapResponseListener {
13+
14+
fun onSuccess(headers: Headers, bitmap: Bitmap)
15+
16+
fun onProgress(downloadBytes: Long, totalBytes: Long)
17+
18+
fun onError(e: HttpException)
19+
}

EasyHttp/src/main/java/com/jimmyworks/easyhttp/service/DoRequestService.kt

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.jimmyworks.easyhttp.service
22

33
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.graphics.BitmapFactory
46
import com.google.gson.reflect.TypeToken
7+
import com.jimmyworks.easyhttp.EasyHttpConfig
58
import com.jimmyworks.easyhttp.entity.RequestInfo
69
import com.jimmyworks.easyhttp.entity.ResponseInfo
710
import com.jimmyworks.easyhttp.exception.HttpException
11+
import com.jimmyworks.easyhttp.listener.BitmapResponseListener
812
import com.jimmyworks.easyhttp.listener.DownloadListener
913
import com.jimmyworks.easyhttp.listener.JsonResponseListener
1014
import com.jimmyworks.easyhttp.listener.StringResponseListener
@@ -61,7 +65,7 @@ class DoRequestService(
6165
}
6266

6367
if (isSaveRecord) {
64-
saveRecordService.save(e.message)
68+
saveRecordService.save(e.message ?: "")
6569
}
6670
}
6771

@@ -157,6 +161,49 @@ class DoRequestService(
157161
})
158162
}
159163

164+
fun getAsBitmap(bitmapResponseListener: BitmapResponseListener) {
165+
getAsBitmap(null, bitmapResponseListener)
166+
}
167+
168+
fun getAsBitmap(
169+
bitmapOptions: BitmapFactory.Options?,
170+
bitmapResponseListener: BitmapResponseListener
171+
) {
172+
val imageCacheFile = File(
173+
CommonUtils.getCacheDir(context, EasyHttpConfig.IMAGE_CACHE_DIR_NAME),
174+
CommonUtils.createUUID()
175+
)
176+
FileUtils.mkdir(imageCacheFile.parentFile!!)
177+
download(imageCacheFile, object : DownloadListener {
178+
override fun onSuccess(headers: Headers, file: File) {
179+
val bitmap: Bitmap? = if (null == bitmapOptions) {
180+
BitmapFactory.decodeFile(file.path)
181+
} else {
182+
BitmapFactory.decodeFile(file.path, bitmapOptions)
183+
}
184+
FileUtils.delete(file)
185+
if (null == bitmap) {
186+
bitmapResponseListener.onError(
187+
HttpException(
188+
requestInfo.url,
189+
"Convert response to bitmap fail."
190+
)
191+
)
192+
return
193+
}
194+
bitmapResponseListener.onSuccess(headers, bitmap)
195+
}
196+
197+
override fun onProgress(downloadBytes: Long, totalBytes: Long) {
198+
bitmapResponseListener.onProgress(downloadBytes, totalBytes)
199+
}
200+
201+
override fun onError(e: HttpException) {
202+
bitmapResponseListener.onError(e)
203+
}
204+
})
205+
}
206+
160207
fun download(file: File, downloadListener: DownloadListener) {
161208
val saveRecordService = SaveRecordService(context, requestInfo, Date())
162209
FileUtils.mkdir(file.parentFile!!)
@@ -168,7 +215,7 @@ class DoRequestService(
168215
}
169216

170217
if (isSaveRecord) {
171-
saveRecordService.save(e.message)
218+
saveRecordService.save(e.message ?: "")
172219
}
173220
}
174221

EasyHttp/src/main/java/com/jimmyworks/easyhttp/service/SaveRecordService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SaveRecordService(
100100
saveResponse(id, responseInfo)
101101
}
102102

103-
fun save(errorMessage: String?) {
103+
fun save(errorMessage: String) {
104104
val httpRecordInfo = HttpRecordInfo(
105105
null,
106106
requestInfo.url,

EasyHttp/src/main/java/com/jimmyworks/easyhttp/utils/CommonUtils.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ import kotlin.math.abs
2323
*/
2424
class CommonUtils {
2525
companion object {
26+
/**
27+
* 產生UUID
28+
*
29+
* @return UUID
30+
*/
31+
fun createUUID(): String {
32+
var uuid = UUID.randomUUID().toString()
33+
uuid = uuid.replace("-", "")
34+
return uuid
35+
}
2636

2737
/**
2838
* 執行在主執行緒

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ android {
2323
}
2424
2525
dependencies {
26-
implementation 'io.github.af19git5:easy-http-android:0.0.3'
26+
implementation 'io.github.af19git5:easy-http-android:0.0.4'
2727
}
2828
```
2929

@@ -351,6 +351,51 @@ EasyHttp.get(context, url)
351351
});
352352
```
353353

354+
### Get response as bitmap
355+
356+
**Kotlin Example:**
357+
358+
```kotlin
359+
EasyHttp.get(context, url)
360+
.build()
361+
.getAsBitmap(file, object : BitmapResponseListener {
362+
override fun onSuccess(headers: Headers, bitmap: Bitmap) {
363+
// Do something...
364+
}
365+
366+
override fun onProgress(downloadBytes: Long, totalBytes: Long) {
367+
// Do something...
368+
}
369+
370+
override fun onError(e: HttpException) {
371+
// Do something...
372+
}
373+
})
374+
```
375+
376+
**Java Example:**
377+
378+
```java
379+
EasyHttp.get(context, url)
380+
.build()
381+
.getAsBitmap(file, new BitmapResponseListener() {
382+
@Override
383+
public void onSuccess(@NonNull Headers headers, @NonNull Bitmap bitmap) {
384+
// Do something...
385+
}
386+
387+
@Override
388+
public void onProgress(long downloadBytes, long totalBytes) {
389+
// Do something...
390+
}
391+
392+
@Override
393+
public void onError(@NonNull HttpException e) {
394+
// Do something...
395+
}
396+
});
397+
```
398+
354399
### Download response
355400

356401
**Kotlin Example:**

README_ZH.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ android {
2121
}
2222
2323
dependencies {
24-
implementation 'io.github.af19git5:easy-http-android:0.0.3'
24+
implementation 'io.github.af19git5:easy-http-android:0.0.4'
2525
}
2626
```
2727

@@ -349,6 +349,51 @@ EasyHttp.get(context, url)
349349
});
350350
```
351351

352+
### 取得Response並轉為Bitmap
353+
354+
**Kotlin範例:**
355+
356+
```kotlin
357+
EasyHttp.get(context, url)
358+
.build()
359+
.getAsBitmap(file, object : BitmapResponseListener {
360+
override fun onSuccess(headers: Headers, bitmap: Bitmap) {
361+
// Do something...
362+
}
363+
364+
override fun onProgress(downloadBytes: Long, totalBytes: Long) {
365+
// Do something...
366+
}
367+
368+
override fun onError(e: HttpException) {
369+
// Do something...
370+
}
371+
})
372+
```
373+
374+
**Java範例:**
375+
376+
```java
377+
EasyHttp.get(context, url)
378+
.build()
379+
.getAsBitmap(file, new BitmapResponseListener() {
380+
@Override
381+
public void onSuccess(@NonNull Headers headers, @NonNull Bitmap bitmap) {
382+
// Do something...
383+
}
384+
385+
@Override
386+
public void onProgress(long downloadBytes, long totalBytes) {
387+
// Do something...
388+
}
389+
390+
@Override
391+
public void onError(@NonNull HttpException e) {
392+
// Do something...
393+
}
394+
});
395+
```
396+
352397
### 下載Response
353398

354399
**Kotlin範例:**

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "com.jimmyworks.easyhttpexample"
1111
minSdk rootProject.ext.minSdkVersion
1212
targetSdk rootProject.ext.targetSdkVersion
13-
versionCode 4
14-
versionName "1.0.0"
13+
versionCode 5
14+
versionName "1.0.1"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}

0 commit comments

Comments
 (0)