List PDFs from selected folder in Android App
In this video it shows the Android app code to list all the PDF files from a folder picked by the user in Android App.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Details:
package com.programmerworld.pdfreader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> pdfFilesList;
private ArrayAdapter<String> adapter;
// Launcher for SAF to pick the Downloads directory
private final ActivityResultLauncher<Intent> directoryPickerLauncher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if ((result.getResultCode() == RESULT_OK) && (result.getData() != null)) {
Uri treeUri = result.getData().getData();
if (treeUri != null) {
// Persist the permission across restarts
getContentResolver().takePersistableUriPermission(
treeUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
);
listPdfFiles(treeUri);
}
} else {
Toast.makeText(this, "No directory selected", Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
Button btnListPdfs = findViewById(R.id.btnListPdfs);
pdfFilesList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pdfFilesList);
listView.setAdapter(adapter);
btnListPdfs.setOnClickListener(v -> openDownloadsDirectoryPicker());
}
private void openDownloadsDirectoryPicker() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Optionally, suggest the Downloads directory (not guaranteed to work on all devices)
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
Uri.parse("content://com.android.externalstorage.documents/document/primary:Download"));
directoryPickerLauncher.launch(intent);
}
private void listPdfFiles(Uri treeUri) {
pdfFilesList.clear();
try {
// Query the document tree
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
treeUri,
DocumentsContract.getTreeDocumentId(treeUri)
);
Cursor cursor = getContentResolver().query(
childrenUri,
new String[]{
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_MIME_TYPE
},
null,
null,
null
);
if (cursor != null) {
while (cursor.moveToNext()) {
String fileName = cursor.getString(0); // COLUMN_DISPLAY_NAME
String mimeType = cursor.getString(1); // COLUMN_MIME_TYPE
if ("application/pdf".equals(mimeType)) {
pdfFilesList.add(fileName);
}
}
cursor.close();
}
if (pdfFilesList.isEmpty()) {
Toast.makeText(this, "No PDF files found", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Found " + pdfFilesList.size() + " PDF files", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
adapter.notifyDataSetChanged();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/btnListPdfs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="List PDF Files from Downloads" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
No changes required in Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PDFReader"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Screenshots:







