0

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;
}

}

Krova
  • 61
  • 1
  • 4

1 Answers1

0

You are getting all PDF files. I dont see any method to download file. Nevermind. If you download new file and you dont see it try call this method

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file));
sendBroadcast(intent);
Oktawian
  • 334
  • 1
  • 2
  • 16