【问题标题】:Save downloaded file to Android DIRECTORY_DOCUMENTS将下载的文件保存到 Android DIRECTORY_DOCUMENTS
【发布时间】:2018-08-01 04:27:48
【问题描述】:

我是 android 新手,我想将下载的 Pdf 文件保存到“DIRECTORY_DOCUMENTS”,因为当我保存到下载文件夹时,它没有在我的应用程序中显示应用程序。 这是代码(当我切换到其他目录并将我的pdf放入其中时它可以工作)。

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

}

【问题讨论】:

    标签: android-studio directory download-manager


    【解决方案1】:

    您将获得所有 PDF 文件。我没有看到任何下载文件的方法。没关系。如果您下载新文件但没有看到它,请尝试调用此方法

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file));
    sendBroadcast(intent);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 2016-04-09
      相关资源
      最近更新 更多