Skip to content

Commit 5ac46cc

Browse files
committed
Fixed download for API 29+ and minor refactor on older file saveing
1 parent 3cb11e5 commit 5ac46cc

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

widgetssdk/src/main/java/com/glia/widgets/chat/ChatView.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.Intent
88
import android.content.pm.PackageManager
99
import android.content.res.TypedArray
1010
import android.net.Uri
11+
import android.os.Build
1112
import android.provider.MediaStore
1213
import android.provider.Settings
1314
import android.text.Editable
@@ -877,7 +878,7 @@ internal class ChatView(context: Context, attrs: AttributeSet?, defStyleAttr: In
877878
}
878879

879880
override fun onFileDownloadClick(file: AttachmentFile) {
880-
if (ContextCompat.checkSelfPermission(
881+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || ContextCompat.checkSelfPermission(
881882
context, Manifest.permission.WRITE_EXTERNAL_STORAGE
882883
) == PackageManager.PERMISSION_GRANTED
883884
) {

widgetssdk/src/main/java/com/glia/widgets/filepreview/data/source/local/DownloadsFolderDataSource.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,18 @@ private Completable downloadFileToDownloadsAPI29(String fileName, String content
171171

172172
private Completable downloadFileToDownloadsOld(String fileName, InputStream inputStream) {
173173
return Completable.create(emitter -> {
174-
OutputStream fos = null;
175-
try {
176-
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
177-
File file = new File(imagesDir, fileName);
178-
fos = new FileOutputStream(file);
174+
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
175+
File file = new File(imagesDir, fileName);
176+
try (OutputStream fos = new FileOutputStream(file)) {
179177
byte[] buffer = new byte[10 * 1024]; // or other buffer size
180178
int read;
181179
while ((read = inputStream.read(buffer)) != -1) {
182180
fos.write(buffer, 0, read);
183181
}
184-
fos.flush();
185182
emitter.onComplete();
186183
} catch (FileNotFoundException ex) {
187184
Logger.e(TAG, "File saving to downloads folder failed: " + ex.getMessage());
188185
emitter.onError(ex);
189-
} finally {
190-
if (fos != null) {
191-
try {
192-
fos.flush();
193-
fos.close();
194-
} catch (IOException e) {
195-
Logger.e(TAG, e.getMessage());
196-
}
197-
}
198186
}
199187
emitter.onComplete();
200188
});

0 commit comments

Comments
 (0)