如何获取我的 Android 设备内部下载文件夹路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21724706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:58:24  来源:igfitidea点击:

How to get my Android device Internal Download Folder path

androiddownloadstorageinternal

提问by Andrey

It is possible to get the Android device Internal Download Folder path?

是否可以获取 Android 设备内部下载文件夹路径?

回答by Dinithe Pieris

if a device has an SD card, you use:

如果设备有 SD 卡,您可以使用:

Environment.getExternalStorageState() 

if you don't have an SD card, you use:

如果您没有 SD 卡,您可以使用:

Environment.getDataDirectory()

if there is no SD card, you can create your own directory on the device locally.

如果没有SD卡,您可以在本地设备上创建自己的目录。

    //if there is no SD card, create new directory objects to make directory on device
        if (Environment.getExternalStorageState() == null) {
                        //create new file directory object
            directory = new File(Environment.getDataDirectory()
                    + "/RobotiumTestLog/");
            photoDirectory = new File(Environment.getDataDirectory()
                    + "/Robotium-Screenshots/");
            /*
             * this checks to see if there are any previous test photo files
             * if there are any photos, they are deleted for the sake of
             * memory
             */
            if (photoDirectory.exists()) {
                File[] dirFiles = photoDirectory.listFiles();
                if (dirFiles.length != 0) {
                    for (int ii = 0; ii <= dirFiles.length; ii++) {
                        dirFiles[ii].delete();
                    }
                }
            }
            // if no directory exists, create new directory
            if (!directory.exists()) {
                directory.mkdir();
            }

            // if phone DOES have sd card
        } else if (Environment.getExternalStorageState() != null) {
            // search for directory on SD card
            directory = new File(Environment.getExternalStorageDirectory()
                    + "/RobotiumTestLog/");
            photoDirectory = new File(
                    Environment.getExternalStorageDirectory()
                            + "/Robotium-Screenshots/");
            if (photoDirectory.exists()) {
                File[] dirFiles = photoDirectory.listFiles();
                if (dirFiles.length > 0) {
                    for (int ii = 0; ii < dirFiles.length; ii++) {
                        dirFiles[ii].delete();
                    }
                    dirFiles = null;
                }
            }
            // if no directory exists, create new directory to store test
            // results
            if (!directory.exists()) {
                directory.mkdir();
            }
        }// end of SD card checking

add permissions on your manifest.xml

在 manifest.xml 上添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Happy coding..

快乐编码..

回答by Nayan Rath

To find locations on internal storage for your app, use getFilesDir(), called on any Context (such as your Activity, to get a File object.

要为您的应用程序查找内部存储上的位置,请使用getFilesDir(),在任何上下文(例如您的活动)上调用以获取 File 对象。

why can't you use Environment.getExternalStorageDirectory()for getting /storage/sdcard0/ path from code ?

为什么不能使用Environment.getExternalStorageDirectory()从代码中获取 /storage/sdcard0/ 路径?

need to add permissions as well.

还需要添加权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope U will get your Solution...

希望你能得到你的解决方案......