From 7b4effe8893414a5fbfa6757daa0f29f1c19588b Mon Sep 17 00:00:00 2001 From: doteq Date: Tue, 7 Sep 2021 22:11:29 +0200 Subject: [PATCH] [UI] Fix block timetable export. (#57) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use MediaStore on SDK level >= 29 for timetable export * Fix imports * Use subdirectory in the Photos folder * Use MediaStore for all API levels * Remove not-null assertion * Remove unnecessary outputStream close. * Use File constructor for directory path * Use File(File, String) constructor Co-authored-by: Kuba SzczodrzyƄski --- .../timetable/GenerateBlockTimetableDialog.kt | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/GenerateBlockTimetableDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/GenerateBlockTimetableDialog.kt index c93d5a6e..9eefffd0 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/GenerateBlockTimetableDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/GenerateBlockTimetableDialog.kt @@ -4,11 +4,14 @@ package pl.szczodrzynski.edziennik.ui.dialogs.timetable +import android.content.ContentResolver +import android.content.ContentValues import android.content.Intent import android.graphics.* import android.net.Uri import android.os.Build import android.os.Environment +import android.provider.MediaStore import android.util.Log import android.view.View import android.view.View.MeasureSpec @@ -373,25 +376,31 @@ class GenerateBlockTimetableDialog( val today = Date.getToday().stringY_m_d val now = Time.getNow().stringH_M_S + val filename = "plan_lekcji_${app.profile.name}_${today}_${now}.png" + val resolver: ContentResolver = activity.applicationContext.contentResolver + val values = ContentValues() + values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png") - val outputDir = Environment.getExternalStoragePublicDirectory("Szkolny.eu").apply { mkdirs() } - val outputFile = File(outputDir, "plan_lekcji_${app.profile.name}_${today}_${now}.png") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + values.put(MediaStore.MediaColumns.DISPLAY_NAME, filename) + values.put(MediaStore.MediaColumns.RELATIVE_PATH, File(Environment.DIRECTORY_PICTURES, "Szkolny.eu").path) + } else { + val picturesDirectory = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Szkolny.eu") + picturesDirectory.mkdirs() + values.put(MediaStore.MediaColumns.DATA, File(picturesDirectory, filename).path) + } try { - val fos = FileOutputStream(outputFile) - bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos) - fos.close() + val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) ?: return@withContext null + resolver.openOutputStream(uri).use { + bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) + } + uri } catch (e: Exception) { Log.e("SAVE_IMAGE", e.message, e) return@withContext null } - val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - FileProvider.getUriForFile(activity, app.packageName + ".provider", outputFile) - } else { - Uri.parse("file://" + outputFile.absolutePath) - } - uri } progressDialog.dismiss()