view app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt @ 30:aacf7a856b5f

Bug fixes, incl for getting "stuck" when file chooser backed out of.
author David Barts <n5jrn@me.com>
date Wed, 24 Feb 2021 19:14:50 -0800
parents 20da616dcda0
children 0023e6013dd9
line wrap: on
line source

package com.bartsent.simpleresizer

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.bartsent.simpleresizer.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    val GET_IMAGE = 1
    private lateinit var binding: ActivityMainBinding
    private var showChooser = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    override fun onResume() {
        super.onResume()
        if (showChooser)
            doShowChooser()
        else
            showChooser = true
    }

    fun doShowChooser(): Unit {
        Intent(Intent.ACTION_GET_CONTENT).run {
            type = "image/*"
            addCategory(Intent.CATEGORY_OPENABLE)
            startActivityForResult(this, GET_IMAGE)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        showChooser = false
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GET_IMAGE) {
            val imageUri = data?.data
            if (resultCode == Activity.RESULT_OK && imageUri != null) {
                Intent(Intent.ACTION_SEND, imageUri, this, EditImage::class.java).run {
                    startActivity(this)
                }
            } else {
                Toast.makeText(applicationContext, "Unable to get image!", Toast.LENGTH_LONG).show()
                doShowChooser()
            }
        }
    }
}