changeset 43:9cb9bb5da247

At long last it auto-deletes old cruft files.
author David Barts <n5jrn@me.com>
date Sat, 10 Apr 2021 17:29:08 -0700
parents 45e4df5226c0
children 2b91619da650
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt
diffstat 1 files changed, 28 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Sat Apr 10 09:02:44 2021 -0700
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Sat Apr 10 17:29:08 2021 -0700
@@ -1,6 +1,7 @@
 package com.bartsent.simpleresizer
 
 import android.Manifest
+import android.content.ContentUris
 import android.content.ContentValues
 import android.content.Intent
 import android.content.pm.PackageManager
@@ -49,7 +50,7 @@
         sort()
     }
 
-    private val IMAGE_TO_SEND = "sent_image.jpg"
+    private val IMAGE_TO_SEND = "simple_resizer_sent_image.jpg"
 
     private lateinit var binding: ActivityEditImageBinding
 
@@ -232,7 +233,7 @@
         }
         val scaleType = PreferenceManager.getDefaultSharedPreferences(applicationContext).getString(
             "scale_type", "speed" )
-        Log.d("EditImage", "scaling, scale_type = $scaleType")
+        // Log.d("EditImage", "scaling, scale_type = $scaleType")
         binding.progressBar.visibility = ProgressBar.VISIBLE
         ThreadPools.WORKERS.execute {
             val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt()
@@ -394,23 +395,33 @@
         val contentValues = makeContentValues(IMAGE_TO_SEND)
 
         // Delete any old file(s)
-        val cols = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q)
-            arrayOf<String>(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE, MediaStore.MediaColumns.RELATIVE_PATH)
-        else
-            arrayOf<String>(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE)
-        val query = StringBuilder()
-        for (col in cols) {
-            if (query.isNotEmpty())
-                query.append(" and ")
-            query.append(col)
-            query.append(" = ?")
+        val cols = arrayOf<String>(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE)
+        val query = StringBuilder().run {
+            for (col in cols) {
+                if (isNotEmpty())
+                    append(" and ")
+                append(col)
+                append(" = ?")
+            }
+            toString()
         }
         try {
-            contentResolver.delete(
+            val cursor = contentResolver.query(
                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
-                query.toString(),
-                cols.map { contentValues.getAsString(it) }.toTypedArray()
-            )
+                arrayOf<String>(MediaStore.MediaColumns._ID),
+                query,
+                cols.map { contentValues.getAsString(it) }.toTypedArray(),
+                null)
+            var deleted = 0
+            cursor?.use {
+                // Log.d("EditImage", "${it.count} entries matched")
+                while (it.moveToNext()) {
+                    val uri =
+                        ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, it.getLong(0))
+                    deleted += contentResolver.delete(uri, null, null)
+                }
+            }
+            // Log.d("EditImage", "$deleted entries deleted")
         } catch (e: Exception) {
             Log.e("EditImage", "unexpected exception when sharing!", e)
         }
@@ -481,7 +492,7 @@
                 val quality = maxOf(0, minOf(100,
                         PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt(
                                 "jpeg_quality", 85)))
-                Log.d("EditImage", "saving, jpeg_quality = $quality")
+                // Log.d("EditImage", "saving, jpeg_quality = $quality")
                 stream.use {
                     if (!image.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
                         throw IOException(getString(R.string.error_save_bitmap))