Android 带有和不带有 SD 卡的设备的 /Downloads 文件夹的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11351840/
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 07:07:21  来源:igfitidea点击:

Location of /Downloads folder for devices with and without SD card

android

提问by Eugene

Please let me know what are default locations for Downloadsfolder on devices with and without SD card.

请让我知道Downloads有和没有 SD 卡的设备上文件夹的默认位置是什么。

And how can I check if particular phone doesn't have SD card.

以及如何检查特定手机是否没有 SD 卡。

回答by BlackHatSamurai

To check and see if a device has an SD card, you use: Environment.getExternalStorageState()if you don't have an SD card, you use: Environment.getDataDirectory()

要检查设备是否有 SD 卡,请使用:Environment.getExternalStorageState()如果您没有 SD 卡,请使用:Environment.getDataDirectory()

Essentially, if there is no SD card, you can create your own directory on the device locally. I've listed some code that might help:

本质上,如果没有 SD 卡,您可以在本地设备上创建自己的目录。我列出了一些可能有帮助的代码:

/*
             * This sections checks the phone to see if there is a SD card. if
             * there is an SD card, a directory is created on the SD card to
             * store the test log results. If there is not a SD card, then the
             * directory is created on the phones internal hard drive
             */
                    //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

Hope this helps.

希望这可以帮助。

回答by pogo2065

I do not know the internal download location.

我不知道内部下载位置。

To programmatically obtain the external download location, use:

要以编程方式获取外部下载位置,请使用:

Environment.getExternalStorageDirectory()+"/Downloads/"

Or at least, that is what is it on my HTC EVO 4G.

或者至少,我的 HTC EVO 4G 就是这样。

For my app, i developed a simple class that detected the current state of the SD Card, simplified into human-understood terms. It can be easily done using

对于我的应用程序,我开发了一个简单的类来检测 SD 卡的当前状态,简化为人类可以理解的术语。使用它可以轻松完成

Environment.getExternalStorageState()

and comparing it to the various default states, defined under the Environmentclass. The important ones are Environment.MEDIA_REMOVEDand Environment.MEDIA_SHARED. The first (i believe) tells you if there is any external storage at all and the second tells you if it is mounted or not.

并将其与Environment类下定义的各种默认状态进行比较。重要的是Environment.MEDIA_REMOVEDEnvironment.MEDIA_SHARED。第一个(我相信)告诉您是否有任何外部存储,第二个告诉您它是否已安装。

Note: when comparing the states, use .equals as they are strings, not integers like most state objects in Android.

注意:在比较状态时,请使用 .equals,因为它们是字符串,而不是像 Android 中的大多数状态对象那样的整数。