I'm new to android, and i want to save a Pdf file i downloaded to the "DIRECTORY_DOCUMENTS" , because when i save to downloads folder, it didn't shows app in my app. Here is the code (it works when i change to other directory , and put my pdf in it).
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = findViewById(R.id.lv);
lv.setAdapter(new CustomAdapter(MainActivity.this, getPDFs()));
}
private ArrayList<PDFDoc> getPDFs()
{
ArrayList<PDFDoc> pdfDocs = new ArrayList<>();
//TARGET FOLDER
File downloadsFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
PDFDoc pdfDoc;
if (downloadsFolder.exists()) {
//GET ALL FILES IN DOWNLOAD FOLDER
File[] files = downloadsFolder.listFiles();
//LOOP THRU THOSE FILES GETTING NAME AND URI
for (File file : files) {
if (file.getPath().endsWith("pdf")) {
pdfDoc = new PDFDoc();
pdfDoc.setName(file.getName());
pdfDoc.setPath(file.getAbsolutePath());
pdfDocs.add(pdfDoc);
}
}
}
return pdfDocs;
}
}