Skip to content

Commit 3dcf456

Browse files
committed
Convert MultimediaDownloader to Kotlin
1 parent 8c441a1 commit 3dcf456

3 files changed

Lines changed: 67 additions & 60 deletions

File tree

app/src/main/java/ai/elimu/content_provider/ui/image/ImagesFragment.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,20 @@ class ImagesFragment : Fragment() {
140140
baseApplication.baseUrl + imageGson.fileUrl
141141
Log.i(TAG, "fileUrl: $fileUrl")
142142
val bytes = MultimediaDownloader.downloadFileBytes(fileUrl)
143-
Log.i(TAG, "bytes.length: " + bytes.size)
144-
145-
// Store the downloaded file in the external storage directory
146-
try {
147-
val fileOutputStream = FileOutputStream(imageFile)
148-
fileOutputStream.write(bytes)
149-
} catch (e: FileNotFoundException) {
150-
Log.e(TAG, null, e)
151-
} catch (e: IOException) {
152-
Log.e(TAG, null, e)
143+
bytes?.let {
144+
Log.i(TAG, "bytes.length: " + bytes.size)
145+
146+
// Store the downloaded file in the external storage directory
147+
try {
148+
val fileOutputStream = FileOutputStream(imageFile)
149+
fileOutputStream.write(bytes)
150+
} catch (e: FileNotFoundException) {
151+
Log.e(TAG, null, e)
152+
} catch (e: IOException) {
153+
Log.e(TAG, null, e)
154+
}
155+
Log.i(TAG, "imageFile.exists(): " + imageFile?.exists())
153156
}
154-
Log.i(TAG, "imageFile.exists(): " + imageFile?.exists())
155157
}
156158

157159
// Store the Image in the database

app/src/main/java/ai/elimu/content_provider/ui/video/VideosFragment.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,20 @@ class VideosFragment : Fragment() {
129129
val fileUrl = videoGson.fileUrl
130130
Log.i(javaClass.name, "fileUrl: $fileUrl")
131131
val bytes = MultimediaDownloader.downloadFileBytes(fileUrl)
132-
Log.i(javaClass.name, "bytes.length: " + bytes.size)
133-
134-
// Store the downloaded file in the external storage directory
135-
try {
136-
val fileOutputStream = FileOutputStream(videoFile)
137-
fileOutputStream.write(bytes)
138-
} catch (e: FileNotFoundException) {
139-
Log.e(javaClass.name, null, e)
140-
} catch (e: IOException) {
141-
Log.e(javaClass.name, null, e)
132+
bytes?.let {
133+
Log.i(javaClass.name, "bytes.length: " + bytes.size)
134+
135+
// Store the downloaded file in the external storage directory
136+
try {
137+
val fileOutputStream = FileOutputStream(videoFile)
138+
fileOutputStream.write(bytes)
139+
} catch (e: FileNotFoundException) {
140+
Log.e(javaClass.name, null, e)
141+
} catch (e: IOException) {
142+
Log.e(javaClass.name, null, e)
143+
}
144+
Log.i(javaClass.name, "videoFile.exists(): " + videoFile.exists())
142145
}
143-
Log.i(javaClass.name, "videoFile.exists(): " + videoFile.exists())
144146
}
145147
}
146148

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
1-
package ai.elimu.content_provider.util;
1+
package ai.elimu.content_provider.util
22

3-
import android.util.Log;
3+
import android.util.Log
4+
import org.apache.commons.io.IOUtils
5+
import java.io.BufferedReader
6+
import java.io.IOException
7+
import java.io.InputStream
8+
import java.io.InputStreamReader
9+
import java.net.HttpURLConnection
10+
import java.net.URL
411

5-
import org.apache.commons.io.IOUtils;
12+
object MultimediaDownloader {
13+
fun downloadFileBytes(urlValue: String): ByteArray? {
14+
Log.i(MultimediaDownloader::class.java.name, "downloadFileBytes")
615

7-
import java.io.BufferedReader;
8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.io.InputStreamReader;
11-
import java.net.HttpURLConnection;
12-
import java.net.URL;
16+
Log.i(MultimediaDownloader::class.java.name, "Downloading from $urlValue")
1317

14-
public class MultimediaDownloader {
15-
16-
public static byte[] downloadFileBytes(String urlValue) {
17-
Log.i(MultimediaDownloader.class.getName(), "downloadFileBytes");
18-
19-
Log.i(MultimediaDownloader.class.getName(), "Downloading from " + urlValue);
20-
21-
byte[] bytes = null;
18+
var bytes: ByteArray? = null
2219

2320
try {
24-
URL url = new URL(urlValue);
25-
26-
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
27-
httpURLConnection.setRequestMethod("GET");
28-
httpURLConnection.connect();
29-
30-
int responseCode = httpURLConnection.getResponseCode();
31-
Log.i(MultimediaDownloader.class.getName(), "responseCode: " + responseCode);
32-
InputStream inputStream = null;
21+
val url = URL(urlValue)
22+
23+
val httpURLConnection = url.openConnection() as HttpURLConnection
24+
httpURLConnection.requestMethod = "GET"
25+
httpURLConnection.connect()
26+
27+
val responseCode = httpURLConnection.responseCode
28+
Log.i(
29+
MultimediaDownloader::class.java.name,
30+
"responseCode: $responseCode"
31+
)
32+
var inputStream: InputStream? = null
3333
if (responseCode == 200) {
34-
inputStream = httpURLConnection.getInputStream();
35-
bytes = IOUtils.toByteArray(inputStream);
34+
inputStream = httpURLConnection.inputStream
35+
bytes = IOUtils.toByteArray(inputStream)
3636
} else {
37-
inputStream = httpURLConnection.getErrorStream();
38-
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
39-
String response = "";
40-
String line;
41-
while ((line = bufferedReader.readLine()) != null) {
42-
response += line;
37+
inputStream = httpURLConnection.errorStream
38+
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
39+
var response = ""
40+
var line: String
41+
while ((bufferedReader.readLine().also { line = it }) != null) {
42+
response += line
4343
}
44-
Log.e(MultimediaDownloader.class.getName(), "responseCode: " + responseCode + ", response: " + response);
44+
Log.e(
45+
MultimediaDownloader::class.java.name,
46+
"responseCode: $responseCode, response: $response"
47+
)
4548
}
46-
} catch (IOException e) {
47-
Log.e(MultimediaDownloader.class.getName(), null, e);
49+
} catch (e: IOException) {
50+
Log.e(MultimediaDownloader::class.java.name, null, e)
4851
}
4952

50-
return bytes;
53+
return bytes
5154
}
5255
}

0 commit comments

Comments
 (0)