In my code I have a ListView that lists the pdf files in the "Documents" folder on the phone that is updated when you create one, but the ones that have been created previously do not appear, they are still in the documents folder and I can see them by entering the files on the phone , but they don't appear from the app.
This is my code:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var pdfFileNames = arrayOf<String>() // Lista de nombres de archivos PDF
private lateinit var pdfAdapter: ArrayAdapter<String> // Adaptador para el ListView
private lateinit var listView: ListView
private val documentsDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
@SuppressLint("MissingInflatedId")
@Override
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("MainActivity", "Iniciando la cámara")
// CONFIGURAMOS VIEWBINDING
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Inicializa los elementos de la interfaz
listView = binding.listaPdf
// Inicializa el adaptador para el ListView
pdfAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, pdfFileNames)
listView.adapter = pdfAdapter
listView.setOnItemClickListener { parent, view, position, id ->
val selectedFileName = pdfFileNames[position] // Obtiene el nombre del archivo PDF seleccionado
val selectedFile = File(documentsDirectory, selectedFileName) // Obtiene el archivo PDF correspondiente
openPdf(selectedFile) // Abre el archivo PDF seleccionado
}
}
override fun onResume() {
super.onResume()
loadPDFs()
}
//LISTA PDF
// Función para cargar los nombres de archivos PDF
private fun loadPDFs() {
if (documentsDirectory.exists() && documentsDirectory.isDirectory) {
pdfFileNames = documentsDirectory
.listFiles { file -> file.isFile && file.name.toLowerCase().endsWith(".pdf") }
.map { it.name }
.toTypedArray()
if(pdfFileNames.isNotEmpty()){
// Actualiza el adaptador del ListView
pdfAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, pdfFileNames)
listView.adapter = pdfAdapter
}else{
Toast.makeText(this, "No se encontraron archivos pdf en la carpeta Documentos",Toast.LENGTH_SHORT).show()
}
}
}
//PDF
private fun openPdf(pdfFile: File) {
val intent = Intent(this, VisorPDF::class.java)
intent.putExtra("pdf_path", pdfFile.absolutePath)
startActivity(intent)
}
I tried to call the function loadPDFs on the onCreate and onResume functions but it doesn`t works
EDIT 1: I have tried adding a log to see how many files it detects within the "/Documents" folder and it only counts the new files, with a total of 11 (8 from the previous installation and 3 pdf created from the new installation)
EDIT 2: I have checked if having the phone in Spanish language changes the name of the "/Documents" folder to "Documentos" but that does not happen, and both new and old files have the same path
Your app can only see files in
Documents/that this app installation created. If the user uninstalls and reinstalls your app, your newly-installed app installation cannot see the files created inDocuments/from your prior app installation.You may be better served using
ACTION_OPEN_DOCUMENT/ActivityResultContracts.OpenDocument, as that allows the user to access everything inDocuments/, and elsewhere.