Last active
December 3, 2021 16:42
-
-
Save mrkrash/191999148991007933ce2664509afe33 to your computer and use it in GitHub Desktop.
Use Android Volley for download file from server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is only a snip code | |
*/ | |
/** | |
* Function that use VolleyFileDownloadRequest on a singletone | |
*/ | |
fun download(url: String, callback: VolleyByteArrayCallback) { | |
Log.d(TAG, "Download File from Server") | |
val volleyFileDownloadRequest = object : VolleyFileDownloadRequest( | |
Method.GET, url, | |
{ response -> callback.onSuccessResponse(response) }, | |
{ error -> Log.e(TAG, "Download Exception", error) } | |
) { | |
@Throws(AuthFailureError::class) | |
override fun getHeaders(): MutableMap<String, String> { | |
val headers = HashMap<String, String>() | |
headers["Authentication: Basic"] = authenticationToken!! | |
return headers | |
} | |
} | |
addToRequestQueue(volleyFileDownloadRequest) | |
} | |
/** | |
* Function that call above function from singletone | |
*/ | |
networkRequest!!.download( | |
DOWNLOAD_URL, | |
object : VolleyByteArrayCallback { | |
override fun onSuccessResponse(response: ByteArray) { | |
val storageDir = getOutputDirectory() | |
try { | |
val destinationFile = File(storageDir, file.get("filename").toString()) | |
val input = ByteArrayInputStream(response) | |
val output = BufferedOutputStream( | |
FileOutputStream(destinationFile) | |
) | |
var bytesAvailable = input.available() | |
val maxBufferSize = 1024 * 1024 | |
var bufferSize = min(bytesAvailable, maxBufferSize) | |
val buffer = ByteArray(bufferSize) | |
var bytesRead = input.read(buffer, 0, bufferSize) | |
while (bytesRead > 0) { | |
output.write(buffer, 0, bufferSize) | |
bytesAvailable = input.available() | |
bufferSize = min(bytesAvailable, maxBufferSize) | |
bytesRead = input.read(buffer, 0, bufferSize) | |
} | |
output.flush() | |
output.close() | |
input.close() | |
} catch (ioexception: IOException) { | |
Log.e(TAG, "Unable to write file", ioexception) | |
} | |
} | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mrkrash | |
import org.json.JSONObject | |
interface VolleyByteArrayCallback { | |
fun onSuccessResponse(response: ByteArray) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mrkrash | |
import com.android.volley.* | |
import com.android.volley.toolbox.HttpHeaderParser | |
open class VolleyFileDownloadRequest( | |
method: Int, | |
url: String, | |
listener: Response.Listener<ByteArray>, | |
errorListener: Response.ErrorListener) : Request<ByteArray>(method, url, errorListener) { | |
private var responseListener: Response.Listener<ByteArray>? = null | |
init { | |
this.responseListener = listener | |
} | |
private var headers: Map<String, String>? = null | |
override fun getHeaders(): MutableMap<String, String> = | |
when(headers) { | |
null -> super.getHeaders() | |
else -> headers!!.toMutableMap() | |
} | |
override fun parseNetworkResponse(response: NetworkResponse): Response<ByteArray> { | |
return try { | |
Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response)) | |
} catch (e: Exception) { | |
Response.error(ParseError(e)) | |
} | |
} | |
override fun deliverResponse(response: ByteArray?) { | |
responseListener?.onResponse(response) | |
} | |
override fun deliverError(error: VolleyError) { | |
errorListener?.onErrorResponse(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment