简体   繁体   English

(已解决)如何为Android 的getExternalStorageState() 指定文件路径?

[英](Solved) How do I specify file path for Android's getExternalStorageState()?

I'm completely new to Android development and would like to index a file from the Android phone's external storage.我对 Android 开发完全陌生,想从 Android 手机的外部存储中索引文件。 I found some old code which used a deprecated method:我发现了一些使用过时方法的旧代码:

String file1 = Environment.getExternalStorageDirectory() + "/folder/file.blah";

I'm trying to rewrite this line and tried the Environment.getExternalFilesdir method but got a "cannot find symbol method" error.我正在尝试重写这一行并尝试使用Environment.getExternalFilesdir方法,但出现“找不到符号方法”错误。 The only available method seems to be getExternalStorageState , but I can't figure out how to pass the file path argument in. How do I rewrite the deprecated code, using getExternalStorageState or otherwise?唯一可用的方法似乎是getExternalStorageState ,但我不知道如何传入文件路径参数。如何使用getExternalStorageState或其他方式重写已弃用的代码? Thanks.谢谢。

For getting directory of public file.用于获取公共文件的目录。 you can use like below你可以像下面这样使用

File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS);
docFile = new File(storageDir, docFileName);

Actually getExternalFilesDir() is not a part of Environment bundle.实际上 getExternalFilesDir() 不是 Environment 包的一部分。 You can call it via the application context.您可以通过应用程序上下文调用它。

String file2 = getApplicationContext().getExternalFilesDir("folder")

The difference between these two are as follows这两者的区别如下

getExternalFilesDir() : This will return the path to files folder inside Android/data/data/your_package_name/. getExternalFilesDir() :这将返回 Android/data/data/your_package_name/ 中文件夹的路径。 It is used to store any required files for your app.它用于存储您的应用程序所需的任何文件。

getExternalStorageDirectory() : This returns the root path of you external SD card. getExternalStorageDirectory() :这将返回外部 SD 卡的根路径。

As far as the deprecation warning is concerned, Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory(), are deprecated.就弃用警告而言,不推荐使用 Environment.getExternalStorageDirectory() 或 Environment.getExternalStoragePublicDirectory()。 They still work, but will be removed soon, so you need to stop using those.它们仍然有效,但很快就会被删除,所以你需要停止使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM