Skip to content

Instantly share code, notes, and snippets.

@DharmeshBasapatiBacancy
Created February 13, 2024 12:15
Show Gist options
  • Select an option

  • Save DharmeshBasapatiBacancy/2d211af4e60adbeb1ffed2babafb1c56 to your computer and use it in GitHub Desktop.

Select an option

Save DharmeshBasapatiBacancy/2d211af4e60adbeb1ffed2babafb1c56 to your computer and use it in GitHub Desktop.
Load image stored in App's internal directory
private fun loadImage() {
//val picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
// /storage/emulated/0/Android/data/com.bacancy.ccs2androidhmi/files/
val picturesDirectory = getExternalFilesDir(null) ?: return
// Create a File object representing the desired directory
val directory = File(picturesDirectory.absolutePath)
// Check if the directory exists
if (directory.exists()) {
// List all files in the directory
val files = directory.listFiles()
// Check if there are any files in the directory
if (files != null && files.isNotEmpty()) {
// Load the first image file found in the directory
for (file in files) {
Log.i("TAG", "loadImage1: ${file.absolutePath} && isDirectory = ${file.isDirectory}")
if(file.isDirectory && file.name.equals(FOLDER_NAME)){
val filesFiles = file.listFiles() ?: emptyArray()
for(inFile in filesFiles){
if(inFile.isFile && inFile.name.equals(SPLASH_LOGO_NAME)){
val bitmap = BitmapFactory.decodeFile(inFile.absolutePath)
// Set the Bitmap object to the ImageView
binding.ivCustomLogo.setImageBitmap(bitmap)
}
}
}
}
} else {
// Directory is empty, show a message or handle the case accordingly
Toast.makeText(
this,
"No images found in the Pictures directory",
Toast.LENGTH_SHORT
).show()
}
} else {
// Directory doesn't exist, show a message or handle the case accordingly
Toast.makeText(this, "Pictures directory does not exist", Toast.LENGTH_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment