79406983

Date: 2025-02-02 17:10:44
Score: 2
Natty:
Report link

Many thanks to @Moe for the help, I finally got this working thanks to him and I want to leave a trace of what works for others because (to me) this was insanely difficult and I still don't fully understand everything about how it works.

I'm only going to show the specific code for remember/mutableStateListOf here i.e. where it goes in the main class and then composable and, for the moment, I'm not worrying about the blocking issue mentioned above - this is a simplest, probably poorly implemented, but working solution. Note that it's also not everything you need, all the compose layout is missing. Here is what's in the mainactivity class:

class MainActivity : ComponentActivity() {

    // get the folder to save images to
    val outputDirectory = getOutputDirectory()

    // read in a list of images in the images folder
    var fileList: ArrayList<File> = imageReaderNew(outputDirectory)

    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            CameraApp1Theme {

                // create a mutablestatelistof and remember it for jetpack compose
                val bitmaps = remember { mutableStateListOf<Bitmap>() }

                // get a list of bitmaps of the images and add it to the bitmaps list
                val loadedBitmaps = getBitmapList(fileList)
                bitmaps.clear()
                bitmaps.addAll(loadedBitmaps)

                BottomSheetScaffold(
                    scaffoldState = scaffoldState,
                    sheetContent = {
                        PhotoBottomSheetContent(
                            // pass bitmaps to the composable
                            bitmaps = bitmaps,
                            modifier = Modifier.fillMaxWidth()
                        )
                    }
                )
                {
                   IconButton(
                        // triggered when the user open the photosheet
                        onClick = {
                            // read in a list of images in the images folder (update)
                            fileList = imageReaderNew(outputDirectory)

                            // get a list of bitmaps of the images and add it to the bitmaps list to update it
                            val loadedBitmaps = getBitmapList(fileList)
                            bitmaps.clear()
                            bitmaps.addAll(loadedBitmaps)
                        }
                    )
                    {
                    }
                }
            }
        }
    }


    // Get output directory for photos
    private fun getOutputDirectory(): File {

        //... get the directory here (internal or external etc.) ...

        // return absolute path to directory
        return fullpath
    }


    // Read in an arraylist of all saved images for this app
    private fun imageReaderNew(root: File): ArrayList<File> {

        //... build the list here ...

        // return filelist arraylist
        return fileList
    }

    private fun getBitmapList(fileList: ArrayList<File>): ArrayList<Bitmap> {

        // set up an empty bitmaps arraylist
        val bitmaps: ArrayList<Bitmap> = ArrayList()

        //... build the list here ...

        // return bitmaps arraylist
        return bitmaps
    }

}

Finally in the composable you will need to declare bitmaps like so:

@Composable
fun PhotoBottomSheetContent(
    //this is the basic working one
    bitmaps: List<Bitmap>
)
{
}
Reasons:
  • Blacklisted phrase (0.5): thanks
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Moe
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: garrettlynchirl