我是安卓新手,我想把我下载的文件保存到"DIRECTORY_DOCUMENTS“,因为当我保存到downloads文件夹时,它没有在我的应用程序中显示应用程序。以下是代码(当我切换到其他目录,并将我的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;
}
}
发布于 2018-08-02 06:20:59
您将获得所有PDF文件。我看不到任何下载文件的方法。不要紧。如果您下载了新文件但没有看到它,请尝试调用此方法
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file));
sendBroadcast(intent);
https://stackoverflow.com/questions/51625343
复制相似问题