comparison app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 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
comparison
equal deleted inserted replaced
42:45e4df5226c0 43:9cb9bb5da247
1 package com.bartsent.simpleresizer 1 package com.bartsent.simpleresizer
2 2
3 import android.Manifest 3 import android.Manifest
4 import android.content.ContentUris
4 import android.content.ContentValues 5 import android.content.ContentValues
5 import android.content.Intent 6 import android.content.Intent
6 import android.content.pm.PackageManager 7 import android.content.pm.PackageManager
7 import android.graphics.Bitmap 8 import android.graphics.Bitmap
8 import android.graphics.BitmapFactory 9 import android.graphics.BitmapFactory
47 48
48 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply { 49 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply {
49 sort() 50 sort()
50 } 51 }
51 52
52 private val IMAGE_TO_SEND = "sent_image.jpg" 53 private val IMAGE_TO_SEND = "simple_resizer_sent_image.jpg"
53 54
54 private lateinit var binding: ActivityEditImageBinding 55 private lateinit var binding: ActivityEditImageBinding
55 56
56 override fun onCreate(savedInstanceState: Bundle?) { 57 override fun onCreate(savedInstanceState: Bundle?) {
57 super.onCreate(savedInstanceState) 58 super.onCreate(savedInstanceState)
230 if (factor >= 1.0) { 231 if (factor >= 1.0) {
231 throw IllegalArgumentException("can only scale down") 232 throw IllegalArgumentException("can only scale down")
232 } 233 }
233 val scaleType = PreferenceManager.getDefaultSharedPreferences(applicationContext).getString( 234 val scaleType = PreferenceManager.getDefaultSharedPreferences(applicationContext).getString(
234 "scale_type", "speed" ) 235 "scale_type", "speed" )
235 Log.d("EditImage", "scaling, scale_type = $scaleType") 236 // Log.d("EditImage", "scaling, scale_type = $scaleType")
236 binding.progressBar.visibility = ProgressBar.VISIBLE 237 binding.progressBar.visibility = ProgressBar.VISIBLE
237 ThreadPools.WORKERS.execute { 238 ThreadPools.WORKERS.execute {
238 val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt() 239 val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt()
239 val newHeight = (oldBitmap.height.toDouble() * factor + 0.5).toInt() 240 val newHeight = (oldBitmap.height.toDouble() * factor + 0.5).toInt()
240 val newBitmap = if (scaleType == "quality") 241 val newBitmap = if (scaleType == "quality")
392 393
393 // If we get here, we have permission to save (if we need it). 394 // If we get here, we have permission to save (if we need it).
394 val contentValues = makeContentValues(IMAGE_TO_SEND) 395 val contentValues = makeContentValues(IMAGE_TO_SEND)
395 396
396 // Delete any old file(s) 397 // Delete any old file(s)
397 val cols = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) 398 val cols = arrayOf<String>(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE)
398 arrayOf<String>(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE, MediaStore.MediaColumns.RELATIVE_PATH) 399 val query = StringBuilder().run {
399 else 400 for (col in cols) {
400 arrayOf<String>(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.MIME_TYPE) 401 if (isNotEmpty())
401 val query = StringBuilder() 402 append(" and ")
402 for (col in cols) { 403 append(col)
403 if (query.isNotEmpty()) 404 append(" = ?")
404 query.append(" and ") 405 }
405 query.append(col) 406 toString()
406 query.append(" = ?")
407 } 407 }
408 try { 408 try {
409 contentResolver.delete( 409 val cursor = contentResolver.query(
410 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 410 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
411 query.toString(), 411 arrayOf<String>(MediaStore.MediaColumns._ID),
412 cols.map { contentValues.getAsString(it) }.toTypedArray() 412 query,
413 ) 413 cols.map { contentValues.getAsString(it) }.toTypedArray(),
414 null)
415 var deleted = 0
416 cursor?.use {
417 // Log.d("EditImage", "${it.count} entries matched")
418 while (it.moveToNext()) {
419 val uri =
420 ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, it.getLong(0))
421 deleted += contentResolver.delete(uri, null, null)
422 }
423 }
424 // Log.d("EditImage", "$deleted entries deleted")
414 } catch (e: Exception) { 425 } catch (e: Exception) {
415 Log.e("EditImage", "unexpected exception when sharing!", e) 426 Log.e("EditImage", "unexpected exception when sharing!", e)
416 } 427 }
417 428
418 // Save new file, use it to share data. 429 // Save new file, use it to share data.
479 throw IOException(getString(R.string.error_get_output)) 490 throw IOException(getString(R.string.error_get_output))
480 } 491 }
481 val quality = maxOf(0, minOf(100, 492 val quality = maxOf(0, minOf(100,
482 PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt( 493 PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt(
483 "jpeg_quality", 85))) 494 "jpeg_quality", 85)))
484 Log.d("EditImage", "saving, jpeg_quality = $quality") 495 // Log.d("EditImage", "saving, jpeg_quality = $quality")
485 stream.use { 496 stream.use {
486 if (!image.compress(Bitmap.CompressFormat.JPEG, quality, it)) { 497 if (!image.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
487 throw IOException(getString(R.string.error_save_bitmap)) 498 throw IOException(getString(R.string.error_save_bitmap))
488 } 499 }
489 } 500 }