annotate app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 29:eec88eba58e9

Fix receiving from another app.
author David Barts <n5jrn@me.com>
date Wed, 24 Feb 2021 18:44:47 -0800
parents 648908f492c0
children aacf7a856b5f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package com.bartsent.simpleresizer
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
3 import android.content.ContentValues
15
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
4 import android.content.Intent
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
5 import android.graphics.Bitmap
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import android.graphics.BitmapFactory
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
7 import android.graphics.Canvas
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
8 import android.graphics.Matrix
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import android.net.Uri
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import android.os.Bundle
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
11 import android.os.Environment
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
12 import android.provider.MediaStore
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
13 import android.provider.OpenableColumns
15
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
14 import android.util.Log
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
15 import android.view.Menu
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
16 import android.view.MenuItem
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 import android.view.View
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
18 import android.widget.EditText
10
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
19 import android.widget.ProgressBar
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
20 import android.widget.Toast
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
21 import androidx.appcompat.app.AlertDialog
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
22 import androidx.appcompat.app.AppCompatActivity
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
23 import androidx.appcompat.widget.PopupMenu
22
c29f941d09cd Add landscape mode.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
24 import androidx.preference.PreferenceManager
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 import com.bartsent.simpleresizer.databinding.ActivityEditImageBinding
22
c29f941d09cd Add landscape mode.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
26 import com.bartsent.simpleresizer.lib.ThreadPools
6
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
27 import com.bartsent.simpleresizer.lib.getScaledInstance
5
247e03baf77c Fix rotation.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
28 import java.io.File
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
29 import java.io.IOException
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
30
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 class EditImage : AppCompatActivity() {
5
247e03baf77c Fix rotation.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
32 private object State {
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
33 var uri: Uri? = null
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
34 var bitmap: Bitmap? = null
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
35 }
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
36
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
37 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
38 sort()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
39 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
40
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 private lateinit var binding: ActivityEditImageBinding
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
42
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 override fun onCreate(savedInstanceState: Bundle?) {
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 super.onCreate(savedInstanceState)
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 binding = ActivityEditImageBinding.inflate(layoutInflater)
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 setContentView(binding.root)
15
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
47 PreferenceManager.setDefaultValues(applicationContext, R.xml.root_preferences, false)
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
48 }
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
49
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
50 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
51 menuInflater.inflate(R.menu.menu_edit, menu)
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
52 return super.onCreateOptionsMenu(menu)
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
53 }
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
54
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
55 override fun onOptionsItemSelected(item: MenuItem): Boolean {
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
56 if (item.itemId == R.id.settings_item) {
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
57 startActivity(
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
58 Intent(Intent.ACTION_APPLICATION_PREFERENCES, null, this,
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
59 SettingsActivity::class.java))
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
60 return true
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
61 }
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
62 return false
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 }
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
64
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
65 // Cribbed from: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
66 private fun getFileName(uri: Uri): String? {
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
67 var result: String? = null
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
68 if (uri.scheme == "content") {
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
69 contentResolver.query(uri, null, null, null, null).use { cursor ->
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
70 if (cursor != null && cursor.moveToFirst())
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
71 result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
72 }
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
73 }
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
74 if (result == null) {
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
75 val uriPath = uri.path
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
76 result = uriPath?.substring(uriPath.lastIndexOf('/') + 1)
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
77 }
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
78 return result
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
79 }
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
80
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
81 private fun showFatalError(message: String): Unit {
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
82 AlertDialog.Builder(this).also {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
83 it.setMessage(message)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
84 it.setNeutralButton(R.string.ok_text) { dialog, _ ->
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
85 dialog.dismiss()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
86 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
87 it.setOnDismissListener {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
88 finish()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
89 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
90 it.create()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
91 }.show()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
92 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
93
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 override fun onResume() {
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 super.onResume()
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
96
29
eec88eba58e9 Fix receiving from another app.
David Barts <n5jrn@me.com>
parents: 25
diff changeset
97 Log.d("EditImage", "intent action = ${intent?.action ?: "(none)"}")
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
98 // Read the URI, die if we can't.
29
eec88eba58e9 Fix receiving from another app.
David Barts <n5jrn@me.com>
parents: 25
diff changeset
99 val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 if (imageUri == null) {
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
101 if (State.bitmap == null)
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
102 showFatalError(getString(R.string.error_no_uri))
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
103 else
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
104 setImage(State.bitmap!!)
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
105 return
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 }
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
107
21
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
108 // User has opened a new image.
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
109 if (imageUri != State.uri) {
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
110 State.uri = imageUri
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
111 binding.progressBar.visibility = ProgressBar.VISIBLE
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
112 ThreadPools.WORKERS.execute {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
113 State.bitmap = contentResolver.openInputStream(imageUri).use {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
114 BitmapFactory.decodeStream(it)
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
115 }
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
116 runOnUiThread {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
117 binding.progressBar.visibility = ProgressBar.INVISIBLE
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
118 if (State.bitmap == null)
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
119 showFatalError(getString(R.string.error_bad_image))
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
120 else
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
121 setImage(State.bitmap!!)
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
122 }
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
123 }
21
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
124 return
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
125 }
21
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
126
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
127 // Rotation (of the phone).
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
128 val oldBitmap = State.bitmap
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
129 if (oldBitmap != null)
7e7e71724770 No longer breaks when phone rotated.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
130 setImage(oldBitmap)
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
131 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
132
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
133 private fun setImage(image: Bitmap): Unit {
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
134 binding.imageSize.text = getString(R.string.image_size_text, image.width, image.height)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
135 binding.image.setImageBitmap(image)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
136 State.bitmap = image
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
137 binding.root.invalidate()
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
138 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
139
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
140 private val CUSTOM = 999998
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
141 private val CANCEL = 999999
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
142
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
143 fun scaleClicked(view: View): Unit {
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
144 val (maxSize, horizontal) = State.bitmap!!.run {
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
145 if (width > height) Pair(width, true) else Pair(height, false)
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
146 }
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
147 PopupMenu(this, view).apply {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
148 menu.run {
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
149 STDDIMS.filter { it < maxSize }.forEach { major ->
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
150 val minor = major * 3 / 4
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
151 add(Menu.NONE, major, Menu.NONE,
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
152 if (horizontal) "$major ✕ $minor" else "$minor ✕ $major")
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
153 }
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
154 add(Menu.NONE, CUSTOM, Menu.NONE, R.string.custom_text)
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
155 add(Menu.NONE, CANCEL, Menu.NONE, R.string.cancel_text)
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
156 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
157 setOnMenuItemClickListener(::scaleMenuItemClicked)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
158 show()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
159 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
160 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
161
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
162 private fun scaleMenuItemClicked(item: MenuItem) : Boolean =
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
163 when (item.itemId) {
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
164 CUSTOM -> { showCustomScaleDialog(); true }
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
165 CANCEL -> true
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
166 in STDDIMS -> { doScale(item.itemId); true }
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
167 else -> false
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
168 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
169
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
170 private fun copyColorSpace(old: Bitmap, new: Bitmap): Unit {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
171 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
172 val oldColorSpace = old.colorSpace
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
173 if (oldColorSpace != null)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
174 new.setColorSpace(oldColorSpace)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
175 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
176 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
177
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
178 private fun doScale(newMax: Int): Unit {
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
179 val oldBitmap = State.bitmap!!
6
e8059b166de1 Lanczos works, but is painfully slow.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
180 val factor = newMax.toDouble() / maxOf(oldBitmap.width, oldBitmap.height).toDouble()
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
181 if (factor >= 1.0) {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
182 throw IllegalArgumentException("can only scale down")
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
183 }
15
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
184 val scaleType = PreferenceManager.getDefaultSharedPreferences(applicationContext).getString(
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
185 "scale_type", "speed" )
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
186 Log.d("EditImage", "scaling, scale_type = $scaleType")
10
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
187 binding.progressBar.visibility = ProgressBar.VISIBLE
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
188 ThreadPools.WORKERS.execute {
15
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
189 val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt()
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
190 val newHeight = (oldBitmap.height.toDouble() * factor + 0.5).toInt()
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
191 val newBitmap = if (scaleType == "quality")
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
192 oldBitmap.getScaledInstance(newWidth, newHeight)
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
193 else
20da616dcda0 Add preferences.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
194 Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true)
10
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
195 runOnUiThread {
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
196 binding.progressBar.visibility = ProgressBar.INVISIBLE
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
197 setImage(newBitmap)
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
198 }
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
199 }
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
200 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
201
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
202 private fun showCustomScaleDialog(): Unit {
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
203 val image = State.bitmap!!
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
204 val curMaxDim = maxOf(image.width, image.height)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
205 val dialogView = layoutInflater.inflate(R.layout.dialog_custom_scale, null)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
206 AlertDialog.Builder(this).also {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
207 it.setPositiveButton(R.string.ok_text) { dialog, _ ->
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
208 val maxDim = dialogView.findViewById<EditText>(R.id.custom_scale)?.text.toString().toIntOrNull()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
209 dialog.dismiss()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
210 if (maxDim == null || maxDim < 8 || maxDim >= curMaxDim) {
16
3ed74dc0e34a Fix error messages, make scaling menu more user-friendly, fix return from settings.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
211 Toast.makeText(applicationContext, R.string.bad_scale, Toast.LENGTH_LONG).show()
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
212 } else {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
213 doScale(maxDim)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
214 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
215 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
216 it.setNegativeButton(R.string.cancel_text) { dialog, _ ->
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
217 dialog.dismiss()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
218 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
219 it.setView(dialogView)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
220 it.create()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
221 }.show()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
222 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
223
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
224 fun rotateClicked(view: View): Unit {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
225 PopupMenu(this, view).apply {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
226 setOnMenuItemClickListener {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
227 when(it.itemId) {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
228 R.id.r_90_cw -> {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
229 doRotate(90)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
230 true
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
231 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
232 R.id.r_180 -> {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
233 doRotate(180)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
234 true
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
235 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
236 R.id.r_90_ccw -> {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
237 doRotate(270)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
238 true
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
239 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
240 R.id.r_cancel -> true
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
241 else -> false
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
242 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
243 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
244 inflate(R.menu.rotate)
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
245 show()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
246 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
247 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
248
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
249 private fun doRotate(deg: Int): Unit {
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
250 val oldBitmap = State.bitmap!!
5
247e03baf77c Fix rotation.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
251 if (deg % 90 != 0) {
247e03baf77c Fix rotation.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
252 throw IllegalArgumentException("$deg not a multiple of 90")
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
253 }
5
247e03baf77c Fix rotation.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
254 val (w, h) = if (deg % 180 == 0) Pair(oldBitmap.width, oldBitmap.height) else Pair(oldBitmap.height, oldBitmap.width)
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
255 val rotater = Matrix().apply {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
256 setRotate(deg.toFloat(), oldBitmap.width.toFloat()/2.0f, oldBitmap.height.toFloat()/2.0f)
5
247e03baf77c Fix rotation.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
257 postTranslate((w - oldBitmap.width).toFloat()/2.0f, (h - oldBitmap.height).toFloat()/2.0f)
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
258 }
10
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
259 binding.progressBar.visibility = ProgressBar.VISIBLE
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
260 ThreadPools.WORKERS.execute {
11
678adef4774f Move more stuff into worker threads.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
261 val newBitmap = Bitmap.createBitmap(w, h, oldBitmap.config)
678adef4774f Move more stuff into worker threads.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
262 copyColorSpace(oldBitmap, newBitmap)
10
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
263 Canvas(newBitmap).run {
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
264 drawBitmap(oldBitmap, rotater, null)
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
265 }
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
266 runOnUiThread {
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
267 binding.progressBar.visibility = ProgressBar.INVISIBLE
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
268 setImage(newBitmap)
5626557ac542 Add progress bar and a worker thread. Still way slow.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
269 }
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
270 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
271 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
272
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
273 fun cancelClicked(view: View): Unit {
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
274 State.uri = null
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
275 State.bitmap = null
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
276 finish()
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
277 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
278
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
279 fun doneClicked(view: View): Unit {
25
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
280 val image = State.bitmap!!
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
281 binding.progressBar.visibility = ProgressBar.VISIBLE
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
282 ThreadPools.WORKERS.execute {
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
283 val contentValues = ContentValues().apply {
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
284 var fileName = getFileName(State.uri!!)
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
285 if (fileName == null) {
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
286 val d = java.util.Date()
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
287 fileName = "IMG_%tY%tm%td_%tH%tM%tS".format(d, d, d, d, d, d)
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
288 }
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
289 val dot = fileName.lastIndexOf('.')
25
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
290 if (dot != -1)
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
291 fileName = fileName.substring(0, dot)
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
292 fileName = "${fileName}_${image.width}x${image.height}.jpg"
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
293 put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
294 put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
295 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
296 put(MediaStore.MediaColumns.RELATIVE_PATH,
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
297 File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).path)
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
298 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
299 }
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
300 var errorMessage: String? = null
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
301 try {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
302 val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
303 if (myUri == null) {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
304 throw IOException(getString(R.string.error_create_mediastore))
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
305 }
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
306 val stream = contentResolver.openOutputStream(myUri)
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
307 if (stream == null) {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
308 throw IOException(getString(R.string.error_get_output))
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
309 }
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
310 val quality = maxOf(0, minOf(100,
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
311 PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt(
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
312 "jpeg_quality", 85)))
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
313 Log.d("EditImage", "saving, jpeg_quality = $quality")
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
314 stream.use {
25
648908f492c0 Fix output file names.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
315 if (!image.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
316 throw IOException(getString(R.string.error_save_bitmap))
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
317 }
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
318 }
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
319 } catch (ioe: IOException) {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
320 errorMessage = ioe.message ?: getString(R.string.error_io)
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
321 }
18
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
322 runOnUiThread {
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
323 binding.progressBar.visibility = ProgressBar.INVISIBLE
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
324 if (errorMessage == null)
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
325 finish()
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
326 else
eedf995462d9 Parallalized, but ConstraintLayout started hosing the edit window.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
327 Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show()
2
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
328 }
06825e49f7aa Got it scaling and rotating, needs settings, etc.
David Barts <n5jrn@me.com>
parents: 1
diff changeset
329 }
0
13935000c2d9 Stuff Andriod Studio ignored for some reason.
David Barts <n5jrn@me.com>
parents:
diff changeset
330 }
1
f26f61a8a9ad Make the EditImage activity stateful.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
331 }