如何在Android的下载目录中获取每个文件的完整路径和名称 [英] How to get complete path and name of each file in Download directory in Android

查看:56
本文介绍了如何在Android的下载目录中获取每个文件的完整路径和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要开发一个应用程序,用户可以在其中从下载目录中选择文件.现在,我需要用户选择的文件的路径+名称.

I am going to develop an app, where user can choose File from a Download directory. Now i need the path + name of the file, which the user has chosen.

DownloadManager dm = (DownlaodManager) getSystemService(DOWNLOAD_SERVICE);

通过这种方式,用户可以选择文件.现在,我需要选择文件的绝对路径.但是这里的问题是,如果用户选择一个文件,该文件将处于打开状态(这不应该发生)

By this way the user can choose the file. Now i need the absoloute path of the file, which is choosen. But the problem here is, if the user chosse a file, the file will be open(this shouldn't be happen)

你们中的任何人都可以帮我吗?

Can anyone of you help me with that?

推荐答案

使用, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 访问文件夹,然后 File.listFiles()访问该目录中的单个文件.

Use, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); to access the folder, and File.listFiles() to access individual files from that directory.

看这个简单的例子,

编写一种方法来浏览和列出 Downloads 目录中的每个文件,如果在 Downloads 文件夹中找到目录,请同时列出其文件

write a method to browse and list each file from Downloads directory, and if a directory is found in Downloads folder, list its files too,

public void getFilesFromDir(File filesFromSD) {

    File listAllFiles[] = filesFromSD.listFiles();

    if (listAllFiles != null && listAllFiles.length > 0) {
        for (File currentFile : listAllFiles) {
            if (currentFile.isDirectory()) {
                getFilesFromDir(currentFile);
            } else {
                if (currentFile.getName().endsWith("")) {
                    // File absolute path
                    Log.e("File path", currentFile.getAbsolutePath());
                    // File Name
                    Log.e("File path", currentFile.getName());

                }
            }
        }
    }
}

获取您的 Downloads 目录的路径,并将其作为参数传递给上述方法,

Get the path of your Downloads directory and pass it as parameter in your above method,

File downloadDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());

getFilesFromDir(downloadDir);

这篇关于如何在Android的下载目录中获取每个文件的完整路径和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆