简体   繁体   English

如何使用getExternalStorageDirectory()从外部存储读取子目录

[英]How to read a Subdirectory from External Storage with getExternalStorageDirectory()

I am developing an Android app that should read Files from the External Storage and display them in a Listview; 我正在开发一个Android应用,该应用应从外部存储读取文件并在Listview中显示它们; creating the subdirectory on the External Storage works fine. 在外部存储上创建子目录工作正常。 The problem is reading from the Subdirectory of the External Storage and I still haven´t figured out how to solve this problem. 问题是从外部存储的子目录中读取的,但我仍然没有弄清楚如何解决此问题。 The Listview displays all the subdirectories of the External Storage and I just want to display the content in one particular subdirectory. Listview显示了外部存储的所有子目录,我只想在一个特定的子目录中显示内容。 The problem consists in "casting" a String into a File Object, which is obviously not possible. 问题在于将字符串“广播”到文件对象中,这显然是不可能的。 Do I really need to open a FileInputStream for that? 我真的需要为此打开FileInputStream吗? Any help or hints would be very much appreciated. 任何帮助或提示将不胜感激。

I tried to attach the subdirectory as a string to the Environment.getExternalStorageDirectory() method, lik this: Environment.getExternalStorageDirectory() + "/Audio"; 我试图将子目录作为字符串附加到Environment.getExternalStorageDirectory()方法,就像这样:Environment.getExternalStorageDirectory()+“ / Audio”;

mTextMessage = findViewById(R.id.message);
BottomNavigationView navigation = findViewById(R.id.navigation); 
navigation.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
mListView = (ListView) findViewById(R.id.listView1);
file = Environment.getExternalStorageDirectory() + "/Audio";
fileNameList = getFileListfromSDCard();
mAdapter = new FlAdapter(this, R.layout.fl_list_item, fileNameList);
mListView.setAdapter(mAdapter);

The error message: 错误信息:

error: incompatible types: String cannot be converted to File 错误:类型不兼容:字符串无法转换为文件

Because getExternalStorageDirectory() returns String , you need to convert it to file before assigning it to file. 由于getExternalStorageDirectory()返回String ,因此需要在将其分配给文件之前将其转换为文件。 You can try like this. 您可以这样尝试。

File file = new File(Environment.getExternalStorageDirectory()+"/Audio");

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

相关问题 从getExternalStorageDirectory到内部存储 - From getExternalStorageDirectory to internal storage 不推荐使用 environment.getexternalstoragedirectory() 后如何访问外部存储 - How to get access to external storage since environment.getexternalstoragedirectory() is deprecated 如何使用getExternalStorageDirectory()从内部存储加载多首歌曲 - How to use getExternalStorageDirectory() to load multiple songs from internal storage Nexus 6 Environment.getExternalStorageDirectory() 无法创建目录外部存储 - Nexus 6 Environment.getExternalStorageDirectory() cannot create directory External storage Flutter:在外部存储路径上创建目录 - 路径提供程序 getExternalStorageDirectory() - Flutter : Create directory on external storage path - path provider getExternalStorageDirectory() 从外部存储读取文件 - read file from external storage 如何在Android应用上从外部存储解析或读取json文件 - How to parse or read json file from external storage on Android app 如何从 Android 10 中的外部存储“文档”中读取文件? - How to read files from external storage "Documents" in Android 10? Android - 如何从外部存储读取文本文件 - Android - How can I read text files from external storage 如何从外部存储读取属于其他应用的文件? - How to read file that belongs to other app from external storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM